Nicolas Neu

dies & ditt.

LaTex Sytax Highlighting Using Minted, Pygments and Latexmk

This post mostly serves as a reminder for myself. There are already dozen of tutorials on the web describing how to display code inside a latex document. Yet, every time i needed to typeset code I found myself on google and stackexchange looking for the best way to do it. All solutions come with some kind of caveat, so maybe the best way to look at this post is as some kind of best practices document.

First of all: Why not listings? That’s what I have been using so far and it works. Never was quite satisfied with it though. Support for the languages I use is built in, at least rudimentary. Occasionally keywords are missing which need to be patched in, which is not a big problem itself but it always felt kind of hacky and I would prefer a definite solution. Secondly, although colours are supported it does not do so out of the box. Colormappings have to be added by hand and the ones I found online always felt off. So what else is there?

The second solution you will find if you google this longer than one minute is the minted package which relies on the python pygments package for syntax highlighting. It does a superb job of highlighting without any manual fiddling, colours feel right and the output overall looks just as beautiful as the rest of your tex’d document.

Installation of pygments is business as usual:

pip install pygments

Then in the document header just include the minted package and fancyvrb:


The only downside is the additional requirement of pygments. I try to avoid solutions like this. I occasionally switch workstations which means dependencies might not be set up and especially on windows dealing with python, packages and the command line in general is always a little bit more painful than on other systems. Unfortunately I had no success getting around this. Pygments supports tex output through a formatter which I used to generate tex files with syntax highlighting macros already in place. The macros this is relying on are undefined and compiling fails. There probably is a way to set them up, but apart from mentioning a get_style_defs() method somewhere in the pygments package, I found no documentation of actually getting these definitions. This does work however when the minted package is included. If anyone knows how to do this, I’d be happy to hear about it.

Did I say only downside? Correction! Two downsides. As an external library is invoked, latex needs to be called with the –shell-escape argument. I am usually building my documents with latexmk which I did not expect to be very cooperative. I was wrong. If you don’t already have a .latexmkrc in your home directory, just create one with the following content and you are done:

$pdf_mode = 1;
$pdflatex = 'pdflatex --shell-escape interaction=nonstopmode %O %S -file-line-error -synctex=1';

To use it in your document you can define a code area like this:

    print 'waddap?'

The end result should look something like this. Neat eh?