How can I achieve it with lstlisting, that LaTeX commands like \\
or \!
are properly highlighted as syntax?
Adding those commands (with or without backslash) to moretexcs
does not deliver the indented result.
\documentclass{scrartcl}
\usepackage{fontspec}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
language=[LaTeX]TeX,
basicstyle=\ttfamily,
texcsstyle=*\color{cyan},
moretexcs={!},
}
\begin{document}
\begin{lstlisting}
\begin{equation}
\left(\frac{1}{2}\right)^{\! 2}
\end{equation}
\end{lstlisting}
\end{document}
Best Answer
You can use
literate
option to replace desired commands with highlighted ones.Problem is that, when you use starred
texcsstyle
option, code added toSelectCharTable
hook, bytex
aspect (p.152 of listings developers guide), partially overrides code used byliterate
option (p. 137).If you want
listings
to properly use literate replacement, you need to "rehook" code used byliterate
, after loadingtex
aspect.Of course you can add all replacements, as in Ismo's answer, just without
ยก
s at the beginning.