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}
1

1 Answer 1

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}
6
  • 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, 2015 at 19:40
  • 1
    @Gaussler Do you mean \char`\\\textcolor{blue}{\char`\ }?
    – Symbol 1
    Apr 16, 2015 at 0:22
  • No I need no colours at all. Is there a less "hacky" solution in this case?
    – Gaussler
    Apr 16, 2015 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, 2015 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, 2015 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .