# Nesting fractal tikz decorations (Koch snowflake)

I am trying to come up with some code to allow the use of a decoration (for example the Koch snowflake from decorations.fractals) where you provide a single parameter to determine how many times to decoration is applied (in a nested way). I have tried to use a pic, as outlined below, but it comes up with an errors to do with the decorator either having an empty argument or not being followed by a brace:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

\begin{document}
\newcommand{\koch}[1]{
\foreach \i in {1,...,#1}{decorate\{}
(0,0)--(1,0)
\foreach \i in {1,...,#1}{\}};
}

\tikzset{pics/koch/.style={decoration={Koch snowflake}
code={\draw \koch{#1}}
}
}
\begin{tikzpicture}
\path (0,0) pic {koch=4};
\end{tikzpicture}

\end{document}


I have tried a few variations on this (it says \pic is undefined) this code is saying that \tikz@parabola is undefined and the error seems to be between the i and c of pic (that is where the error message splits).

What I want in the end is some code so that I can do something like

\begin{tikzpicture}
\draw (0,0) \koch{4};
\draw (0,1) \koch{3};
\end{tikzpicture


To get the 3rd and 4th iterations of the Koch decorator on an interval. Even better I suppose would be to have something to pass a decorator and a number to so you can choose different fractal decorators to use.

\documentclass[tikz, border=5]{standalone}
\usetikzlibrary{lindenmayersystems}

\tikzset{koch snowflake/.style={insert path={%
l-system [l-system={rule set={F -> F-F++F-F}, axiom=F++F++F,
step=0.75cm/3^#1, angle=60, order=#1,anchor=center}] -- cycle}}}

\begin{document}
\tikz
\foreach \i in {0,...,4}
\fill (0,\i) [koch snowflake=\i] node [text=white, font=\sf] {\i};

\end{document}


• I have not encountered the lindenmayersystems library yet - I'll have to have a look at it. It does look promising though. – Dom Oct 11 '14 at 11:44
• @Dom You can look at this example: latex-community.org/forum/viewtopic.php?f=45&p=62466 – AboAmmar Oct 11 '14 at 12:03
• Is it possible to modify the code in order to see the construction line and also all the equilateral triangle inside every figure? @mark-wibrow – ryuk Nov 26 '17 at 0:08

With

\usepackage{tikz}
\usetikzlibrary{decorations.fractals}


The loop

\def\deco{(C) -- (B) -- (A) --cycle}% "path at 0 decorations"
\let\decorationlist=\empty% create List
\foreach \n  in {1,...,\NoIterations}
{
\ifx\empty\decorationlist{} \xdef\decorationlist{\deco}%
\else \xdef\deco{decorate{\deco}} \xdef\decorationlist{\decorationlist,\deco}%
\fi
}


produces

 (C) – (B) – (A)
–cycle ,decorate (C) – (B) – (A) –
cycle ,decoratedecorate (C) – (B) –
(A) –cycle ,decoratedecoratedecorate
(C) – (B) – (A) –cycle


This may be sth. of a brute-force method, but it works.

%\documentclass[margin=5pt, tikz]{standalone}
\documentclass[a4paper]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

\begin{document}
\section{Simple Example}
\begin{tikzpicture}[decoration=Koch snowflake]
\coordinate (A) at (0,0);
\coordinate (B) at (3,0);
\coordinate (C) at (60:3);

\draw[] (A) -- (B) -- (C) --cycle;
\draw[transform canvas={shift={(3.2,0)}}]  decorate{ (C) -- (B) -- (A) --cycle};
\draw[transform canvas={shift={(6.4,0)}}]  decorate{ decorate{ (C) -- (B) -- (A) --cycle} };
\end{tikzpicture}

%\newpage
\section{More systematic}
\pgfmathsetmacro\a{4.0}
\pgfmathtruncatemacro\NoIterations{6}
\pgfmathtruncatemacro\cols{3}

\pgfmathtruncatemacro\Cols{\cols-1}
\pgfmathtruncatemacro\Rows{ceil(\NoIterations/\cols)-1}
%\subsection{Info: cols: \cols, Cols: \Cols, Rows: \Rows}
\noindent\begin{tikzpicture}[decoration=Koch snowflake,
]
\coordinate (A) at (0,0);
\coordinate (B) at (\a,0);
\coordinate (C) at (60:\a);

\def\deco{(C) -- (B) -- (A) --cycle}% "path at 0 decorations"
\let\decorationlist=\empty% create List
\foreach \n  in {1,...,\NoIterations}
{
\ifx\empty\decorationlist{} \xdef\decorationlist{\deco}%
\else \xdef\deco{decorate{\deco}} \xdef\decorationlist{\decorationlist,\deco}%
\fi
}
%\node[text width=6cm] at (0,-15) {Show decorationlist: \decorationlist};

\newcounter{mypos}
\setcounter{mypos}{-1}
\foreach \y in {0,...,\Rows} {
\foreach \x in {0,...,\Cols} {
\stepcounter{mypos}
\pgfmathsetmacro\Showcoord{\themypos < \NoIterations ? \themypos : ""}
\coordinate[label=left:{\Showcoord}] (Coord-\themypos) at (1.2*\a*\x,-1.2*\a*\y);
%\node[red, right] at (Coord-\themypos) {\x,\y};
}}