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.
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,
or if you want to display another language apart from the default, and with no line numbers
It is good practice to provide both the
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
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
We then use it thusly
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