# Control Structures in TikZ

I'm just now learning TikZ and I'm experimenting with some control structures. Could somebody possibly help me with my code below?

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}
\draw[fill=black] (0,0)circle(0.05) node[above=1.5]{You};
\foreach $$\x,\y) in {(-2,green),(-1,red),(0,black),(1,blue),(2,yellow)} { \filldraw[draw=\y] (0,0)--(\x,-1); \draw[fill=black] (-\x,-1)circle(0.05); } \end{tikzpicture} \end{center} \end{document}  I know that the problem is coming from where I try to use \foreach \(\x,\y) in {(-2,green),(-1,red),(0,black),(1,blue),(2,yellow)}  Is there any way TikZ could handle something like this? - ## 2 Answers Yep, there’s a way to do this. From section 83/page 911 of the PGF/TikZ Manual (v3.0.0): Multiple variables. You will often wish to iterate over two variables at the same time. Since you can nest \foreach loops, this is normally straight-forward. However, you sometimes wish variables to iterate “simultaneously.” For example, we might be given a list of edges that connect two coordinates and might wish to iterate over these edges. While doing so, we would like the source and target of the edges to be set to two different variables. To achieve this, you can use the following syntax: The ⟨variables⟩ may not only be a single TEX-variable. Instead, it can also be a list of variables separated by slashes (/). In this case the list items can also be lists of values separated by slashes. Assuming that the ⟨variables⟩ and the list items are lists of values, each time the ⟨commands⟩ are executed, each of the variables in ⟨variables⟩ is set to one part of the list making up the current list item. The PGF/TikZ documentation is ~very detailed, and that whole section walks you through using \foreach loops very well. I wouldn’t recommend reading from start-to-finish when you’re first learning (the full document is >1000 pages), but it’s very good if you want to learn about something new in great detail. So you should replace the line \foreach \(\x,\y) in {(-2,green),(-1,red),(0,black),(1,blue),(2,yellow)}  with \foreach \x/\col in {-2/green, -1/red, 0/black, 1/blue, 2/yellow}  I’d suggest renaming the second variable to \col or \color/\colour because it’s more descriptive. You may find yourself looping over (x,y) coordinate pairs fairly regularly, so you don’t want to get confused. This is what your example then looks like: \documentclass{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \draw [fill=black] (0,0) circle (0.05) node [above=1.5] {You}; \foreach \x/\col in {-2/green, -1/red, 0/black, 1/blue, 2/yellow} { \filldraw [draw=\col] (0, 0) -- (\x, -1); \draw [fill=black] (-\x, -1) circle (0.05); } \end{tikzpicture} \end{document}  and here’s what it compiles as: - I really like this explanation. But I notice a problem in both of our compiles. I was able to fix the first dot (where the lines are placed over it) by placing the code for it at the end of the tikzpicture environment. But what about where the yellow line is overlapping the black dot in the second row? I wouldn't expect that to happen. – Higgie2718 Mar 14 '14 at 23:15 @Higgie2718 It comes from (-\x, -1) circle (0.05), but the first line has (\x, -1). If you swap the signs to (\x, -1) circle (0.05), then the circles get drawn with the circles, and the yellow/black match. – alexwlchan Mar 14 '14 at 23:17 Thank you! I didn't catch that when I looked over my code. – Higgie2718 Mar 14 '14 at 23:19 Well even if it supported Python like loops, \( would still cause problems since it is the inline math environment opening for example \(\alpha$$ is $\alpha$.

But it doesn't.

Hence you need to use

\foreach \x/\y in {-2/green,-1/red,0/black,1/blue,2/yellow}

-
Thank you! This works great! And would this same method extend to more than two values (ex: -2/green/thick)? – Higgie2718 Mar 14 '14 at 23:09
@Higgie2718 Yes. – Paul Gaborit Mar 14 '14 at 23:10
Thank you for all of your help! – Higgie2718 Mar 14 '14 at 23:11