Inserting C language code into latex

Source: Internet
Author: User

Latex is a text-typesetting language that can be formatted with all the effects we want. And the advantage of code layout is easy to modify the plate, so in the text content of the composition, latex application is very extensive.

When we need to insert code in Latex, we need to use the \USEPACKAGE{LISTINGS} macro package. For example, insert a simple C language code

#include <stdio.h>int main (intChar * * argv)    {printf ("  Hello, world!\n");     return 0 ;}

To put the above hello,world! This c language code uses latex to achieve the effect of typesetting, latex script is as follows

\documentclass{article}\usepackage{Listings}\usepackage{Xcolor}% code Coloring Macro Pack \usepackage{CJK}% display Chinese macro Pack \lstset{Basicstyle=\tt,% line number Numbers=left, rulesepcolor=\color{red!20!green!20!blue!20}, Escapeinside=", Xleftmargin=2em,xrightmargin=2em, aboveskip=1em,% background frame framexleftmargin=1. 5mm, Frame=shadowbox,% background color backgroundcolor=\COLOR[RGB]{245,245,244},% style Keywordstyle=\color{Blue}\bfseries, Identifierstyle=\BF, NumberStyle=\COLOR[RGB]{0,192,192}, Commentstyle=\IT\COLOR[RGB]{96,96,96}, Stringstyle=\RMFAMILY\SLSHAPE\COLOR[RGB]{128,0,0},% display space showstringspaces=false}\begin{Document}\begin{cjk*}{GBK}{Song}\lstset{Language=c}\begin{lstlisting}% Insert the code to display # include<stdio.h>int main (int argc, char**argv){/* ' print ' hello,world*/printf ("Hello, world!\n"); return 0;}\End{lstlisting}\End{cjk*}\End{Document}

The latex script above can show comments in the C language (including Chinese comments), code coloring, and add code line numbers. The effect is as follows

Inserting C language code into latex

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.