
Seguimiento deLista personalizada de elementos usando etoolbox en particular, la segunda solución: ¿Existe una forma conveniente de iterar sobre alguna de esas listas?
Ejemplo:
\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}
Da como mensaje de error:
! 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 qué foreach? Porque estos bucles al final deben ir a una imagen tikz)
¡Gracias!
Respuesta1
Simplemente no lo uses \foreach
.
Aquí \listLoop
se toman dos argumentos: el primero es el nombre de la lista, el segundo una plantilla que #1
representa el ciclo actual y #2
el artículo.
\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}