C language code for latex formatting

Source: Internet
Author: User
Document directory
  • 1. Basic usage
  • 4. Add a border
  • 5. Add a row number.
  • 6. Global settings
  • 7. display Chinese Characters
  • 8. Adjust the margin

Reprinted from http://blog.sina.com.cn/s/blog_51e68f8d0100avil.html

Reprinted from http://blog.linuxgem.org/lyanry/show/319.html

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: To use the syntax highlighted by the listings macro package, xcolor macro package is required.

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 just an example of a shadow border. For other border Styles, you can refer to the listings macro package documentation, which provides some examples.

The following latex 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}

The typographical effect is as follows:

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 explain the role 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.

The listings macro package uses the numbers parameter to set the row number. The value of this parameter is left or right, indicating whether the row number is displayed on the left or right 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}

Shows the typographical effect:

6. Global settings

In the examples given above, the lstlisting environment is followed by many parameters. If so many parameters need to be set every time the lstlisting environment is used, it is meaningless.

You can use the \ lstset command to set the public parameters used in the lstlisting environment in the introduction area of the latex source document, 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

By default, the listings macro package does not support code Display containing Chinese strings, but the "escape" string can be used to display Chinese characters.

Set the start and end symbols of the escape string in the \ lstset command. The recommended symbols are the left quotation marks (').

\ 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 margins of the code box to 2em, the top margin to 1em, and the bottom margin to use the default value. 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}

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.