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

In my thesis that I'm writing on, I'm using a Mandelbrot generator to test an application I've written. To explain a certain problem discussed in the report an actual generated image need to be included.

Since every figure in the report so far has been generated using TikZ, I'm wondering whether I could also generate the Mandelbrot image using TikZ. But would it possible?

share|improve this question
There's a TikZ library for fractal decorations (30.7 Fractal Decorations) and a Lindenmayer systems library (37 Lindenmayer System Drawing Library) – Tom Bombadil Aug 25 '12 at 15:44
Heiko Oberdiek posted a Mandelbrot generator in LaTeX at the Programming Puzzels & Code Golf stackexchange: codegolf.stackexchange.com/questions/23423/… – Martin Heller Mar 25 '14 at 5:50
up vote 27 down vote accepted

There's a Mandelbrot set shading in the shadings library:

\tikz\shade[shading=Mandelbrot set] (0,0) rectangle (4,4);

Mandelbrot set

Displayed with the Acrobat Reader, as the TeXworks PDF previewer did not show high resolution.

The Mandelbrot set is computed and generated by the PDF renderer. The algorithm is written using PostScript commands, PDF supports a subset of Postscript operators, that's why it should be much faster than computing using higher level pgf or TikZ operators.

share|improve this answer
Ha! Well, that was unexpected. ^^ Unfortunately, the image doesn't exactly look like a Mandelbrot set, but cool nonetheless. – gablin Dec 28 '11 at 17:05
@gablin This could be caused by a low number of iterations, I would expect that a higher number of iterations would be nearer to the exact Mandelbrot set, which would require an infinite number of iterations. – Stefan Kottwitz Dec 28 '11 at 17:10
@StefanKottwitz Is there any way to increase the iteration count? That looks like 10 or so iterations . . . you get a much better picture even with just 20. – mrc Nov 15 '13 at 18:53
@mrc Yes, it can be seen in the code of that TikZ library, the number of iteration is fixed but can be extended by changing or redefining the code. – Stefan Kottwitz Nov 15 '13 at 22:15

Here is a rather crude solution using luatex. Note that the calculations are done without any optimization, no use of symmetry or any other properties of the Mandelbrot set. Warning: the code takes a while to process even on a fairly fast computer.



local NUMITER=100

point_iter = function (a,b) 
   local n = 1
   local x = a
   local y = b
   return function ()
      if n > NUMITER then return nil end
      n = n+1
      x,y = x*x - y*y + a, 2*x*y + b
      return n, x*x + y*y

escape_time = function (a,b)
   local esc_time
   for n, d in point_iter(a,b) do
      esc_time = n
      if d > 4 then break end
   return esc_time

mand_color = function (a,b)
   local etime = escape_time(a,b)
   if etime == NUMITER+1 then tex.print("black") else tex.print("red!"..etime) end
   \foreach \x in {-2,-1.99,...,.5}{
      \foreach \y in {-1.3,-1.29,...,1.3}{
      \draw[\mandcolor{\x}{\y},fill] (\x,\y) +(-.005,-.005) rectangle +(.005,.005);}}

This is the resulting image:

mandelbrot set generated with lualatex

The next step would be to use mplib directly from lua to render the picture.

share|improve this answer

For those who are interested in another approach, i.e., using PSTricks:



enter image description here

Compile the code snippet above with xelatex or latex-dvips-ps2pdf sequence.

share|improve this answer
Very cool! How does the pstricks compare to MATLAB or Python in the terms of speed when it comes to things like plotting strange attractors, KAM islands or fractals? How difficult is to generate movie? In MATLAB is for example trivial to generate movie which shows creation of the Lorenz attractor. It is fairly easy to call such movie from the Powerdot slides (written in PSTricks) but I wonder if I could create animation natively which will not require to launch a video player. – Predrag Punosevac Dec 29 '11 at 2:05
I believe that pstricks does not actually generate the image, it inserts postscript code that does the actual calculation. Which means the speed would depend on your postscript interpreter. When printing such document on a postscript printer, the calculations would be done on the printer. When viewing the resulting dvi or postscript file, the calculations would be done by your dvi or ps viewer at the time of viewing. Perhaps you could first convert from postscript to another format, the calculation would happen then, and view the result later. – Jan Hlavacek Dec 29 '11 at 2:52
Of course. I meant PostScript and Ghostscript interpreter vs MATLAB or Python. See my post here for getting row PostScript into TeX with command special tex.stackexchange.com/questions/23987/… – Predrag Punosevac Dec 29 '11 at 4:39

