Source code highlighting in LaTeX

I need to highlight source code in LaTeX. The package listings seems to be the best choice for most use-cases and for me it was, until now.

However, now I need more flexibility. Generally, what I’m looking for is a real lexer. In particular, I need (for an own language definition) to define (and highlight!) own number styles. listings does not allow highlighting numbers in code. However, I need to produce something like this:

Required result

listings also cannot cope with arbitrary delimiters for strings. Consider the following valid Ruby code:

s = %q!this is a string.!

Here, ! can be replaced by almost any delimiter.

(That listings cannot handle Unicode is also quite vexing, but that’s another issue.)

Ideally, I am looking for an extension of listings that allows me to provide more complex lexing rules. But barring that, I am also searching for viable alternatives.

Other threads have suggested using Pygments which can produce LaTeX output. There’s even a package – texments – to ease the transition.

However, this sorely lacks features. In particular, I am interested in listings-style line numbering, source code line references, and the possibility of embedding LaTeX in source code (options texcl and mathescape in listings).

As an example, here’s a source code typeset with listings which shows some of the things that a replacement should also provide:

LaTeX listings example: Sideways addition
[“Sideways addition” modified from Bit Twiddling Hacks]

3 Answers
3

Leave a Comment