# How to add custom C++ keywords to be recognized by Minted?

I have defined several structures and types in a C++ project. I have to write some documentation for this code, and I use the minted package to typeset my code.

I would like to be able to add some keywords to the minted database, so minted will also recognize these and give them the appropriate color. (I would like to have them colored in the same way a type, eg. double would be colored.)

Minted uses Pygments to parse source-code, so one way to go about this is by modifying pygments/lexers/compiled.py and adding your types there (Somewhere about line 210 looks reasonable to me). This is what I had to do when Pygments was incorrectly parsing a symbol in some Erlang I'd written.

The obvious disadvantage of this is that Pygments is now non-standard. This will mean a number of things:

• This change has to be made on all PCs that want to compile this document, meaning the document will be built inconsistantly or lots of people have to have modified Pygments installations.
• Future updates to Pygments could break your modifications and so your document may not be correctly syntax-highlighted in the future.
• You may incorrectly parse/highlight future documents if you use these keywords in a different way (So it may print a variable name in the same way it prints a type, for example).
• @Edd another disadvantage is that the modifications will only be valid on a single pc. – pmav99 May 12 '11 at 16:31
• @pmav99 Thanks. I know it's not a good method it's just the only way I know. I've converted the disadvantages to a list and included yours to try and make it clear this is very non-ideal. – Edd May 12 '11 at 16:35
• @Edd I think that the change doesn't mean that the document is not compilable, but rather that the output will not be the expected one (i.e. no highlighting). – pmav99 May 12 '11 at 17:16
• @pmav99 Yes of course. Sorry; brain really wasn't engaged. – Edd May 12 '11 at 17:49
• yeah it's just for a school project, so there's only one computer used to compile the file. Actually the class is vectorwhich I would want to be highlighted every time, so there's no problem in the future. – romeovs May 12 '11 at 18:24

May be late for you but can help others.

The better solution is to write your own lexer, as pmav99 said, but on top of another lexer (in your case the CppLexer).

It's very simple! I did this very quickly to add some extra keywords for ruby.

• Also, instead of installing the lexer in your Pygments installation, you can keep the lexer with your TeX documents. So, if your lexer file is named "a.py" and the class is "ALexer", put a.py in the same directory as your main TeX source file, then specify the language in minted with \begin{minted}{a.py:ALexer -x} ... \end{minted}. Probably not officially supported since it's just jamming in the necessary command line args to pygmentize, but it works for me. – Jonathan Schuster Jun 20 '17 at 13:26
• @JonathanSchuster: I would really love to do it, because I'm working with overleaf (so I cannot modify conf files, AFAIK). I tried copying Hugo file (and renaming the class to ALexer and the file to a.py, but what I get is "Package minted Error: Missing Pygments output; \inputminted was probably given a file that does not exist--otherwise, you may need the outputdir package option, or may be using an incompatible build tool." This is regardless the name I put before the .py in \begin{minted}{a.py:ALexer -x}, so it does not seem a path problem. Any help would be great. – massi Nov 28 '18 at 20:20
• @massi The .py file has to be at the root of your TeX project; it can't be in a subdirectory (at least not without changing the path to it in the minted environment, but I'm not sure if that works). If that's not the problem, you may want to create a separate question on this Stack Exchange about your issue. – Jonathan Schuster Dec 3 '18 at 16:40
• @JonathanSchuster: I'm sorry, it works like a charm. The file already was in the root of the TeX project (though I did not know it was necessary), I changed obviously all the RubyLexer occurrences, but I simple missed to change the third element in the <pre> <code>from pygments.lexers.agile import BashLexer</code></pre> which has to be  from pygments.lexers.shell import BashLexer So I'm writing it for future reference – massi Dec 12 '18 at 18:11

Another (obvious) solution would be to write your own lexer (i.e. a superset of c++)

• This seems a nice way to do it, but I don't have time to spend on learning how to write the lexer.. – romeovs May 12 '11 at 19:39
• Yeah of course, I just added it for reference. – pmav99 May 12 '11 at 20:09