I posted a pgfplots solution on TeXwelt.de drawing the Mandelbrot set: Fraktale mit pgfplots. Christian Feuersänger improved it, instead of creating a table (as inspired by Herbert) doing the sampling via \addplot3 and the coloring by surf and shader=interp. Run it with LuaLaTeX.

Mandelbrot set

The complete code:

  function mandelbrot(cx,cy, max_iter, max)
    local x,y,xtemp,ytemp,squaresum,iter
    squaresum = 0
    x = 0
    y = 0
    iter = 0
    while (squaresum <= max) and (iter < max_iter)  do
      xtemp = x * x - y * y + cx
      ytemp = 2 * x * y + cy
      x = xtemp
      y = ytemp
      iter = iter + 1
      squaresum = x * x + y * y
    local result = 0
    if (iter < max_iter) then
        result = iter
    -- result = squaresum
    -- io.write("" .. cx .. ", " .. cy .. " = " .. result .. " (iter " .. iter .. " squaresum " .. squaresum .. ") \string\n")
    axis equal,
    point meta max=30,
    tick label style={font=\tiny},
    \addplot3 [surf,domain=-1.72:0.72,shader=interp,domain y=-1:1,samples=300] {
share|improve this answer

I couldn't resist to proposing a MetaPost version. After all, MetaPost is embedded in LuaTeX and thanks to the package luamplib we have direct access to it, so it would be a pity not to take advantage of this fact :-)) All credits must go to Stephan Kottwitz though (and to Herbert previously), since the following code is almost entirely an adaptation of his Lua Code to MetaPost. Without it, drawing this would probably not have been possible to me, since I know too little about fractals.

Notice the call to MetaPost's newly implemented floating point number system by the instruction \mplibnumbersystem{double}. I figure it is necessary, due to the heaviness of the computations.

Caution: it may take several minutes for the following code to compile! (10 min for my 5-year old laptop.)

    u := 4cm;
    vardef mandelbrot(expr xa, xb, ya, yb, steps, max_iter, maxs) =
      save dx, dy, cx, cy, xtemp, ytemp, squaresum, iter; clearxy;
      dx := (xb-xa)/steps; 
      dy := (yb-ya)/steps; 
      cx := xa;
        exitunless cx <= xb;
        cy := ya;
          exitunless cy <= yb;
          squaresum := 0;
          x:= 0; y:= 0; iter := 0;
            exitunless (squaresum <= maxs) and (iter < max_iter);
            xtemp := x**2 - y**2 + cx;
            ytemp := 2*x*y + cy;
            x := xtemp;
            y := ytemp;
            iter := iter + 1;
            squaresum := x**2 + y**2;
          if iter >= max_iter: draw u*(cx, cy); fi;
          cy := cy + dy;
        cx := cx + dx;
      % Coordinates and marks
      xmin = -2; xmax = 0.75; ymin = -1.25; ymax = 1.25; len := 6bp;
      draw u*(xmin, ymin) -- u*(xmax, ymin) -- u*(xmax, ymax) -- u*(xmin, ymax) -- cycle; 
      for i = -1.5 step .5 until .5:
        label.bot("$" & decimal i & "$", u*(i, ymin));
        draw u*(i, ymin) -- (i*u, ymin*u+len);
        draw u*(i, ymax) -- (i*u, ymax*u-len);
      for j = -1 step 1 until 1:
        label.lft("$"&decimal j&"$", u*(xmin, j));
        draw u*(xmin, j) -- (xmin*u+len, j*u);
        draw u*(xmax, j) -- (xmax*u-len, j*u);
      % Mandelbrot fractal
      pickup pencircle scaled 1bp;
      mandelbrot(-2, 1, -2, 2, 500, 1000, 4);

enter image description here

share|improve this answer

Your Answer


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

Not the answer you're looking for? Browse other questions tagged or ask your own question.