
跟進使用 etoolbox 自訂元素列表 特別是第二個解決方案:是否有一種方便的方法來迭代這些列表中的任何一個?
例子:
\documentclass{article}
\usepackage{tikz}
\ExplSyntaxOn
\NewDocumentCommand{\newList}{m}
{
\seq_new:c { l_kees_list_#1_seq }
}
\NewDocumentCommand{\addToList}{mm}
{
\seq_put_right:cn { l_kees_list_#1_seq } { #2 }
}
\NewDocumentCommand{\getFromList}{mm}
{
\seq_item:cn { l_kees_list_#1_seq } { #2 }
}
\NewDocumentCommand{\getListLength}{m}
{
\seq_count:c { l_kees_list_#1_seq }
}
\NewDocumentCommand{\getList}{m}
{
\seq_use:cn { l_kees_list_#1_seq } { , }
}
\ExplSyntaxOff
\begin{document}
\newList{listA}
\newList{listB}
\addToList{listA}{one}
\addToList{listA}{two}
\addToList{listA}{three}
\addToList{listB}{oneinB}
\addToList{listB}{anoterinB}
% this works, but is cumbersome:
\foreach \n in {1,...,\getListLength{listA}} {n: \n, \getFromList{listA}{\n} }
\foreach \n in {1,...,\getListLength{listB}} {n: \n, \getFromList{listB}{\n} }
% not sure why this does not expand properly?
\foreach \i in {\getList{ListA}} {i: \i }
\end{document}
它給出錯誤訊息:
! Use of \??? doesn't match its definition.
<argument> \???
! LaTeX Error: Erroneous variable \l_kees_list_ListA_seq used!
l.46 \foreach \i in {\getList{ListA}} {i: \i }
(為什麼是foreach?因為這些循環最終需要進入tikzpicture)
謝謝!
答案1
只是不要使用\foreach
。
這裡\listLoop
有兩個參數:第一個是清單名稱,第二個是模板,其中#1
代表當前週期和#2
項目。
\documentclass{article}
\usepackage{tikz}
\ExplSyntaxOn
%%% Lists
\NewDocumentCommand{\newList}{m}
{
\seq_new:c { l_kees_list_#1_seq }
}
\NewDocumentCommand{\addToList}{mm}
{
\seq_put_right:cn { l_kees_list_#1_seq } { #2 }
}
\NewDocumentCommand{\getFromList}{mm}
{
\seq_item:cn { l_kees_list_#1_seq } { #2 }
}
\NewDocumentCommand{\getListLength}{m}
{
\seq_count:c { l_kees_list_#1_seq }
}
\NewDocumentCommand{\getList}{m}
{
\seq_use:cn { l_kees_list_#1_seq } { , }
}
%%% Loops
\NewDocumentCommand{\loopList}{m +m}
{
\seq_map_indexed_inline:cn { l_kees_list_#1_seq } { #2 }
}
\cs_generate_variant:Nn \seq_map_indexed_inline:Nn { c }
\ExplSyntaxOff
\begin{document}
\newList{listA}
\newList{listB}
\addToList{listA}{one}
\addToList{listA}{two}
\addToList{listA}{three}
\addToList{listB}{oneinB}
\addToList{listB}{anoterinB}
% this works, but is cumbersome:
\foreach \n in {1,...,\getListLength{listA}} {n: \n, \getFromList{listA}{\n} }
%\foreach \n in {1,...,\getListLength{listB}} {n: \n, \getFromList{listB}{\n} }
% this is better
\loopList{listA}{n: #1, #2 }
\end{document}