11

I want to highlight a LaTeX code (including backslash \, e.g. \begin). Unfortunately, by default a LaTeX listings package does syntax highlighting only keywords WITHOUT back slash (e.g. begin).

16
\documentclass{article}
\usepackage[T1]{fontenc}  
\usepackage[scaled=0.82]{beramono}  
\usepackage{listings,xcolor}
\begin{document}

\begin{lstlisting}[basicstyle=\small\ttfamily,language={[LaTeX]TeX},
                   texcsstyle=*\color{red}\bfseries,
                   keywordstyle=\color{blue}\bfseries,
                   morekeywords=alignat,moretexcs=intertext]
\begin{alignat*}{4}
   y &= -4   &+ 3 &+4     &-7      \\
   y &=      &+ 3 &       &-7      \\
   \intertext{Therefore}
   a &= b    &d   &= cccc &e  &= d \\
   a &= bbbb &d   &= c    &e  &= d
\end{alignat*}
\end{lstlisting}

\end{document}

enter image description here

  • Herbert, thank you, an excellent answer, it is simple and effective code! – Vladimir S. May 9 '11 at 7:41
  • 1
    Is it simply the star prepended to the texcsstyle that does the job ? – Carel Nov 24 '15 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.