![为什么无法格式化 LaTeX3 逗号列表的项目?](https://linux22.com/image/321866/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%97%A0%E6%B3%95%E6%A0%BC%E5%BC%8F%E5%8C%96%20LaTeX3%20%E9%80%97%E5%8F%B7%E5%88%97%E8%A1%A8%E7%9A%84%E9%A1%B9%E7%9B%AE%EF%BC%9F.png)
尝试适应第一个代码这个答案为了格式化(以粗体表示)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
,在写答案时可能无法获得。