我想做以下事情:
\mycmd{foo={a,b,c},baz={1,2,3}}
其中{a,b,c}
和{1,2,3}
是数据数组/序列。
MWE 是这样的:
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\begin{document}
\keys_define:nn { mycmd }
{
\foo .tl_set:N = \l_mycmd_foo_tl,
\baz .tl_set:N = \l_mycmd_baz_tl
}
\NewDocumentCommand\mycmd{m}{
\keys_set:nn { mycmd } { #1 }
% iterate through `\l_mycmd_foo_tl` comma-separated list
% iterate through `\l_mycmd_bar_tl` comma-separated list
}
\mycmd{foo={a,b,c},baz={1,2,3}}
\end{document}
答案1
如果逗号分隔列表是强制性的,则使用foo .clist_set:N
等来设置expl3
列表,然后对它们进行迭代,例如使用\clist_map_inline:Nn
,其中##1
用列表的相关条目替换(假设\clist_map_inline:Nn
在另一个宏中使用)。
\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}
\ExplSyntaxOn
\begin{document}
\keys_define:nn { mycmd }
{
% \foo .tl_set:N = \l_mycmd_foo_tl,
% \baz .tl_set:N = \l_mycmd_baz_tl
foo .clist_set:N = \l_mycmd_foo_clist,
baz .clist_set:N = \l_mycmd_baz_clist
}
\NewDocumentCommand\mycmd{m}{
\keys_set:nn { mycmd } { #1 }
\clist_map_inline:Nn \l_mycmd_foo_clist {
value\space##1\par
}
\clist_map_inline:Nn \l_mycmd_baz_clist {
value\space##1\par
}
% iterate through `\l_mycmd_foo_tl` comma-separated list
% iterate through `\l_mycmd_bar_tl` comma-separated list
}
\mycmd{foo={a,b,c},baz={1,2,3}}
\end{document}