# parallel iteration over elements of separate lists

I guess this is a simple problem, but I don't know how to do it in TeX/LaTeX/tikz. What I'd like to do is to iterate over several lists in parallel, and do something with the respective values. One of the lists should be passed as argument to a command.

\documentclass[xcolor=pdftex]{article} %

\usepackage{pgffor}

\newcommand{\test}[1]{%
\def\x{{1,2,3,4,5,6}}%
\foreach \i in {0,...,5}{%
% in loop use something like \x[\i], #1[\i] etc
}
}

\begin{document}
\test{{a,b,c,d,e,f}}
\end{document}

• Can you explain a bit more about hat you are trying to accomplish? You define \x as a set of numbers, and then use syntax such as \x[\i] which I don't understand what you intend it to do. – Steven B. Segletes Feb 18 at 21:40
• @StevenB.Segletes: I belive that \x[\i] is intended to use \i as an index in to the \x list. So the OP wants to use 0 and \x[0] (ie, 0 and 1), and then in the next loop you have \i=1, so the OP wants 1 and \x[1] (ie, 1 and 2). – Peter Grill Feb 18 at 21:44

\documentclass[xcolor=pdftex]{article} %

\usepackage{pgffor,listofitems}

\newcommand{\test}[1]{%
\foreach \i in {1,...,6}{%
Element \i{} of x is \x[\i]
and of \#1, it is \tmp[\i]\par
}
}

\begin{document}
\test{a,b,c,d,e,f}
\end{document}


With foreach, you can iterate on several variables at the same time. Here is the example given in the manual on page 1002.

\documentclass[border=5mm,tikz]{standalone}
\begin{document}

\begin{tikzpicture}[line cap=round,line width=3pt]
\filldraw [fill=yellow!80!black] (0,0) circle (2cm);
\foreach \angle / \label in
{0/3, 30/2, 60/1, 90/12, 120/11, 150/10, 180/9,
210/8, 240/7, 270/6, 300/5, 330/4}
{
\draw[line width=1pt] (\angle:1.8cm) -- (\angle:2cm);
\draw (\angle:1.4cm) node{\textsf{\label}};
}

\end{tikzpicture}%
\end{document}

• Thanks, the problem in my case is that the lists are separate. – user52366 Feb 19 at 5:48

Your approach works. You only need to enclose strings by ".

\documentclass[xcolor=pdftex]{article} %

\usepackage{pgffor}

\newcommand{\test}[1]{%
\def\x{{1,2,3,4,5,6}}%
\foreach \i in {0,...,5}{%
\pgfmathsetmacro{\entrynum}{\x[\i]}%
\pgfmathsetmacro{\entry}{#1[\i]}%
entry \entrynum\space of the argument of \texttt{\textbackslash test} is \entry\par
% in loop use something like \x[\i], #1[\i] etc
}
}

\begin{document}
\test{{"a","b","c","d","e","f"}}
\end{document}


Of course, you can define a helper that wraps the quotes around the entries.

\documentclass[xcolor=pdftex]{article} %

\usepackage{pgffor}
\makeatletter
\newcommand{\WrapQuotes}[2]{%
\def\pgf@temputila{0}%
\pgfutil@for\tikz@temp:=#1\do{%
\ifnum\pgf@temputila=1\relax
\edef#2{#2,"\tikz@temp"}%
\else
\edef#2{"\tikz@temp"}%
\def\pgf@temputila{1}%
\fi%
}}%
\makeatother
\newcommand{\test}[1]{%
\def\x{1,2,3,4,5,6}%
\WrapQuotes{#1}{\tmplst}%
\foreach \i in {0,...,5}{%
\pgfmathsetmacro{\entrynum}{{\x}[\i]}%
\pgfmathsetmacro{\entry}{{\tmplst}[\i]}%
entry \entrynum\space of the argument of \texttt{\textbackslash test} is \entry\par
% in loop use something like \x[\i], #1[\i] etc
}
}

\begin{document}
\test{{a,b,c,d,e,f}}
\end{document}


It is clear that all the actual applications shown here are not to exciting. I interpret the question as a question asking how to access the ith element of a list of general things.

• I would have accepted this answer as well if that was possible. BTW, in the end I switched to perltex because to code got too complicated (at least for my latex skills). – user52366 Feb 23 at 19:53