我尝试嵌套两个序列映射,到目前为止,这种方法是可行的,但我想在命令定义中使用它们,这样应该<items>
可以作为和访问##1
,###1
但这种方法行不通。是否可以在此级别使用嵌套映射?
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\makeatletter
% some variants
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnf , NnV , Nnx }
% sequence to store the higlight key value pair
\seq_new:N \l_@@_highlight_seq
% sequence to store the numbers inside the key
\seq_new:N \l_@@_highlight_pos_seq
\cs_new_protected:Npn \@@_evaluate_higlights:n #1
{
% map the whole argument as clist
\clist_map_inline:nn { #1 }
{
% split a clist item at the equal sign
\seq_set_split:Nnn \l_@@_highlight_seq { = } { ##1 }
% split the first part of the higlight sequence at plus signs
\seq_set_split:Nnf \l_@@_highlight_pos_seq { + }
{
\seq_item:Nn \l_@@_highlight_seq { 1 }
}
\par\bigskip
% map the key numbers to their values
\seq_map_inline:Nn \l_@@_highlight_pos_seq
{
% for this example just some text output
number~###1
{}~has~value~
\seq_item:Nn \l_@@_highlight_seq { 2 }\par
}
}
}
\NewDocumentCommand { \highlights } { m }
{
\@@_evaluate_higlights:n { #1 }
}
\ExplSyntaxOff
\begin{document}
\textbf{Highlights}
\highlights{1=a, 2+3=b}
\bigskip
\textbf{Desired output}
\par\bigskip
number 1 has value a\par
\par\bigskip
number 2 has value b\par
number 3 has value b\par
\end{document}
答案1
我自己明白了。参数的嵌套顺序是#1
,,##1
等等####1
。
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\makeatletter
% some variants
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnf , NnV , Nnx }
% sequence to store the higlight key value pair
\seq_new:N \l_@@_highlight_seq
% sequence to store the numbers inside the key
\seq_new:N \l_@@_highlight_pos_seq
\cs_new_protected:Npn \@@_evaluate_higlights:n #1
{
% map the whole argument as clist
\clist_map_inline:nn { #1 }
{
% split a clist item at the equal sign
\seq_set_split:Nnn \l_@@_highlight_seq { = } { ##1 }
% split the first part of the higlight sequence at plus signs
\seq_set_split:Nnf \l_@@_highlight_pos_seq { + }
{
\seq_item:Nn \l_@@_highlight_seq { 1 }
}
\par\bigskip
% map the key numbers to their values
\seq_map_inline:Nn \l_@@_highlight_pos_seq
{
% for this example just some text output
number~####1
{}~has~value~
\seq_item:Nn \l_@@_highlight_seq { 2 }\par
}
}
}
\NewDocumentCommand { \highlights } { m }
{
\@@_evaluate_higlights:n { #1 }
}
\ExplSyntaxOff
\begin{document}
\textbf{Highlights}
\highlights{1=a, 2+3=b}
\bigskip
\textbf{Desired output}
\par\bigskip
number 1 has value a\par
\par\bigskip
number 2 has value b\par
number 3 has value b\par
\end{document}