3

I have used this pattern (found on this site) many times to animate listings in beamer. Unfortunately, it does not work anymore.

This code comes from a solution to this issue : listings: highlight part of a delimited word starting with space

\documentclass{article}
\usepackage{listings,xparse}
\usepackage{expl3}
\usepackage{xcolor}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\colorlet{orangeb}{orange!80!black}
\def\noprint#1{}

\ExplSyntaxOn \NewDocumentCommand \namespaces { } { \tl_set:No \l_demo_tl {\the\use:c{lst@token}} \regex_replace_all:nnN { ([a-zA-Z]):([a-zA-Z]) } { \c{textcolor}\cB{ orangeb \cE}\cB{ \1 \cE}:\c{textcolor}\cB{ darkblue \cE}\cB{ \2 \cE} } \l_demo_tl \tl_use:N \l_demo_tl \noprint }

\ExplSyntaxOff

\lstset{ basicstyle=\ttfamily, alsoletter={:}, columns=fullflexible, identifierstyle=\namespaces }

\begin{document}

\begin{lstlisting} a:b c:d e:f . \end{lstlisting}

\end{document}

Here is the error message (same with pdflatex, latex, xelatex):

 restricted \write18 enabled.
entering extended mode
(./test3.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-04-10>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/latex/listings/listings.sty
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-dist/tex/latex/listings/listings.cfg))
(/usr/share/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/share/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-dvips.def)))
(/usr/share/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/share/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/usr/share/texmf-dist/tex/latex/graphics-def/dvips.def)) (./test3.aux))
! Incomplete \iffalse; all text was ignored after line 30.
<inserted text>
                \fi

Do you have an idea to make this very useful code functional again ?

Julien
  • 31
  • Side note, you could have debugged this by printing out \l_demo_tl with \tl_analysis_show:N (or if it's too verbose I recommend using my package) – user202729 Jul 15 '22 at 11:49
  • @frougon It does? You'll see that the letters "orangeb" are active so would deduce that they would (do what active tokens do instead of typeset the letters themselves) when executed. – user202729 Jul 15 '22 at 13:51

1 Answers1

3

The failure is due to the fact that recent implementations of the l3regex module use the current category code for characters when performing the replacement; inside lstlisting the current category code for the letters a-z is 13 (active), so the problem ensues. In the “search” part, l3regex is catcode agnostic (unless you specify a precise charcode,catcode pair).

The fix is simple: declare you want other characters (catcode 12) when doing the replacement: \cO(orangeb), for instance. Also the colon should be declared “other”.

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

\definecolor{darkblue}{rgb}{0.0,0.0,0.6} \colorlet{orangeb}{orange!80!black}

\ExplSyntaxOn

\NewDocumentCommand \namespaces { m } { \tl_set:Nx \l_tmpa_tl {\the\use:c{lst@token}} \regex_replace_all:nnN { ([a-zA-Z]):([a-zA-Z]) } % any run of letters with a colon in between { \c{textcolor}\cB{ \cO(orangeb) \cE}\cB{ \1 \cE} % first part orange \cO: \c{textcolor}\cB{ \cO(darkblue) \cE}\cB{ \2 \cE} % second part blue } \l_tmpa_tl \tl_use:N \l_tmpa_tl }

\ExplSyntaxOff

\lstset{ basicstyle=\ttfamily, alsoletter={:}, columns=fullflexible, identifierstyle=\namespaces }

\begin{document}

\begin{lstlisting} a:b c:d e:f . \end{lstlisting}

\end{document}

enter image description here

egreg
  • 1,121,712