tquadrat.org

View on GitHub

Integration of Source Code Listings

To integrate ‘listings’ – source code of programs, scripts and alike – into a document, use the package listings. It allows syntax highlighting for several programming languages.

To integrate the package, put the line \usepackage{listings} to the preamble of your document.

The configuration of the package is done with the \lstset environment.

\lstset{
language=Java,                    % The programming language
backgroundcolor=\color[gray]{.9}, % The background color

basicstyle=\ttfamily\footnotesize\bfseries,
                                  % The basic style
commentstyle=\color[gray]{.2}\bfseries,
                                  % The style for the comments if different
                                  % from the basic style
identifierstyle=,                 % The style for identifiers
stringstsyle=,                    % The style for String constants
keywordstyle=\color[gray]{.2},    % The style for the keywords of the selected
                                  % programming language if different from the
                                  % basic style
showstringspaces=true,            % A special character is displayed for the
                                  % blanks in a String

frame=single,                     % The listing should be surrounded by a frame
framerule=0.2pt,                  % The width of that frame

xleftmargin=.2cm,                 % additional indentation for the source code
xrightmargin=.2cm,                % right margin

breaklines=true,                  % source code lines can be broken at the
                                  % margin
breakatwhitespace=true,           % lines are broken at whitespace

inputencoding=utf8,               % the input encoding
extendedchars=true,               % allows non-ASCII characters
literate=                         % the mapping of extended characters to
                                  % TeX/LaTeX constructs; required for nearly
                                  % all non-ASCII character in the source code
  {á}1 {Á}1 {à}1 {À}1 {ä}1 {Ä}1 
  {â}1 {Â}1 {ã}1 {Ã}1 {æ}1 {Æ}1
  {å}1 {Å}1
  {ç}1 {Ç}1
  {é}1 {É}1 {è}1 {È}1 {ë}1 {Ë}1 
  {ê}1 {Ê}1 {}1 {}1
  {í}1 {Í}1 {ì}1 {Ì}1 {ï}1 {Ï}1
  {î}1 {Î}1 {ĩ}1 {Ĩ}1
  {ñ}1 {Ñ}1 
  {ó}1 {Ó}1 {ò}1 {Ò}1 {ö}1 {Ö}1 
  {ô}1 {Ô}1 {õ}1 {Õ}1 {œ}1 {Œ}1    
  {ø}1 {ő}}1 {Ő}}1
  {ß}1
  {ú}1 {Ú}1 {ù}1 {Ù}1 {ü}1 {Ü}1
  {û}1 {Û}1 {ũ}1 {Ũ}1 {ű}}1 {Ű}}1 
  {}1 {£}1 
  {«}1 {»}1 
  {¿}1 {¡}1
  {©}1
  {}1
}

The format for the literate= entries is {<source character>}<effectice length of the replacement sequence>. To print the copyright symbol in the listing, the literate sequence would be {©}1.

The list above is not exhaustive, it just contains those characters that I have used within a source code file at some time in the past.

The package is used basically in two ways:

The environment allows to overwrite some of the settings provide with \lstset: \begin{lstlisting}[language=python, numbers=left] … \end{lstlisting}.

\lstinline|…| works similar to \verb: the vertical bars are just the delimiters for the value; any character that is not part of the value can be used for that.