
AcompanhandoLista personalizada de elementos usando etoolbox em particular, a segunda solução: existe uma maneira conveniente de iterar em qualquer uma dessas listas?
Exemplo:
\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}
Dá como mensagem de erro:
! 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 }
(por que foreach? porque esses loops precisam entrar em uma imagem tikz no final)
Obrigado!
Responder1
Só não use \foreach
.
Aqui \listLoop
são necessários dois argumentos: o primeiro é o nome da lista, o segundo é um modelo que #1
representa o ciclo atual e #2
o item.
\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}