I'm not so new to TikZ but am now trying something a little more advanced and am puzzled by the following problem. From researching solutions, I see that lots of good answers with good explanations are found on this site!
I'd like to iterate with \foreach
over multiple variables simultaneously and \remember
one of them to position nodes next to each other. It doesn't work as I had expected, so I narrowed it down to how the list of values is presented to \foreach
— it works for 1 variable if I give a range definition with ...
and only 1 item at the beginning and end of list (1st case below). However, it doesn't work if I spell the list items out (2nd case below). I think I need to spell out the list items as I actually want to combine more than one variable to specify different shapes etc. of each node (see 3rd case below).
Here is my MWE:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
all rectangles next to each other:
\begin{tikzpicture}[every node/.style={draw}]
\node (A) [rectangle] {};
\foreach \x [remember=\x as \lastx (initially A)] in {B,...,E} {
\node (\x) [rectangle,right=of \lastx] {};
}
\end{tikzpicture}
2nd and further rectangles on top of each other:
\begin{tikzpicture}[every node/.style={draw}]
\node (A) [rectangle] {};
\foreach \x [remember=\x as \lastx (initially A)] in {B,C,D,E} {
\node (\x) [rectangle,right=of \lastx] {};
}
\end{tikzpicture}
goal: iterate over 2 variables at the same time:
\begin{tikzpicture}[every node/.style={draw}]
\node (A) [rectangle] {};
\foreach \x/\y [remember=\x as \lastx (initially A)] in {B/circle,C/rectangle,D/circle,E/rectangle} {
\node (\x) [\y,right=of \lastx] {};
}
\end{tikzpicture}
\end{document}
There is a related question (related question) but the solution uses 2 nested \foreach
loops, where you can, of course, use the ...
notation. However, I don't think I can use the ...
notation in my case.
I'm using PGF/TikZ 2.10 (MacTeX 2011).
Best Answer
Apparently
\lastx
is not evaluated correctly, it is always A. Unfortunately, I also don't know why, but here are some suggestions: