# How can I typeset LaTeX source code with this very specific syntax highlighting? [closed]

I want to put some LaTeX source code in my PDF output with the style like this:

Color in text mode is white, in math mode is yellow and in comments is grey. For control sequences, I divide it into four kinds with different highlight mode:

• Commands in text mode are cyan, and its argument(s) (non-optional) is/are orange and italic.
• Commands in text mode are cyan, but its argument(s) (non-optional) is/are white and upright.
• Commands in text mode are rose-color, and its optional argument(s) is/are orange and italic, non-optional argument(s) is/are cyan and italic.
• Commands in math mode are purple, its arguments (optional or non-optional) should be rose-color and upright.

Remember that backslash before a command is always highlighted with the same color of the command, but delimiters of arguments remains white (color of body text).

I tried and searched the web for about ten hours, but still failed. Any hints or clues would be appreciated.

## closed as unclear what you're asking by jub0bs, Svend Tveskæg, Werner, Heiko Oberdiek, ThorstenMar 18 '14 at 16:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• minted is probably you best bet. – You Dec 1 '13 at 12:08
• Might be of your interest: tex.stackexchange.com/questions/102596/… – juliohm Dec 1 '13 at 14:12
• tex.stackexchange.com/q/8230/19356 might help. – kiss my armpit Dec 1 '13 at 20:45
• @You Thank you for you comment, and sorry for my late, since my computer shut down for several days. I've read the documentation of minted, however, I couldn't compile a simple example provided by it. What's worse, I'm not familiar with Python, so.. Should I open another question for those problems? – Tachibana Kanade Dec 6 '13 at 3:22
• @juliohm It seems that most people prefer Python-based packages, such as minted, or other script-based packages (am I right?). The reason, I guess, is that these script languages provide much more controlling and conditional switching than LaTeX itself does. I will follow your advice to study these packages when I get rid of my current article. :) – Tachibana Kanade Dec 6 '13 at 3:34

This solution isn't perfect (hence the community wiki), but it might get you or someone else started (if anyone would like to improve this solution, please feel free).

I've borrowed some code from Emphasize (color) contents between two delimiters in listings, but not the delimiters themselves to help the grouping delimiters.

It's a tricky problem because you essentially wish to change the listings style midway through the listings (I tried a few things, but couldn't get it working).

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

% https://tex.stackexchange.com/questions/147836/emphasize-contents-between-two-with-listings
\def\begindollar{\ttfamily\$\bgroup\color{yellow}\aftergroup\enddollar} \def\enddollar{\$\egroup}

% tweaked from above for { }
\def\beginbrace{\ttfamily\color{white}{\{}\bgroup\color{orange}\itshape\aftergroup\endbrace}
\def\endbrace{\color{white}{\}}\egroup}

% tweaked from above for [ ]
\def\beginbracket{\ttfamily\color{white}{[}\bgroup\color{orange}\itshape\aftergroup\endbracket}
\def\endbracket{\color{white}{]}\egroup}
\lstset{%
basicstyle=\ttfamily\color{orange},language={[LaTeX]TeX},
texcsstyle=*\color{cyan},
columns=flexible,
backgroundcolor=\color{black},
literate={usepackage}{\bgroup\color{purple}{usepackage}\egroup}1
{documentclass}{\bgroup\color{purple}{documentclass}\egroup}1,
moredelim = **[is][\begindollar]{\$}{\$},
moredelim=**[is][\beginbrace]{\{}{\}},
moredelim=**[is][\beginbracket]{[}{]},
}

\begin{document}

\begin{lstlisting}
\documentclass[10pt]{article}
\usepackage{tikz}
\begin{document}
\tikz \color{blue} $(a+b)$ $n \choose r$ % \tikz
\end{document}
\end{lstlisting}

\end{document}
• Oh, this is a fantastic work! However, it has some foibles, such as the backslash signs were colored differ from the following control sequence, and command(s) in math environment does/do not color as a special part. I will read the documentation more carefully and try to improve this answer later. Again, thank you very much! :) – Tachibana Kanade Dec 6 '13 at 3:50
• @TachibanaKanade yes indeed, it isn't perfect :) if you can improve it, please feel free! – cmhughes Dec 6 '13 at 4:48