As the title says, the option breaklines=true
seems to have an undesired interaction when using literate
; in the following example the closing parenthesis doesn't get colorized
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
literate=
{)}{{\textcolor{red}{)}}}{1}
{(}{{\textcolor{red}{(}}}{1},
breaklines=true,
}
\begin{document}
\begin{lstlisting}
()
\end{lstlisting}
\end{document}
Commenting out the breaklines=true
option produces the desired result. What is producing this odd behaviour and how can it be prevented?
Best Answer
When the
breaklines=true
option is given, a code that's executed after doing\lst@literate
(which assigns the special meaning to the defined characters) isand this expands to
which makes the active
)
expand towhere
)
is not active. All this happens when\lsthk@SelectCharTable
is executed.If, just for trying, I apply a patch with
etoolbox
then the right parenthesis will be printed red, but a similar problem will appear with the
]
in case, say,is specified (the right bracket will become a black right parenthesis, which I expected).
I believe this has to do with precautions
listings
makes in order to correctly treat parenthesized expressions. And I suspect that not much can be done about this.If I try to patch the definition of
\lsthk@SelectCharTable
removing that conditional part, then the right parenthesis comes out red. But I don't know if other problems will arise.