[Tex/LaTex] Iterate with `\foreach` over multiple variables and ‘\remember’ one of them does not work as expected

foreachtikz-pgf

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:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}

\verb! apparantly `\lastx` is not increased: !

\begin{tikzpicture}[every node/.style={draw}]
\node (A) at (1,0) [rectangle] {A};
\foreach \x/\y [remember=\x as \lastx (initially A)] in {B/2,C/3,D/4,E/5}
{   \node[rectangle] (\x) at (\y,0) {\lastx \x};
}
\end{tikzpicture}

\verb! you could use `\xdef` to do it yourself !

\begin{tikzpicture}[every node/.style={draw}]
    \node (A) [rectangle] {};
    \xdef\lastx{A}
  \foreach \x in {B,C,D,E}
  { \node (\x) [rectangle,right=of \lastx] {};
    \xdef\lastx{\x}
  }
\end{tikzpicture}

\verb! or you could use absolute positions: !

\begin{tikzpicture}[every node/.style={draw}]
    \pgfmathsetmacro{\multi}{1.245} 
  \foreach \x in {1,...,5}
  { \node at (\multi*\x,0) {};
  }
\end{tikzpicture}

\verb! this also works for multiple variables !

\begin{tikzpicture}[every node/.style={draw}]
    \pgfmathsetmacro{\multi}{1.245} 
  \foreach \x/\shp/\clr in {1/rectangle/green,2/circle/blue,3/diamond/red,4/ellipse/yellow,5/trapezium/orange}
  { \node[shape=\shp,fill=\clr] at (\multi*\x,0) {};
  }
\end{tikzpicture}

\end{document}

enter image description here