title: Source code highlighting in LaTeX
body: <p>I need to highlight source code in LaTeX. The package <code>listings</code> seems to be the best choice for most use-cases and for me it was, until now.</p>
  
  <p>However, now I need more flexibility. Generally, what I’m looking for is a <em>real</em> lexer. In particular, I need (for an own language definition) to define (and highlight!) own number styles. <code>listings</code> does not allow highlighting numbers in code. However, I need to produce something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/Hh4W1.png" alt="Required result"></p>
  
  <p><code>listings</code> also cannot cope with arbitrary delimiters for strings. Consider the following valid Ruby code:</p>
  
  <pre><code>s = %q!this is a string.!
  </code></pre>
  
  <p>Here, <code>!</code> can be replaced by almost <em>any</em> delimiter.</p>
  
  <p>(That <code>listings</code> cannot handle Unicode is also quite vexing, but that’s another issue.)</p>
  
  <p><strong>Ideally, I am looking for an extension of <code>listings</code> that allows me to provide more complex lexing rules. But barring that, I am also searching for viable alternatives.</strong></p>
  
  <p><a href="https://stackoverflow.com/questions/300521/latex-package-to-do-syntax-highlighting-of-code-in-various-languages/1452086#1452086">Other threads</a> have suggested using <a href="http://pygments.org/" rel="noreferrer">Pygments</a> which can produce LaTeX output. There’s even a package – <a href="http://www.ctan.org/tex-archive/macros/latex/contrib/texments/" rel="noreferrer"><code>texments</code></a> – to ease the transition.</p>
  
  <p>However, this sorely lacks features. In particular, I am interested in <code>listings</code>-style line numbering, source code line references, and the possibility of embedding LaTeX in source code (options <code>texcl</code> and <code>mathescape</code> in <code>listings</code>).</p>
  
  <p>As an example, here’s a source code typeset with <code>listings</code> which shows some of the things that a replacement should also provide:</p>
  
  <p><img src="https://i.stack.imgur.com/9Guwv.png" alt="LaTeX listings example: Sideways addition">
  <sup><sub>[“Sideways addition” modified from Bit Twiddling Hacks]</sub></sup></p>