# How to iteratively draw a path e.g. with foreach

I have a long path and want to draw parts of it in an iterative manner

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

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{shapes,arrows}

\begin{tikzpicture}
\newcommand{\room}{
\coordinate (a) at  (0,0) ;
\coordinate (b) at (5,0);
\coordinate (c) at   (5,5);
\coordinate (d) at (0,4);
\coordinate (e) at (1,3);
\coordinate (f) at  (1.4,3) ;
\coordinate (g) at (1.4,2.6)    ;
\coordinate (h) at (1,2.6)  ;
\coordinate (i) at (1,3)    ;
}

\begin{scope}[xshift= 0cm]
\room
\draw plot coordinates{(a) (b)};
\end{scope}

\begin{scope}[xshift= 6cm]
\room
\draw plot coordinates{(a) (b) (c)};
\end{scope}

\begin{scope}[xshift= 12cm]
\room
\draw plot coordinates{(a) (b) (c) (d)};
\end{scope}

%... more plots to come here

\end{tikzpicture}
\end{document}

-

Do you mean something like this?

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

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}

\begin{tikzpicture}[scale=0.2]
\newcommand{\room}{
\coordinate (a1) at  (0,0) ;
\coordinate (a2) at (5,0);
\coordinate (a3) at   (5,5);
\coordinate (a4) at (0,4);
\coordinate (a5) at (1,3);
\coordinate (a6) at  (1.4,3) ;
\coordinate (a7) at (1.4,2.6)    ;
\coordinate (a8) at (1,2.6)  ;
\coordinate (a8) at (1,3)    ;
}

\foreach \i in {2,...,8}{
\pgfmathsetmacro{\li}{\i-1}
\pgfmathsetmacro{\si}{6*\i}
\begin{scope}[xshift=\si cm]
\room
\foreach \k in {1,...,\li}
{
\pgfmathsetmacro{\hk}{\k+1}
\draw plot coordinates{(a\k) (a\hk)};
}
\end{scope}
}

\end{tikzpicture}
\end{document}


-
Exactly !!! I wondered whether it was possible without going down to pgf commands, since I know nothing about them yet, bus this seems no black magic either. Thanks a lot for the solution –  Martin May 5 '12 at 14:21

One solution could be introduce another command, \drawcurrentroom which creates the room, draws the current path and shifts the distance for the next image.

To shift images I used the method shown you yesterday.

Here is my MWE:

\documentclass{article}
\usepackage{tikz}

\newlength\mydistance
\setlength{\mydistance}{0cm}

\usetikzlibrary{shapes,arrows}
\usetikzlibrary{calc}
\usetikzlibrary{shapes,arrows}

\newcommand{\room}{
\coordinate (a) at  (0,0) ;
\coordinate (b) at (5,0);
\coordinate (c) at   (5,5);
\coordinate (d) at (0,4);
\coordinate (e) at (1,3);
\coordinate (f) at  (1.4,3) ;
\coordinate (g) at (1.4,2.6)    ;
\coordinate (h) at (1,2.6)  ;
\coordinate (i) at (1,3)    ;
}

\newcommand{\drawcurrentroom}[1]{
\begin{scope}[xshift=\mydistance]
\room
\foreach \x/\y in {#1}
\draw (\x)--(\y);
\end{scope}
}

\begin{document}

\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}

\end{document}


The command \drawcurrentroom takes as argument the list of connections: they should be separated by a comma; for example:

\drawcurrentroom{a/b,b/c}


means that there is a path from a to b and another one from b to c. If you always use increasing sequences maybe it could be possible to automate more the job, but I think it is easy enough.

The graphical result is:

EDIT I forgot of saying that it is better when you draw another picture, to reset the length to 0; thus:

\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}

\setlength{\mydistance}{0cm}

\begin{tikzpicture}[scale=0.85]
\drawcurrentroom{a/b}
\drawcurrentroom{a/b,b/c}
\drawcurrentroom{a/b,b/c,c/d}
\end{tikzpicture}

-
Hi Claudio, your shift mehtod is very useful. I did not update the shift code for this question out of pure laziness. My main problem is how I prolong the path using foreach. Your idea is pretty good though, thanks! –  Martin May 5 '12 at 14:05
Actually I'm after that bit of more automation if it is possible. But admitted it is more of an academic question ;-) –  Martin May 5 '12 at 14:19