I have a small problem with the TikZ \foreach
loop whenever the list, over which we loop, is defined my a macro. The following code example will show my problem:
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
Picture one:
\begin{tikzpicture}
\foreach \x/\y in {1.0/2.0, 3.0/4.0} \node[draw] at (\x,\y) {\x--\y};
\end{tikzpicture}
Picture two:
\newcommand{\mymacro}{1.0/2.0, 3.0/4.0}
\begin{tikzpicture}
\foreach \x/\y in {\mymacro} \node[draw] at (\x,\y) {\x--\y};
\end{tikzpicture}
\end{document}
The expected result is that both images should look the same. But in the second image, I only get one node – and both \x
and \y
are each time expanded to 1.0/2.0, 3.0/4.0
. I hope someone can help.
Best Answer
The
\mymacro
isn't expanded by the\foreach
loop, but only afterwards. You need to remove the braces{ }
around the macro to make it work:Note that while normally both arguments
{\mymacro}
and\mymacro
are identical,\foreach
seems to test for{
and expands the argument if it isn't present.