6

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}
1
  • probably you need to change the catcode... in linstings' manner of course.
    – Symbol 1
    Sep 14 '15 at 15:27
4

You can use literate option to replace desired commands with highlighted ones.

Problem is that, when you use starred texcsstyle option, code added to SelectCharTable hook, by tex aspect (p.152 of listings developers guide), partially overrides code used by literate option (p. 137).

If you want listings to properly use literate replacement, you need to "rehook" code used by literate, after loading tex aspect.

\documentclass{scrartcl}

\usepackage{fontspec}

\usepackage{listings}
\usepackage{xcolor}


\lstset{
  language=[LaTeX]TeX,
  basicstyle=\ttfamily,
  texcsstyle=*\color{cyan},
  literate=*%
    {\\!}{{\textcolor{cyan}{\textbackslash{}!}}}2
    {\\\\}{{\textcolor{cyan}{\textbackslash{}\textbackslash{}}}}2,
}

% "Rehook" literate char table.
\makeatletter
\lst@AddToHook{SelectCharTable}
    {\ifx\lst@literate\@empty\else
         \expandafter\lst@Literate\lst@literate{}\relax\z@
     \fi}
\makeatother


\begin{document}

\begin{lstlisting}
\begin{equation}
  \left(\frac{1}{2}\right)^{\! 2}
  \gamma!
  \\!
  \\left
\end{equation}
\end{lstlisting}

\end{document}

pdf screen shot

Of course you can add all replacements, as in Ismo's answer, just without ¡s at the beginning.

2
2

So here is another try. Unfortunately it requires a bit of extra markup and is hence unsuitable for reading in a functioning TeX file as a listing.

Of course the ¡ can be replaced by any character that is never needed in the code.

See here how to use a character with a codepoint larger than 255.

EDIT: Even a space ("\ " in literate) will work.

\documentclass{scrartcl}

\usepackage{fontspec}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
  language={[LaTeX]TeX},
  basicstyle=\ttfamily,
  texcsstyle=*\color{red},
  extendedchars,
}

\lstset{
  literate=*{¡\\!}{{\textcolor{red}{\textbackslash!}}}{2}
            {¡\\"}{{\textcolor{red}{\textbackslash"}}}{2}
            {¡\\\#}{{\textcolor{red}{\textbackslash\#}}}{2}
            {¡\\\$}{{\textcolor{red}{\textbackslash\$}}}{2}
            {¡\\\%}{{\textcolor{red}{\textbackslash\%}}}{2}
            {¡\\\&}{{\textcolor{red}{\textbackslash\&}}}{2}
            {¡\\'}{{\textcolor{red}{\textbackslash'}}}{2}
            {¡\\(}{{\textcolor{red}{\textbackslash(}}}{2}
            {¡\\)}{{\textcolor{red}{\textbackslash)}}}{2}
            {¡\\*}{{\textcolor{red}{\textbackslash*}}}{2}
            {¡\\+}{{\textcolor{red}{\textbackslash+}}}{2}
            {¡\\,}{{\textcolor{red}{\textbackslash,}}}{2}
            {¡\\-}{{\textcolor{red}{\textbackslash-}}}{2}
            {¡\\.}{{\textcolor{red}{\textbackslash.}}}{2}
            {¡\\/}{{\textcolor{red}{\textbackslash/}}}{2}
            {¡\\:}{{\textcolor{red}{\textbackslash:}}}{2}
            {¡\\;}{{\textcolor{red}{\textbackslash;}}}{2}
            {¡\\<}{{\textcolor{red}{\textbackslash<}}}{2}
            {¡\\=}{{\textcolor{red}{\textbackslash=}}}{2}
            {¡\\>}{{\textcolor{red}{\textbackslash>}}}{2}
            {¡\\?}{{\textcolor{red}{\textbackslash?}}}{2}
            {¡\\[}{{\textcolor{red}{\textbackslash[}}}{2}
            {¡\\\\}{{\textcolor{red}{\textbackslash\textbackslash}}}{2}
            {¡\\]}{{\textcolor{red}{\textbackslash]}}}{2}
            {¡\\\^}{{\textcolor{red}{\textbackslash\textasciicircum}}}{2}
            {¡\\\{}{{\textcolor{red}{\textbackslash\{}}}{2}
            {¡\\|}{{\textcolor{red}{\textbackslash|}}}{2}
            {¡\\\}}{{\textcolor{red}{\textbackslash\}}}}{2}
            {¡\\\~}{{\textcolor{red}{\textbackslash\textasciitilde}}}{2}
}

\begin{document}
\begin{lstlisting}
  ¡\! ¡\" ¡\# ¡\$ ¡\% ¡\& ¡\' ¡\( ¡\) ¡\* ¡\+ ¡\, ¡\- ¡\. ¡\/ ¡\: ¡\; ¡\< ¡\= ¡\> ¡\? ¡\[ ¡\\ ¡\] ¡\^ ¡\{ ¡\| ¡\} ¡\~
\end{lstlisting}
\end{document}
1

This was easier than I thought.

EDIT: not quite. Of the characters listed in "other" in table 2 of the listings documentation, only !, " and # actually work like this.

\documentclass{scrartcl}

\usepackage{fontspec}

\usepackage{listings}
\usepackage{xcolor}

\lstset{
  language=[LaTeX]TeX,
  basicstyle=\ttfamily,
  texcsstyle=*\color{cyan},
  alsoletter={!},
  moretexcs={!},
}

\begin{document}
\begin{lstlisting}
\begin{equation}
  \left(\frac{1}{2}\right)^{\! 2}
\end{equation}
\end{lstlisting}
\end{document}
2
  • 1
    Problem with this is that \gamma! wouldn't highlight gamma.
    – Manuel
    Sep 15 '15 at 14:37
  • 1
    and that it does not work for \\
    – MaxNoe
    Sep 15 '15 at 15:10

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.