TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to draw some self similar fractals and highlight the first iteration. I was able to do so for the Sierpinski Triangle, although my code is somewhat ugly. (A minimal working example is included at the end of this post.)

But how to achieve similar results for the Sierpinski Carpet, Box Fractal, Cantor Dust, Koch curve and others? The googleable l-systems do help me, as they often only look similar to the fractal in high orders. For example: Can I use this Koch Curve l-system for my purpose?

As promised, finally the code:

\pgfdeclarelindenmayersystem{Sierpinski triangle}{
    \rule{X -> X-Y+X+Y-X}
    \rule{Y -> YY}

  \fill[black] (0,0) -- ++(0:\trianglewidth) -- ++(120:\trianglewidth) -- cycle;
  \path l-system
  [l-system={Sierpinski triangle, axiom=X, step=\trianglewidth/(2^\level), order=\level, angle=-120},fill=blue!25];
  \path l-system
  [l-system={Sierpinski triangle, axiom=X, step=\trianglewidth/(2^1), order=1, angle=-120},fill=white];

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.