This is a guest post by Pieter Goetschalckx. As a fellow classmate of mine we've been working together for most of our projects this year, resulting in some LaTeX-related results like the UAStyle and (unpublished) software for automated Enigma deciphering or numerical continuation of n-dimensional manifolds.

Pygments in combination with the minted package is a very powerful syntax highlighter for LaTeX, with the possibility to use colored, bold and italic text. Unfortunately, the Computer Modern Typewriter font includes no bold shapes. Latin Modern does, but the difference is almost unnoticeable. Therefore I prefer Beramono, which in addition is a nice combination with Bitstream Charter. So far my personal taste.

While using this font setup for highlighting Cython code, I discovered a bug in the LatexFormatter of Pygments. When highlighting the following (rather useless) Cython function, the output (with the default Pygments style) is:

cpdef int add(int a, int b):
return a + b


Actually, this fragment was highlighted with the HTMLFormatter. The LatexFormatter on the other hand outputs something like this:

cpdef int add(int a, int b):
return a + b


when converted to HTML, obviously. The actual LaTeX code would be

resulting in this pdf output. As the output differs in the handling of the type keywords a question arises: which Formatter is correct?

Let's take a look at the style definition in pygments/styles/default.py:

Because int is a Keyword.Type, it should not be bold. Simplified, the macro \PY{k+kt}{int} executes \PY@tok@k and \PY@tok@kt before printing \PY@bf{int}. \PY@tok@k sets \PY@bf=\textbf, and \PY@tok@kt does nothing with \PY@bf, so the result is \textbf{int}.

It looks like the LatexFormatter tries to implement inheritance and fails. Funny thing is, inheritance doesn't need to be implemented, because another part of Pygments already takes care of it. We can replace \PY{k+kt}{int} by \PY{kt}{int}. This can be done in pygments/formatter/latex.py, by replacing

with

Additionally, some macro definitions in the preamble can be simplified. This will not change anything visually, but it removes unused features. This can be done by replacing

with

Now we've arrived at the correct version of the backend, but there are still some issues with the CythonLexer. In most cases this will suffice (unless other lexers contain bugs as well), yet for completeness' sake, I give my version of the CythonLexer. It can be found at 314eter / pygments-CythonLexer.

The result: after all the described modifications, we should arrive at this highlighted piece of code.

As a matter of fact, this issue with the LaTeXFormatter is known to Pygments (by means of this bug report), but they've put the issue on hold. If you encounter it, this workaround could be the solution.

And this kids is why you should never dig into TeX, it's addictive :).