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.
child ... foreach
(TikZ documentation section 21.1 "Introduction to the Child Operation) instead, see tikz pgf - How to expand a macro used in the range of a foreach loop? - TeX - LaTeX Stack Exchange\f{x}
instead of a "single macro" then you must useexpand list
option in the answer below, omitting the braces doesn't work; see also these questions 1 2 3