Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 25 at 16:23

1 Answer 1

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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