LaTeX Syntax Highlighting

There are myriad ways of enabling syntax highlighting to a LaTeX document, either for pseudo code, or large chunks of scripts.

My personal favourite syntax highlighting option is via the listings package. It provides great features for full customisations alongside a minimal interface.

The full code is given now for those who want to dive straight in. Place all of the following in the header of your LaTeX document. It, of course, uses the Solarized colour scheme.

% Required packages
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}

% Solarized colour scheme for listings
\definecolor{[email protected]}{HTML}{002B36}
\definecolor{[email protected]}{HTML}{073642}
\definecolor{[email protected]}{HTML}{586e75}
\definecolor{[email protected]}{HTML}{657b83}
\definecolor{[email protected]}{HTML}{839496}
\definecolor{[email protected]}{HTML}{93a1a1}
\definecolor{[email protected]}{HTML}{EEE8D5}
\definecolor{[email protected]}{HTML}{FDF6E3}
\definecolor{[email protected]}{HTML}{B58900}
\definecolor{[email protected]}{HTML}{CB4B16}
\definecolor{[email protected]}{HTML}{DC322F}
\definecolor{[email protected]}{HTML}{D33682}
\definecolor{[email protected]}{HTML}{6C71C4}
\definecolor{[email protected]}{HTML}{268BD2}
\definecolor{[email protected]}{HTML}{2AA198}
\definecolor{[email protected]}{HTML}{859900}

% Define C++ syntax highlighting colour scheme
\lstset{language=C++,
        basicstyle=\footnotesize\ttfamily,
        numbers=left,
        numberstyle=\footnotesize,
        tabsize=2,
        breaklines=true,
        escapeinside={@}{@},
        numberstyle=\tiny\color{[email protected]},
        keywordstyle=\color{[email protected]},
        stringstyle=\color{[email protected]}\ttfamily,
        identifierstyle=\color{[email protected]},
        commentstyle=\color{[email protected]},
        emphstyle=\color{[email protected]},
        frame=single,
        rulecolor=\color{[email protected]},
        rulesepcolor=\color{[email protected]},
        showstringspaces=false
}

I have included the listings configuration for C++ syntax highlighting, which becomes the default language, and may be changed to suit your own needs.

You can then use the lstlisting environment for block code syntax highlighting,

\begin{lstlisting}[caption={Write to stdout},label={lst:stream-out}]
std::cout << "Hello world" << std::endl;
\end{lstlisting}

or if you want to display another language apart from the default, and with no line numbers

\begin{lstlisting}[language=bash,caption={Echo command},label={lst:bash-echo},numbers=none]
echo "Hello world"
\end{lstlisting}

It is good practice to provide both the caption and label attributes, just so you (or others) may refer back to a particular code snippet at a later date.

If you find that you have some extra vertical space around the code snippets, use the following code

\renewcommand{\refname}{\vskip -1cm}

For full code listings of your program’s source files, perhaps placed in your Appendix, it is best to start each on its own page. Additionally, we (should) want to include the external source file, instead of pasting its contents directly into the LaTeX document. This means any changes made to the source file will be automatically included in the LaTeX listings, thus saving a lot of trouble tracking changes.

To do this, we can first define a short macro

\newcommand{\codelst}[1]{\lstinputlisting[caption=\texttt{\protect\detokenize{#1}}]{#1}\newpage}

We then use it thusly

\codelst{path/to/source/file}

The macro will insert a page break after the code listing, so that the next listing will begin on a new page. The path to the source file is also used as the caption, and will appear in the list of listings, via the \lstlistoflistings command.