我曾多次使用这个模式(在这个网站上找到)来为 Beamer 中的列表制作动画。不幸的是,它不再起作用了。
此代码来自该问题的解决方案: 列表:突出显示以空格开头的分隔词的一部分
\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}
这是错误消息(与 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
您有什么想法可以让这个非常有用的代码再次发挥作用吗?
答案1
失败的原因是l3regex
模块的最新实现在执行替换时使用了字符的当前类别代码;lstlisting
字母 az 的当前类别代码为 13(活动),因此问题随之而来。在“搜索”部分,l3regex
catcode 是不可知的(除非您指定精确的 charcode、catcode 对)。
解决方法很简单:在进行替换时声明您想要其他字符(catcode 12):\cO(orangeb)
例如。此外,冒号也应声明为“其他”。
\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}