parallel iteration over elements of separate lists
\documentclass[xcolor=pdftex]{article} %
\usepackage{pgffor,listofitems}
\newcommand{\test}[1]{%
\readlist\tmp{#1}%
\readlist\x{1,2,3,4,5,6}%
\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}
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 i
th element of a list of general things.