The following MCE points out that, if used as listings
' delimiters:
[
and]
(opening and closing square brackets),\[
and\]
(the same ones preceded with backslash),
cannot be distinguished (the order of the directives doesn't matter).
Update
As pointed out by Schweinebacke, it is enough to escape the backslash with a backslash. But this trick does not work if the optional star of texcsstyle
is also used in order to highlight the backslash in front of the control sequence
names. See the updated example.
Do you know:
- why?
- how make them distinguishable?
Subsidiary (but maybe related) question: how to color the whole \[
delimiter (currently, only [
is colored)?
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
%
\lstset{
language=[AlLaTeX]TeX, %
basicstyle=\ttfamily,%
moredelim=*[s][\color{blue}]{[}{]},%
moredelim=*[s][\color{red}]{\\[}{\\]},%
}
%
\begin{document}
\begin{lstlisting}
\documentclass[fleqn]{article}
\usepackage{amsmath}
\begin{document}
\[x=0\]
\end{document}
\end{lstlisting}
\lstset{texcsstyle=*\color{green}}
\begin{lstlisting}
\documentclass[fleqn]{article}
\usepackage{amsmath}
\begin{document}
\[x=0\]
\end{document}
\end{lstlisting}
\end{document}
\[
is two tokens\
and[
but in the setup tokens are read as normal so\[
is one token (and doesn't match) as the posted answer shows listings lets you use\\
to refer to the\
token as it appears in the listings body, hence\\[
tex
aspect partially overrides char table, so you need to "rehook" delimiters char table:\makeatletter \lst@AddToHook{SelectCharTable}{\lst@DefDelims} \makeatother
after loadingtex
aspect (so after using\lstset{ language=[AlLaTeX]TeX}
)listing
bug?tex
aspect interacts with otherlistings
aspects.listings
' maintainer drawn.