我正在使用属性列表,并且希望能够创建一个命令来生成用于生成 for 循环的键列表。我目前使用 \prop_map_inline 来生成所有键,但如果我在 for 循环中使用它,它会被解释为单个项目,而不是多个项目的集合。
这是一个示例代码:
\documentclass{article}
\usepackage{ifthen}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_persons_prop
% Create a new person
\NewDocumentCommand{\CreatePerson}{m} {
\prop_gput:Nnn \g_persons_prop { #1 }
}
% Gets all persons
\NewDocumentCommand{\GetAllPersonKeys}{}{
\prop_map_inline:Nn \g_persons_prop {
##1,
}
}
\ExplSyntaxOff
\CreatePerson{jd}{John Doe}
\CreatePerson{ad}{Albert Dull}
\CreatePerson{bu}{Ben Under}
\begin{document}
I would like to have this:
\makeatletter
\begin{itemize}
\@for\sun:={jd, ad, bu}\do{\item We have person: \sun. }
\end{itemize}
\makeatother
But I get this instead:
\makeatletter
\begin{itemize}
\@for\sun:={\GetAllPersonKeys}\do{\item We have person: \sun. }
\end{itemize}
\makeatother
\end{document}
答案1
您也可以映射 expl3 中的键:
\documentclass{article}
\usepackage{ifthen}
\usepackage{xparse}
\ExplSyntaxOn
\prop_new:N \g_persons_prop
% Create a new person
\NewDocumentCommand{\CreatePerson}{m} {
\prop_gput:Nnn \g_persons_prop { #1 }
}
\NewDocumentCommand{\MapAllPersonKeys}{m}{
\prop_map_inline:Nn \g_persons_prop {
#1
}
}
\ExplSyntaxOff
\CreatePerson{jd}{John Doe}
\CreatePerson{ad}{Albert Dull}
\CreatePerson{bu}{Ben Under}
\begin{document}
I would like to have this:
\makeatletter
\begin{itemize}
\@for\sun:={jd, ad, bu}\do{\item We have person: \sun. }
\end{itemize}
\makeatother
\begin{itemize}
\MapAllPersonKeys{\item we have person: #1!}
\end{itemize}
\end{document}