# Parallel loop [python-like zip] in Tikz

An easy way to write axis labels in Tikz is:

\foreach \x/\xtext in {-1,-0.5,0.5,1}
\draw[very thin,gray] (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};


Is it possible to have something similar but where \x and \xtext do not get the values from the same list as follows ?

\foreach \x in {-1,-0.5,0.5,1}/\xtext in {-10,-5,5,10}
\draw[very thin,gray] (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};

• Are you talking about this: \x/\xtext in {a/A,b/B,c/C}? Jan 26, 2013 at 18:03
• Does this come close with individual arrays ? tex.stackexchange.com/questions/89135/loop-for-includegraphics/… Jan 26, 2013 at 18:09
• have you thought about using pgfplots? Jan 26, 2013 at 18:37
• yes but here I am using tikz to have more control. Jan 26, 2013 at 20:44

Here a simple code. Is this what you want?

\begin{tikzpicture}
\draw[very thin,gray] (-1,0)--(1,0);
\foreach \x/\xtext in {-1/a_0,-0.5/a_1,0.5/a_2,1/a_3}{
\draw[very thin,gray] (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};
}
\end{tikzpicture}


• yes, it is nice. I have to experiment. Jan 26, 2013 at 20:44
• @pluton I know this is not the core of this question, but in this case, one could also use the count key: \foreach \x[count=\xIndex from 0] and then use $a_{\xIndex}$. Jul 12, 2013 at 16:29

If your lists are from different sources, then you would need to merge them as follows:

\documentclass{article}
\usepackage{tikz}
\makeatletter
% \specialmergetwolists{<coupler>}{<list1>}{<list2>}{<return macro>}
% \specialmergetwolists*{<coupler>}{<listcmd1>}{<listcmd2>}{<return macro>}
\protected\def\specialmergetwolists{%
\begingroup
\@ifstar{\def\cnta{1}\@specialmergetwolists}
{\def\cnta{0}\@specialmergetwolists}%
}
\def\@specialmergetwolists#1#2#3#4{%
\def\tempa##1##2{%
\edef##2{%
\ifnum\cnta=\@ne\else\expandafter\@firstoftwo\fi
\unexpanded\expandafter{##1}%
}%
}%
\tempa{#2}\tempb\tempa{#3}\tempa
\def\cnta{0}\def#4{}%
\foreach \x in \tempb{%
\xdef\cnta{\the\numexpr\cnta+1}%
\gdef\cntb{0}%
\foreach \y in \tempa{%
\xdef\cntb{\the\numexpr\cntb+1}%
\ifnum\cntb=\cnta\relax
\xdef#4{#4\ifx#4\empty\else,\fi\x#1\y}%
\breakforeach
\fi
}%
}%
\endgroup
}
\makeatother

\begin{document}
\specialmergetwolists{/}{-1,-0.5,0.5,1}{a_0,a_1,a_2,a_3}\clist
% If the lists are in macros, use the star (*) form:
% \def\alist{-1,-0.5,0.5,1}
% \def\blist{a_0,a_1,a_2,a_3}
% \specialmergetwolists*{/}\alist\blist\clist

\begin{tikzpicture}
\draw[very thin,gray] (-1,0)--(1,0);
\foreach \x/\y in \clist{
\draw[very thin,gray] (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\y$};
}
\end{tikzpicture}
\end{document}

• Just what I needed! The semantics are like Python's zip: the merged list is truncated to match the shorter of the two input lists: \specialmergetwolists{/}{-1,-0.5,0.5}{a_0,a_1,a_2,a_3}\clist puts -1/a_0,-0.5/a_1,0.5/a_2 in \clist. Jul 11, 2013 at 1:26
• This should become part of tikz-pgf. Aug 30, 2020 at 15:25
• what if one list is a macro and one is a sequence of integers? Apr 4, 2021 at 15:08