# Strange behaviour of foreach

I am totally dumbfounded.

Consider the following code:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\foreach \a / \b in {
(20:1) / (90:1)
,(-20:1) / (-90:2)
}
{
\node at \a {$\a$};
\node at \b {$\b$};
%\draw \a to \b;
}
\end{tikzpicture}

\end{document}


If you run it "as is" it works as expected by placing the coordinate tag at the correct place.

However, if you uncomment the \draw line then it produces the error: "Package tikz Error: (, +, coordinate, pic or node expected." as if \draw would not understand the coordinates.

I have tried different spacings, surrounding coordinates by braces and so on, to no avail.

## 2 Answers

Ulrich has already provided you with a nice fix that works for straight connections. The question is what goes wrong. The issue is that the parser wants to see an explicit ( (or an option etc.) but not a macro that expands to something starting with a ( after a to. So one way to fix this here is to expand the macros first and then "activate" the \draw command.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\foreach \a / \b in {
(20:1) / (90:1)
,(-20:1) / (-90:2)
}
{
\node at \a {$\a$};
\node at \b {$\b$};
\edef\temp{\noexpand\draw \a to \b;}
\temp
}
\end{tikzpicture}

\end{document}


Of course, there are many variations possible, e.g.

 \edef\temp{ to \b}
\draw \a \temp;


or a more TikZy version

 \draw [insert path/.expanded={\a to \b}];


Alternatively, you could give TikZ the explicit parentheses.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\foreach \a/\b in {
20:1/90:1,%
-20:1/-90:2%
}
{
\node at (\a) {$(\a)$};
\node at (\b) {$(\b)$};
\draw (\a) to (\b);
}
\end{tikzpicture}

\end{document}


Anyway, the connections become much nicer if you name the nodes, in which case the problem does not arise.

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

\foreach \a / \b in {
(20:1) / (90:1)
,(-20:1) / (-90:2)
}
{
\node (a) at \a {$\a$};
\node (b) at \b {$\b$};
\draw (a) to (b);
}
\end{tikzpicture}

\end{document}


• It was Ulrich and not me who commented. Apr 26, 2020 at 9:01
• @UlrikeFischer Thanks, fixed.
– user194703
Apr 26, 2020 at 9:02

When I write \draw \a -- \b; instead of \draw \a to \b;, then everything seems to work out.

When I write \draw \a \expandafter t\expandafter o\b; instead of \draw \a to \b;, then everything seems to work out, too.

Seems after to expandable tokens like \b don't get expanded before evaluating the composition of to's operands.

I can offer a macro \SecondArgumentsFirstTokenTopLevelExpanded which can be used for having \b toplevel-expanded before the to-operator is encountered:

\documentclass{standalone}

\newcommand\exchange[2]{#2#1}%
\newcommand\SecondArgumentsFirstTokenTopLevelExpanded[2]{%
\expandafter\exchange\expandafter{#2}{#1}%
}%

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \a / \b in {(20:1)/(90:1),(-20:1)/(-90:2)}{
\node at \a {$\a$};
\node at \b {$\b$};
\draw \a \SecondArgumentsFirstTokenTopLevelExpanded{to}{\b};
}
\end{tikzpicture}

\end{document}