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.

In the MWE below

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
  \foreach \i in {0, 1, 2, 3}
    \draw (\i, 0) rectangle +(0.5, 0.5);
%   \draw (\i, 1) rectangle +(0.5, 0.5);
%   Error if above line is uncommented:
%   ! Undefined control sequence.
%   \pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@
  \end{tikzpicture}
\end{document}

the file compiles as is with pdflatex but gives an error as noted in MWE if the commented line is uncommented.

I know I am doing something silly, but being an occasional user of TikZ, I am unsure what it is. Grateful for an explanation.

share|improve this question
add comment

2 Answers

up vote 17 down vote accepted

To have more than one \draw (or similar) within a loop, you have to enclose them in braces ({}):

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
  \foreach \i in {0, 1, 2, 3} {
    \draw (\i, 0) rectangle +(0.5, 0.5);
    \draw (\i, 1) rectangle +(0.5, 0.5);
   }
  \end{tikzpicture}
\end{document}
share|improve this answer
    
Thank you! I would never have guessed, given the nature of the error message. –  chandra Apr 16 '12 at 17:22
add comment

I agree with Torbjørn's answer but in your case, it was possible to write the next code. I use only one command \draw on one path. I think is important to understand what is a path to work correctly with TikZ. The end of the path is determined by ;

\documentclass{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
  \foreach \i in {0, 1, 2, 3} 
    \draw (\i, 0) rectangle +(0.5, 0.5)
          (\i, 1) rectangle +(0.5, 0.5);
  \end{tikzpicture}
\end{document} 
share|improve this answer
    
I was totally unaware that the end of a path is determined by the semi-colon. Your syntax allows more compact styling of the rectangles as well. Thanks. –  chandra Apr 18 '12 at 9:10
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.