4

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:

  1. why?
  2. 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}

enter image description here

5
  • 2
    note that within the listings it is essentially verbatim so \[ 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 \\[ Feb 5, 2017 at 19:07
  • 1
    The update seems to be a separate issue, similar to lstlisting: color LaTeX commands like \\ or \!. Loading of tex aspect partially overrides char table, so you need to "rehook" delimiters char table: \makeatletter \lst@AddToHook{SelectCharTable}{\lst@DefDelims} \makeatother after loading tex aspect (so after using \lstset{ language=[AlLaTeX]TeX})
    – jkuczm
    Feb 7, 2017 at 10:40
  • @jkuczm That does the trick! Could you make this comment an answer in order I can accept it? I'm not sure to understand your explanation neither here nor in tex.stackexchange.com/a/267701/18401 (and I have currently no time to look further into this) but does what you point be considered as a listing bug? Feb 7, 2017 at 20:25
  • 1
    @DenisBitouzé "... does what you point be considered as a listing bug?" Yes, I believe it might be considered a subtle bug in how tex aspect interacts with other listings aspects.
    – jkuczm
    Feb 7, 2017 at 23:10
  • @jkuczm Attention of the listings' maintainer drawn. Feb 8, 2017 at 6:39

2 Answers 2

3

Problem of interaction of texcsstyle=*... option with moredelim option, from updated question, seems to be a separate issue, than missing backslash, from original question, solved already by answer of Schweinebacke.

Issue from update is similar to problem of interaction of texcsstyle=*... option with literate option, described in my answer to "lstlisting: color LaTeX commands like \\ or \!" question.

When tex aspect is loaded, it adds code, to SelectCharTable hook, that can override behavior of backslashes defined using other aspects. To restore proper behavior one can add relevant code to SelectCharTable hook after loading tex aspect. In case of moredelim option relevant code is \lst@DefDelims macro.

Full example of "rehooking" delimiters char table is:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstset{
  language=[AlLaTeX]TeX, %
  basicstyle=\ttfamily,%
  texcsstyle=*\color{green},%
  moredelim=*[s][\color{blue}]{[}{]},%
  moredelim=*[s][\color{red}]{\\[}{\\]},%
}

% "Rehook" delimiters char table.
\makeatletter
\lst@AddToHook{SelectCharTable}{\lst@DefDelims}
\makeatother

\begin{document}

\begin{lstlisting}
\documentclass[fleqn]{article}
\usepackage{amsmath}
\begin{document}
\[x=0\]
\end{document}
\end{lstlisting}

\end{document}

print screen of PDF

4
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
%
\lstset{%
  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}
\end{document}

enter image description here

You need this extra backslash for {, }, #, % and \. See note 5 in section 4.1 of the listings manual.

2
  • Sigh... I was sure I gave a try to this trick :( Feb 5, 2017 at 18:58
  • In fact, this trick doesn't work if the optional star of texcsstyle is also used in order to highlight the backslash in front of the control sequence names. See my edited question (and sorry for the noise). Feb 5, 2017 at 19:12

You must log in to answer this question.

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