尝试适应第一个代码这个答案为了格式化(以粗体表示)LaTeX3逗号列表的项目,我偶然发现了一个意外的错误:以下MWE 非常有效:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\bolditems}{ m }
{
\bold_items:n {#1}
}
\cs_new_protected:Npn \bold_items:n #1
{
% does what the name suggests, set a sequence from the clist
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
% applies final arg to each element of second seq and stores result in first seq
\seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq {
##1
% \textbf{##1}
}
% \seq_use puts the items from the seq back in the input with "+" as a separator
\seq_use:Nnnn \l_tmpb_seq {,~}{,~}{,~}
}
\ExplSyntaxOff
\begin{document}
\bolditems{}
\bolditems{foo}
\bolditems{foo,bar}
\bolditems{foo,bar,baz}
\end{document}
但是,如果##1
被替换\textbf{##1}
,则会失败并出现错误:
! Argument of \reserved@a has an extra }.
<inserted text>
\par
l.26 \bolditems{foo}
?
为什么?
答案1
传递的物品\seq_set_map:NNn
会完全展开。在链接的答案中显示的应用程序中,它并不真正相关,但它在你的应用程序中,因为\textbf
,可能还有物品,无法完全展开。只需使用\exp_not:n
:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\bolditems}{ m }
{
\bold_items:n {#1}
}
\cs_new_protected:Npn \bold_items:n #1
{
% does what the name suggests, set a sequence from the clist
\seq_set_from_clist:Nn \l_tmpa_seq {#1}
% applies final arg to each element of second seq and stores result in first seq
\seq_set_map:NNn \l_tmpb_seq \l_tmpa_seq { \exp_not:n { \textbf { ##1 } } }
% \seq_use puts the items from the seq back in the input with "+" as a separator
\seq_use:Nn \l_tmpb_seq {,~}
}
\ExplSyntaxOff
\begin{document}
X\bolditems{}X
X\bolditems{foo}X
X\bolditems{foo,bar}X
X\bolditems{foo,bar,baz}X
\end{document}
请注意\seq_use:Nn
,在写答案时可能无法获得。