# Drawing a simple Fractal in Tikz [duplicate]

This question already has an answer here:

I am trying to draw a fractal in tikz that is not a 'common' one - so I'm a little stuck.

It is essentially the Sierpinski Casket but with a square instead of a triangle. Start with the (solid) unit square, remove the centre 9-th, and repeat with the remaining 8 squares. So you end up with 8^k squares of size 1/9^k.

I'd like it to look something like: Preferably the 'last' image will be of stage 5 or 6 but, if possible, a nice definable end stage would be great (like other fractals in LaTeX).

## marked as duplicate by cfr tikz-pgf StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jun 11 '17 at 4:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

## 1 Answer

Use the lindenmayersystems of TikZ.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{lindenmayersystems}
\begin{document}
\begin{tikzpicture}[l-system={step=1pt, order=5, angle=90}]
\pgfdeclarelindenmayersystem{Sierpinski carpet}{
\rule{F -> F+F-F-FF-F-F-fF}
\rule{f -> fff}
}
\draw l-system [l-system={Sierpinski carpet, axiom=F}];
\end{tikzpicture}
\end{document} • Wow, every day I find out a new gorgeous feature of tikz! – CarLaTeX Jun 11 '17 at 5:29