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.

Is it possible to access an array and use one of its elements to specify the last iteration point in a TikZ \foreach?

I've tried with the code below but I keep getting a compilation error:

! Argument of \pgffor@@dotscharcheck has an extra }.
<inserted text>
                \par
l.49   }

And here's a minimal example:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}

\begin{document}

\begingroup
\newdimen\bitSize
\bitSize=2mm
\newdimen\bitSep
\bitSep=0.5mm
\newdimen\bitBigSep
\bitBigSep=3mm
\begin{tikzpicture}[%
    auto,
    every node/.style={%
      node distance=0pt,
    },
    bit/.style={%
      draw,
      rectangle,
      minimum size=\bitSize,
      inner sep=0pt,
      node distance=\bitSep,
    },
  ]

  \def\numBits{{4, 8, 4}}
  \foreach \i in {1, 2, 3} {%
    \pgfmathtruncatemacro\prevI{\i-1}
    \foreach \j in {1, ..., \numBits[\prevI]} {%
      \pgfmathtruncatemacro\prevJ{\j-1}

      % Draw bit
      \ifnum \j=1
        \ifnum \i=1
          \node [bit] (bit\i-\j) {};
        \else
          \node [bit, right=\bitBigSep of bit\prevI-\numBitsArray[\prevI-1]]
            (bit\i-\j) {};
        \fi
      \else
        \node [bit, right=of bit\i-\prevJ] (bit\i-\j) {};
      \fi
    }
  }
\end{tikzpicture}
\endgroup

\end{document}
share|improve this question
    
In the pgf manual (v.3.10) page 578, an example is given similar to what you have, implicitly, given. \foreach \x [remember=\x as \lastx (initially A)] in {B,...,H}{$\overrightarrow{\lastx\x}$, }. It remembers the previous iteration argument as \lastx. It would be much easier if you can provide an example showing what the goal is in your nested iteration. –  percusse Sep 30 '11 at 11:49
    
Wait what? There is a PGF manual 3.10 now?! And I haven't even had time to read my printed 2.10 yet... Anyway, I don't understand how remember will help me here, but I actually managed to solve it with inspiration from this question: tex.stackexchange.com/questions/12091/… –  gablin Sep 30 '11 at 12:00
    
Oops, that should be 2.10. Sorry. And would you care posting your working solution here? –  percusse Sep 30 '11 at 12:02
    
@percusse: Ah *phew*, got kind of scared there. ^^ Certainly, it is already posted. –  gablin Sep 30 '11 at 12:03
1  
my bad, I should have more coffee. One last thing, can you replace the ... with something that would compile e.g. \node at (\n,\j) {\n,\j}; such that one can execute the code and can obtain something, doesn't matter what. Sorry again :) –  percusse Sep 30 '11 at 12:07

1 Answer 1

up vote 2 down vote accepted

For anyone interested, here's how I solved it:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}

\begin{document}

\begingroup
\newdimen\bitSize
\bitSize=2mm
\newdimen\bitSep
\bitSep=0.5mm
\newdimen\bitBigSep
\bitBigSep=3mm
\begin{tikzpicture}[%
    auto,
    every node/.style={%
      node distance=0pt,
    },
    bit/.style={%
      draw,
      rectangle,
      minimum size=\bitSize,
      inner sep=0pt,
      node distance=\bitSep,
    },
  ]

  \def\numBitsList{4, 16, 4}
  \def\numBitsArray{{\numBitsList}}
  \foreach \n [count=\i] in \numBitsList {%
    \pgfmathtruncatemacro\prevI{\i-1}
    \pgfmathparse{\numBitsArray[\prevI]} \let\numBits\pgfmathresult
    \foreach \j in {1, ..., \numBits} {%
      \pgfmathtruncatemacro\prevJ{\j-1}

      % Draw bit
      \ifnum \j=1
        \ifnum \i=1
          \node [bit] (bit\i-\j) {};
        \else
          \pgfmathparse{\numBitsArray[\prevI-1]} \let\pprevJ\pgfmathresult
          \node [bit, right=\bitBigSep of bit\prevI-\pprevJ]
            (bit\i-\j) {};
        \fi
      \else
        \node [bit, right=of bit\i-\prevJ] (bit\i-\j) {};
      \fi
    }
  }
\end{tikzpicture}
\endgroup

\end{document}
share|improve this answer

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.