8

I want to use showexpl and have the LaTeX code printed with colored braces. I can do this within a lstlisting environment by adding a literate option like in the example below, but not within a LTXexample environment (it produces an error). Is there a way to have the braces colored in the code block of a LTXexample environment?

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

\begin{document}
\begin{LTXexample}
\textit{Test}
\end{LTXexample}

\begin{lstlisting}[
    literate=
        *{\{}{{\textcolor{blue}{\{}}}{1}
        {\}}{{\textcolor{blue}{\}}}}{1}
]
\textit{Test}
\end{lstlisting}
\end{document}

example of colored braces

1

1 Answer 1

8

Because showexpl is based on the listings package, most of the latter's features, including the literate key should, in theory, work out of the box. For some reason, though, defining the literal replacements globally, like so

\lstset
{
  literate=
    *{\{}{{\textcolor{blue}{\{}}}{1}
     {\}}{{\textcolor{blue}{\}}}}{1},
}

seems to break showexpl, which complains about an Undefined control sequence \textitTest. I don't know the innards of showexpl well enough to have a good explanation for that error, but it seems that the braces get dropped when they shouldn't. It looks like a bug in showexpl to me, especially because the the inception of the literate key in listings (1999) predates the first release of showexpl (2004).

One workaround is to define those literate replacements, not globally, but within a custom style; you shouldn't get any error, then.

enter image description here

\documentclass{article}

\usepackage{xcolor}
\usepackage{showexpl}

%% returns an error!
%\lstset
%{
%  literate=
%    *{\{}{{\textcolor{blue}{\{}}}{1}
%     {\}}{{\textcolor{blue}{\}}}}{1},
%}

\lstdefinestyle{myLaTeX}
{
  language=[LaTeX]Tex,
  texcsstyle=*\color{red},
  literate=
    *{\{}{{\textcolor{blue}{\{}}}{1}
     {\}}{{\textcolor{blue}{\}}}}{1},
}

\begin{document}

\begin{LTXexample}[style=myLaTeX]
\textit{Test}
\end{LTXexample}

\begin{lstlisting}[style=myLaTeX]
\textit{Test}
\end{lstlisting}

\end{document}

You must log in to answer this question.

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