Source Code Listing
The source environment is used to format and highlight the source code for most any programming language. A number of options are provided for including line numbers highlighting specific lines and applying a variety of default styles. A number of examples are provided below the environment definition.
\begin{source}[
lang=name
linenums=0|1
linestart=#
linestep=#
tabwidth=#
highlight='#, #, #,...'
style=classnames
....
\end{source}
- lang
-
The name of the language for the code contained within the block. A list of supported languages is provided on the Supported Languages page. If no language is provided, then the default specified by the configuration setting SourceLang is used. The default is for plain text. To display source code that is not highlighted, use the code environment instead.
- linenums
-
Determines whether line numbers are included in the list. The default is false (0).
- linestart
-
Indicates the first line number used when numbering the lines. This is useful a source listing is split across multiple blocks or only a portion of the code is included.
- linestep
-
Indicates which line numbers, if any, should be boldfaced. For a value of 2, every other line number would be boldfaced. The default is for every 5th line number to be boldfaced. If set to 0, no line numbers are styled in boldface.
- tabwidth
-
Sets the number of spaces to use for tab (\t) replacement.
- highlight
-
A list of one or more line numbers to be highlighted. The line numbers in the list can be separated by spaces, commas, or semicolons.
- style
-
The name of one or more css class selectors for modifying the default style of the program code block. A standard style is defined for the source code environment
Value Description listing use a fancy style that mimics zebra stripes or green bar paper.
Examples
- \begin{source}[lang=cpp]
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \end{source}
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \begin{source}[lang=cpp linenums=1]
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \end{source}
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
Examples - Listing Style
- \begin{source}[lang=cpp style=listing]
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \end{source}
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \begin{source}[lang=cpp linenums style=listing]
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);
- \end{source}
- float avg;
- avg = (grade1 + grade2 + grade3) / 3.0;
- if(avg >= 90)
- printf("Excellent Work!\n");
- printf("Your average is %0.2f\n", avg);