Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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).

share|improve this question
    
Welcome to TeX.sx! You don't have to sign with your name since it automatically appears in the lower right corner of your post. I also embedded the image into your post. Our way of "thanking" is via up-votes, rather than including "Thanks" in our post signatures. That's just how this community rolls... –  Werner Feb 29 '12 at 22:18
4  
The result is correct with pgf 2.1 cvs –  Alain Matthes Feb 29 '12 at 22:39
    
@Werner thanks for the quick intro as this was indeed my first post. I tried to digest the FAQ on posting but didn't read it thoroughly. Whatever makes this community "roll," it's working! –  Linda Mar 1 '12 at 13:53
add comment

1 Answer 1

up vote 4 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.