# Iterate through a list by index

I'm making a TikZ picture in which I have a list of node names and coordinates and I want to go through the lists putting ith node name at the ith coordinate. My MWE:

\documentclass{beamer}
\usepackage{tikz}
\tikzstyle{arrow} = [thick,->]
\tikzstyle{aAllele} = [text opacity=0, circle,text centered, fill=blue, text=white, minimum size = 0.5cm]
\tikzstyle{bAllele} = [text opacity=0, circle, text centered, fill=red, text=white, minimum size = 0.5cm]

\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\def\names{{24,23,33,22,32,11,21,31,41,00,10,20,30,40}}
\def\coords{(2,4),(2,3),(3,3),(2,2),(3,2),(1,1),(2,1),(3,1),(4,1),(0,0),(1,0),(2,0),(3,0),(4,0)}
\foreach \x in {0,1,2,3,4}{%
\foreach \y in {0,1,2,3,4}{%
\fill[red] (\x,\y) circle (0.25cm);%
}
}
\foreach \i in {0,...,14}{%
\node (\names[\i]) [aAllele] at \coords[\i] {};%
}

\end{tikzpicture}
\end{frame}
\end{document}


In this picture, the circles given by coords should be colored blue. I'm using nodes because I'd eventually like to connect them with lines; I've already gotten it to work simply by iterating over the coordinates, however I need them to be named so I can use lines to connect them. Replacing lines 18-20 with the following:

\foreach \coord in \coords{%
\node (\coord) [aAllele] at \coord {};
}


compiles correctly, though isn't the desired result because I can't easily connect nodes by name (I think) because the nodes are given the "name" of the coordinates used to generate them.

Thanks

• At the moment, your document does not even compile – user31729 Sep 17 '17 at 16:13
• That's odd. I got the above screenshot by compiling it. I'll double check it. Update--it compiles for me (with some TikZ-related errors) to produce a pdf of the above figure. If you comment out the offending lines (18-20) it will compile without error, however that is where I'm having issues. – Mark Sep 17 '17 at 16:13
• Ignoring errors is not 'compiling' actually. I think the array definition of \coords is wrong anyway – user31729 Sep 17 '17 at 16:22
• Yeah, I get and generally agree with that. At any rate, I'm trying to figure out the source of the errors. If I just iterate through \coords, it seems to compile correctly, however then I don't get my node names. I edited the question to show this. – Mark Sep 17 '17 at 16:24

I would use nodes for the red circles and name them:

\documentclass{beamer}
\usepackage{tikz}
\tikzstyle{arrow} = [thick,->]
\tikzstyle{aAllele} = [text opacity=0, circle,text centered, fill=blue, text=white, minimum size = 0.5cm]
\tikzstyle{bAllele} = [text opacity=0, circle, text centered, fill=red, text=white, minimum size = 0.5cm]

\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}
\def\names{24,23,33,22,32,11,21,31,41,00,10,20,30,40}
%\def\coords{(2,4),(2,3),(3,3),(2,2),(3,2),(1,1),(2,1),(3,1),(4,1),(0,0),(1,0),(2,0),(3,0),(4,0)}
\foreach \x in {0,1,2,3,4}{%
\foreach \y in {0,1,2,3,4}{%
\node[bAllele](\x\y)at (\x,\y){};%
}
}
\foreach \i in \names{%
\node[aAllele] at (\i) {};%
}
\path[green,thick](22)
edge(23)
edge(32);
\end{tikzpicture}
\end{frame}
\end{document}


Result: