6

Is there a way to highlight a single backslash (\) within LaTeX code with listings or showexpl? In the following example, \LaTeX is correctly highlighted, but not the following backslash.

\documentclass{article}
\usepackage{xcolor,showexpl}
\lstset{language=[LaTeX]Tex,
   basicstyle=\ttfamily,
   keywordstyle=\color{red},
   texcsstyle=*\color{blue}
   }

\begin{document}

\begin{LTXexample}
Test \LaTeX\ test
\end{LTXexample}

\end{document}
4

See also How can I color braces in showexpl?

The problem is that showexpl, texcl, moretexcs, morekeyword, and literate are not compatible in some ways. So just try out everything you think of.

\documentclass{article}
    \usepackage{xcolor,showexpl}
        \lstset{language=[LaTeX]Tex,basicstyle=\ttfamily,texcsstyle=*\color{blue}}
        \lstdefinestyle{myLaTeX}{literate={~}{\textcolor{blue}{\char`\\\char`\ }}2}
\begin{document}
    \def~{}
    \begin{LTXexample}[style=myLaTeX]
        Test \LaTeX~test
    \end{LTXexample}
\end{document}

This also works, with wrong spacing. (I am using U+00A0, the non-breaking space. But there is an ascii-space before test)

\documentclass{article}
\usepackage{xcolor,showexpl}
    \lstset{language=[LaTeX]Tex,basicstyle=\ttfamily,texcsstyle=*\color{blue}}
    \lstdefinestyle{myLaTeX}{moretexcs={ }}
\begin{document}
    \def\ {}
    \begin{LTXexample}[style=myLaTeX]
        Test\LaTeX\  test
    \end{LTXexample}

\end{document}
  • I have the same problem, except I don't need the backslash to be colored. Is there an easier/better way to get the "char`\ " character after the backslash in this case. – Gaussler Apr 15 '15 at 19:40
  • 1
    @Gaussler Do you mean \char`\\\textcolor{blue}{\char`\ }? – Symbol 1 Apr 16 '15 at 0:22
  • No I need no colours at all. Is there a less "hacky" solution in this case? – Gaussler Apr 16 '15 at 5:37
  • 1
    @Gaussler Consider literate={~}{\char`\ }}1, where ~ can be replaced by any other character you want, even U+00A0. In case you want a global replacement, checkout showspaces=true,showtabs=true. – Symbol 1 Apr 16 '15 at 5:50
  • 1
    @Gaussler It is known that sometimes \lstdefinestyle and \lstset are not equivalent. This issue had appeared several times on TeX.SX but, if I recall correctly, no one can tell why. Feel free to ask a new question if necessary~~ – Symbol 1 Apr 16 '15 at 9:06

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.