我正在使用 LaTeX3 技术cfr 显示于此处\color
在某些段落前面添加命令。在段落完成后par_end_hook
,我添加了\color{black}
重置颜色。对于普通文本,它都可以正常工作,但在列表中使用时,它会为列表索引着色下一个项目也是如此,尽管它之前的文本“EndOfParHook”显示为黑色(应该是黑色)。
这是为什么?
附言:我并不想给列表索引着色。对我来说,如果 2.、3. 和 6. 只是黑色就好了。
这是生成如上所示图像的代码:
\documentclass{article}
\usepackage{xcolor,l3galley,xparse}
\ExplSyntaxOn
\tl_gput_right:Nn \g_galley_par_begin_hook_tl
{
\color{\NextPartColor}
\gdef\NextPartColor{black}
}
\tl_gput_left:Nn \g_galley_par_end_hook_tl
{
\color{black}EndOfParHook
}
\ExplSyntaxOff
\def\NextPartColor{black}
\def\todoPar{\gdef\NextPartColor{red!70!black}}
\def\donePar{\gdef\NextPartColor{green!70!black}}
\begin{document}
\todoPar This works fine for normal paragraphs.
Everything black here, as it should.
\begin{enumerate}
\item\todoPar Some todo item
\item\donePar This one is green, but why is the 2 red?
\item Nothing to do here, but the 3 is green!
\item Another black one
\todoPar\item Doesn't matter if we place it before or after the \verb|\item|.
\item Last one
\end{enumerate}
\end{document}
答案1
因此,问题在于的内容\g_galley_par_end_hook_tl
被放在一个组中。这可以通过在普通段落(或项目)中添加和来轻松检查\the\currentgrouplevel
。\g_galley_par_end_hook_tl
遗憾的是,在 LaTeX 中使颜色更改全局化(如脱离组)非常混乱且依赖于驱动程序。请参阅我怎样才能改变文本颜色以使其效果超越群组?了解血腥细节。
不过,我设法通过修改\item
宏来明确将颜色设置为解决了这个问题\NextParColor
。这样,我们甚至不需要\g_galley_par_end_hook_tl
,因为下一个段落的颜色已经在当前段落的开头重置为黑色。
作为奖励,这还允许我们为列表编号本身着色:
\documentclass{article}
\usepackage{xcolor,l3galley,xparse}
\ExplSyntaxOn
\tl_gput_right:Nn \g_galley_par_begin_hook_tl
{
\color{\NextPartColor}
\gdef\NextPartColor{black}
}
\ExplSyntaxOff
\def\NextPartColor{black}
\def\todoPar{\gdef\NextPartColor{red!70!black}}
\def\donePar{\gdef\NextPartColor{green!70!black}}
%% Hack: Fix wrong-color item labels
\let\OldItem=\item
\def\item{\color{\NextPartColor}\OldItem}
\begin{document}
\todoPar This works fine for normal paragraphs.
Everything black here, as it should.
\begin{enumerate}
\item\todoPar Some todo item
\item\donePar This one is green, but why is the 2 red?
\item This whole line is black. Yay.
\todoPar\item Before \verb|\item|, it also colors the number.
\item Last one, also black.
\end{enumerate}
\end{document}