# how to draw recursive picture with tikzpicture?

Suppose I need to draw a Kantor set, or a Sierpinski carpet, or a Koch curve, or any such of simple fractal.

Tipically you can do that by drawing a picture, then rescalining it and replicating in several places.

I tried by using \foreach \a in {...} combined to \begin{scope}[shift{some function of \a},scale{some function of \a}]

but I did not succeed.

So which is the correct way to combine a cycle for with moving and rescaling a picture?

More precisely I tried:

\foreach \a in {0,1,2}

\begin{scope}[shift={(10\a,0)}, scale=1/3]
mypicture

\end{scope}


this gives errors

"Extra }, or forgotten \endgroup."

and with group parenthesis

\foreach \a in {0,1,2}

{\begin{scope}[shift={(10\a,0)}, scale=1/3]
mypicture

\end{scope}}


which seems just not working.

• Nov 6, 2017 at 16:47
• Did you mean 10*\a? Nov 6, 2017 at 20:01
• Nov 7, 2017 at 9:43
• Ok, thanks for 10*\a (!) but still does not work Nov 7, 2017 at 9:55
• @HenriMenke would you write an answer? Nov 14, 2017 at 17:30

There are two errors in you code.

1. You probably want to multiply \a by 10, so you have to write 10*\a.
2. The body of the foreach loop has to be in a brace group if it is more than a single statement. While

\foreach \a in {0,1,2}
\node at (10*\a,0) {\a};


is okay (single statement, ending with ;), this is not

\foreach \a in {0,1,2}
\begin{scope}[shift={(10*\a,0)}]
\node at (0,0) {\a}; % scanning actually stops at this ;
\end{scope} % this is not reached -> error!


If you correct these errors, the following MWE typesets just fine.

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

\begin{tikzpicture}
\foreach \a in {0,1,2} {
\begin{scope}[shift={(10*\a,0)}]
\node at (0,0) {\a};
\end{scope}
}
\end{tikzpicture}

\end{document}