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 have the following code:

\documentclass[11pt]{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance = 0.5cm, auto]
\node (0) {Block 0};
\node (1) [below= of 0.south] {Block 1};
\node (2) [below= of 1.south] {Block 2};
\end{tikzpicture}
\end{document}

It prints this:

enter image description here

I tried to do the same with pgf loop, but don't know how to decrease the counter:

\foreach \x in {0,...,2}
{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      %\prev=\x - 1 <- ???
      \node (\x) [below= of \prev.south] {Block \x};
  \fi
}
share|improve this question
    
The .south is not needed, see Package PGF Math Error: Unknown operator –  Qrrbrbirlbel Apr 23 '13 at 21:42
    
@Qrrbrbirlbel Plz, mention, that I included positioning library at the top snippet. So, it's ok. –  user4035 Apr 23 '13 at 21:48
    
As the linked question shows, the positioning library actually makes =of possible. What I meant was, that below implicitly sets the .south anchor of the “of” node. It doesn’t hurt either. –  Qrrbrbirlbel Apr 23 '13 at 21:53
    
You might prefer naming nodes with some other pattern because right now it's hanging in the balance waiting TikZ to confuse it with a regular number :) –  percusse Apr 23 '13 at 21:59
    
In this simple case we can also use \foreach \x [remember=\x as \eval] in … and don't need to calculate anything. –  Qrrbrbirlbel Apr 26 '13 at 17:16
add comment

2 Answers

up vote 9 down vote accepted

You can use math :)

\foreach \x in {0,...,2}{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      \pgfmathparse{int(\x-1)}
      \node (\x) [below= of \pgfmathresult.south] {Block \x};
  \fi
}
share|improve this answer
add comment

Besides using \pgfmathparse{int(…)} you can also use \pgfmathtruncatemacro or:

  1. eTeX’s \numexpr,
  2. \foreach’s evaluate (again with int),
  3. \foreach’s remember,
  4. \foreach’s count starting from -1 (i.e. first \x minus one)
  5. (recommended) the chains library with start chain=<chain name> going below.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,chains}
\tikzset{node distance=.5cm}
\begin{document}
\begin{tikzpicture}
\foreach \x in {0,...,2}{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      \node (\x) [below=of \number\numexpr\x-1\relax] {Block \x};
  \fi
}
\end{tikzpicture}

\begin{tikzpicture}
\foreach \x[evaluate=\x as \eval using int(\x-1)] in {0,...,2}{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      \node (\x) [below=of \eval] {Block \x};
  \fi
}
\end{tikzpicture}

\begin{tikzpicture}
\foreach \x[remember=\x as \eval] in {0,...,2}{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      \node (\x) [below=of \eval] {Block \x};
  \fi
}
\end{tikzpicture}

\begin{tikzpicture}
\foreach \x[count=\eval from -1] in {0,...,2}{
  \ifnum\x=0\relax
      \node (\x) {Block \x};
  \else
      \node (\x) [below=of \eval] {Block \x};
  \fi
}
\end{tikzpicture}

\begin{tikzpicture}[start chain=going below]
\foreach \x in {0,...,2}
  \node[on chain] (\x) {Block \x};
\end{tikzpicture}
\end{document}

Output (all solutions)

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.