Latex insert Code Program + border + color + assembly code

Source: Internet
Author: User

Listings is a latex macro package dedicated to code formatting. It can use different fonts, colors, or colors for keywords, comments, and strings, or add borders, backgrounds, and other styles for the code.

1. Basic usage

The following provides a complete latex document for the C-language helloworld program code:

\ Documentclass {Article}
\ Usepackage {listings}

\ Begin {document}

\ Begin {lstlisting} [Language = C]

Int main (INT argc, char ** argv)

{


Printf ("Hello world! \ N ");


Return 0;

}
\ End {lstlisting}

\ End {document}

Note: Use listings
Syntax highlighting provided by the macro package requires the support of the xcolor macro package.

Shows the typographical Effect of syntax highlighting:

4. Add a border

The listings macro package provides many styles for code borders, which can be divided into shadow borders and rounded border borders. Here is only an example of a shadow border. For other border styles, see listings.
Macro package documentation, which provides some examples.

Below Latex
The source document adds a shadow border for the code and sets the shadow to light gray:

\ Begin {lstlisting} [Language = {[ANSI] c}, keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50}, frame = shadowbox,
Rulesepcolor = \ color {red! 20! Green! 20! Blue! 20}]

Int main (INT argc, char ** argv)

{


Printf ("Hello world! \ N ");


Return 0;

}
\ End {lstlisting}

 

5. Add a row number.

Most of the time, you need to explain the code in the Document. Only the code with a line number can make the explanation clearer, because you only need to say
What is the function of Line X code. If there is no row number, it is too cruel for the reader. They have to learn the row number information from your text description, and then find the corresponding code line in a row.

Listings macro package Parameters
Numbers to set the row number. The value of this parameter is left.
And right, indicating that the row number is displayed on the left or right side of the Code. Add a line number for the code with a border and set the font of the line number to \ tiny:

\ Begin {lstlisting} [Language = {[ANSI] c}, numbers = left,
Numberstyle = \ tiny, keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50}, frame = shadowbox,
Rulesepcolor = \ color {red! 20! Green! 20! Blue! 20}]

Int main (INT argc, char ** argv)

{


Printf ("Hello world! \ N ");


Return 0;

}
\ End {lstlisting}

 

6. Global settings

In the examples given above, the lstlisting environment is followed by many parameters.
In the lstlisting environment, it is meaningless to set so many parameters.

You can run the \ lstset command in
The introduction area of the latex source document sets the public parameters used by the lstlisting environment, as follows:

\ Documentclass {Article}
\ Usepackage {listings}
\ Usepackage {xcolor}

\ Begin {document}

\ Lstset {numbers = left,

Numberstyle = \ tiny,

Keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50 },

Frame = shadowbox,

Rulesepcolor = \ color {red! 20! Green! 20! Blue! 20}

}

\ Begin {lstlisting} [Language = {[ANSI] c}]

Int main (INT argc, char ** argv)

{


Printf ("Hello world! \ N ");


Return 0;

}
\ End {lstlisting}

\ End {document} 7 display Chinese Characters

Listings
By default, macro packages do not support code display that contains Chinese strings, but you can use escape strings to display Chinese characters.

In \ lstset
Set the start and end symbols of the escape string in the command. We recommend that you use the left quotation mark (')

\ Lstset {numbers = left,

Numberstyle = \ tiny, keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50 },

Frame = shadowbox, rulesepcolor = \ color {red! 20! Green! 20! Blue! 20 },

Escapeinside = ''}


......

\ Begin {lstlisting} [Language = {[ANSI] c}]

Int main (INT argc, char ** argv)

{


Printf ("'I love Chinese '! \ N ");


Return 0;

}
\ End {lstlisting} 8 adjust the margin

By default, the width of the listings code box is equal to that of the page core, and the top margin is too small. You can adjust it according to your own aesthetic idea. I usually set the left and right margin of the code box to 2em, and the top margin
1em. Use the default value for the bottom margin. The settings are as follows:

\ Lstset {numbers = left, numberstyle = \ tiny, keywordstyle = \ color {blue! 70}, commentstyle = \ color {red! 50! Green! 50! Blue! 50}, frame = shadowbox,
Rulesepcolor = \ color {red! 20! Green! 20! Blue! 20}, escapeinside = '', xleftmargin = 2em, xrightmargin = 2em,
Aboveskip = 1em} [to] http://blog.sina.com.cn/s/blog_50f21fed01016fud.html ------------------------------------------------------- assembly code Settings \ usepackage {xcolor}
\ Usepackage {listings}
\ Lstset {
Basicstyle = \ small, %
Escapeinside = '', %
Keywordstyle = \ color {red} \ bfseries, % \ underbar, %
Identifierstyle ={}, %
Commentstyle = \ color {blue}, %
Stringstyle = \ ttfamily, %
% Labelstyle = \ tiny, %
Extendedchars = false, %
Linewidth = \ textwidth, %
Numbers = left, %
Numberstyle = \ tiny \ color {blue}, %
Frame = trbl %
} ...... \ Begin {lstlisting} [Language = {[x86masm] assembler}]
Data Segment
Bf db 3 DUP (0); 'Save one input data'
Tabttl1 dB 'Code score ',' $ '; 'input schema'
Tabttl2 dB 'Code score ', 9, 'sexy', 9, 15,' $ '; 'output format'
;... \ End {lstlisting} -------------------------------- set the template (from: Wikipedia)
 1 \usepackage{listings} 2 \usepackage{color} 3   4 \definecolor{dkgreen}{rgb}{0,0.6,0} 5 \definecolor{gray}{rgb}{0.5,0.5,0.5} 6 \definecolor{mauve}{rgb}{0.58,0,0.82} 7   8 \lstset{ % 9   language=Octave,                % the language of the code10   basicstyle=\footnotesize,           % the size of the fonts that are used for the code11   numbers=left,                   % where to put the line-numbers12   numberstyle=\tiny\color{gray},  % the style that is used for the line-numbers13   stepnumber=2,                   % the step between two line-numbers. If it's 1, each line 14                                   % will be numbered15   numbersep=5pt,                  % how far the line-numbers are from the code16   backgroundcolor=\color{white},      % choose the background color. You must add \usepackage{color}17   showspaces=false,               % show spaces adding particular underscores18   showstringspaces=false,         % underline spaces within strings19   showtabs=false,                 % show tabs within strings adding particular underscores20   frame=single,                   % adds a frame around the code21   rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. commens (green here))22   tabsize=2,                      % sets default tabsize to 2 spaces23   captionpos=b,                   % sets the caption-position to bottom24   breaklines=true,                % sets automatic line breaking25   breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace26   title=\lstname,                   % show the filename of files included with \lstinputlisting;27                                   % also try caption instead of title28   keywordstyle=\color{blue},          % keyword style29   commentstyle=\color{dkgreen},       % comment style30   stringstyle=\color{mauve},         % string literal style31   escapeinside={\%*}{*)},            % if you want to add LaTeX within your code32   morekeywords={*,...}               % if you want to add more keywords to the set33 }

 

 

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.