Graphics, Figures & Tables\Draw in a \foreach loop

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

\Draw in a \foreach loop

Post by Singularity »

This works:

Code: Select all

\begin{tikzpicture}
	\tikzstyle{every node}=[draw,shape=circle];
	\foreach \n in {1,...,5} {
		\path (\n*360/5:1cm) node (\n) {\n};
	}
	\draw (1) -- (2) -- (3) -- (4) -- (5) -- (1);
\end{tikzpicture}
But this bombs on the \draw loop:

Code: Select all

\begin{tikzpicture}
	\tikzstyle{every node}=[draw,shape=circle];
	\foreach \n in {1,...,5} {
		\path (\n*360/5:1cm) node (\n) {\n};
	}
	\foreach \n in {1,...,4} {
		\draw (\n) -- (\n+1);
	}
	\draw (5) -- (1);
\end{tikzpicture}
Why can't I do \foreach {.... \draw }?

I get the following error messages (the last one is not appearing correctly):
! Package pgf Error: No shape named 1+1 is known.
! Package pgf Error: No shape named 1+1 is known.
! Package pgf Error: No shape named 2+1 is known.
! Package pgf Error: No shape named 3+1 is known.
! Package pgf Error: No shape named 4+1 is known.
! Undefined control sequence.
\endtikzpicture ...icturepositiononpage \endscope
\let \pgf@baseline =\pgf@s...
l.156 \end{tikzpicture}
Why won't this work right?
Thanks.

P.S. Since you're answering that question, how 'bout also telling me how to use a variable for the 5 in the \foreach loop, so I can do something like this

Code: Select all

\begin{tikzpicture}
	\tikzstyle{every node}=[draw,shape=circle];
	\foreach \n in {1,...,\T} {
		\path (\n*360/\T:1cm) node (\n) {\n};
	}
	\draw (1) -- (2) -- (3) -- (4) -- (5) -- (1);
\end{tikzpicture}
Thanks, again.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

\Draw in a \foreach loop

Post by torbjorn t. »

The reason should be clear from the error message. In the \draw command you call nodes with the name \n or \n+1. But the name of a node can be almost anything, and \n+1 isn't evaluated in the \draw command -- hence, you ask for nodes called e.g. "1+1" instead of "2", which is what you want.

By looking in the chapter regarding foreach statements in the pfgmanual (ch. 56, pp. 508 of the PGF 2.10 manual), I found a way of solving this.

For your last question, you could use a normal \newcommand or \def I think, or \pgfmathtruncatemacro as in my example below.

Code: Select all

\documentclass{scrartcl}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
   \pgfmathtruncatemacro{\T}{5}
   \tikzstyle{every node}=[draw,shape=circle];
   \foreach \n in {1,...,\T} 
      \path (\n*360/\T:1cm) node (\n) {\n};
   \foreach \n [remember=\n as \lastn (initially 1)] in {1,...,\T} 
      \draw (\lastn) -- (\n);
   \draw (\T) -- (1);
\end{tikzpicture}
\end{document}
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

\Draw in a \foreach loop

Post by Singularity »

Thanks torbjorn. I'm only cracking the TikZ manual, I didn't even know I had to read the PGF manual, too.

Your code works perfectly, except that my package manager didn't have scrartcl, but I didn't need it, anyway.
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Re: \Draw in a \foreach loop

Post by torbjorn t. »

That's not really the TikZ manual, just an article/a tutorial giving the basics for TikZ.

PGF and TikZ are closely related, in that PGF is the underlying engine on which TikZ, with it's simpler syntax, builds. The official PGF/TikZ manual documents both, and is quite a beast at over 700 pages, but it's not something you read start to finish. The first few chapters are tutorials, which take you through the creation of a few different figures, exploring different concepts along the way. These concepts are treated in more detail in separate chapters, so if you want to know more about one thing, like for loops, the corresponding chapter is a good place to look.

The easiest way of getting the manual is opening a command line and writing "texdoc pgf" or "texdoc tikz". (texdoc is a program included with most LaTeX distributions, for finding documentation.)
Post Reply