1

I am trying to set some LaTeX code with minted.

However, minted (or rather the lexer which it is using) assumes the @ character to be catcode other, as it usually is. In the code that I want to format, however, @ is always catcode letter. Therefore command names are highlighted incorrectly.

Is there an easy option to change that or would I need to write a custom lexer? Does someone know a good tutorial on how to write a custom lexer?

1 Answer 1

1

I have managed to hack myself into it.

With

$ find / -mount -name "*latex*" 2>/dev/null

I have found the location of pygments.

The file lexers/_mapping.py contains a line

'TexLexer': ('pygments.lexers.markup', 'TeX', ('tex', 'latex'), ('*.tex', '*.aux', '*.toc'), ('text/x-tex', 'text/x-latex')),

telling me that the lexer is contained in the file pygments/lexers/markup.py and that the class is called TexLexer.

I have copied the file to lexer/latex_atletter.py in the directory of my LaTeX project and removed all unneeded classes from it so that only the TexLexer class remains.

This class contains a line

(r'\\([a-zA-Z]+|.)', Keyword, 'command'),

I have added an @ to the allowed letters in the regex (and a comment at the end of the line that I have edited it).

From here I have learned how to use this custom lexer in my LaTeX document: I have added

\def\mylatexlexer{lexer/latex_atletter.py:TexLexer -x}
\newmintedfile[inputlatex]{\mylatexlexer}{}

to the preamble and replaced every occurence of \inputminted{latex} with \inputlatex.

Now, commands containing an @ symbol are highlighted correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .