Issue #461 resolved

Better LaTeX integration

Konrad Förstner
created an issue

The Pygments LaTeX formatter works great, but it doesn’t support including arbitrary LaTeX into the source code.

I propose a patch to, along with two required filters, to amend this and furthermore allow a lot of fancy things that (at the moment) are pretty much restricted to the listings.sty package.

To showcase the changes, have a look at the following picture, which has been produced with trivial LaTeX code (and the proposed changes in Pygments):


The most important change to allow arbitrary LaTeX was to change the way the LaTeX formatter issued formatting commands: instead of the style @PY[][] it now uses the conventional \\PY{}{} syntax (incidentally, was there a reason for the previous syntax? The change ''appears'' to work just fine).

To allow arbitrary LaTeX, a filter is also required that merges consecutive tokens of the same type. The reason? Consider nested comments in the source code. At the moment, Pygments doesn’t create ''one'' token for them but several. If such a nested token happens to include LaTeX (as in my example above), this LaTeX code will be split into several tokens, resulting in an error.

An additional filter, GobbleFilter implements the gobble feature known from listings.sty and fancyvrb.sty (and ''no'', this cannot be handled by passing additional options to fancyvrb.sty because at that point, the code is already littered with formatting instructions which get in the way).

(FYI, this patch was triggered by a [ discussion on Stack Overflow].)

Comments (3)

  1. gbrandl

    BTW, I'm not sure anymore what the @[] command chars were for, but I assume it was left over from an older revision which made it necessary.

  2. Log in to comment