Example: Fibonacci spiral

Published 2009-08-03 | Author: Andrew Mertz

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Fibonacci spiral

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

% Fibonacci spiral
% Author: Andrew Mertz
% I have seen many beautiful depictions of Fibonacci spirals and
% golden spirals. So I thought it would be nice to make a Fibonacci
% spiral in TikZ. I like the look of white on black so here I define a
% black background rectangle.
\begin{tikzpicture}[background rectangle/.style={fill=black},
                    show background rectangle] 
  % Create some counters for holding the Fibonacci numbers

  % Initialize the counters

  % The spiral will start at the origin
  \coordinate (0) at (0,0);

  % This loop defines the number of turns in the spiral. Note that we
  % will have to be careful not to overflow our counters or make the
  % spiral too large for TeX to handle. This is easy to do as the
  % Fibonacci sequence grows exponentially.
  \foreach \i in {1,...,18}
    % Get the "name" of the last point on the spiral

    % Compute the angle for this turn of the spiral
    \pgfmathsetmacro{\startangle}{mod(\i-1,4) * 90}

    % Draw this turn of the spiral and remember the point where we end 
    \draw[white] (\lastpoint) arc 
      (\startangle : \startangle + 90 : \value{b}/10.0pt) coordinate (\i);

   % Compute the next Fibonacci number

 % Add some framing for the spiral while at the same time not "boxing"
 % it in. Note that to put a square around each turn of the spiral we
 % could have just used the command \draw[white] (\lastpoint)
 % rectangle (\i); after drawing each turn in the loop above.
 \foreach \i in {1,3,...,17}
   \draw[white] (\lastpoint) -| (\i);

 \foreach \i in {2,4,...,16}
   \draw[white] (\lastpoint) |- (\i);

 \draw[white] (17) -- (17 |- 18);
 % Add some text displaying the formula for the Fibonacci numbers
 \node(eq) at ($(18) + (2.5,1)$) 
   [white,text width = 2cm,font=\fontsize{8}{8}\selectfont] {
     f(n) = \left\{
         0 & \text{~~if } n = 0\\
         1 & \text{~~if } n = 1\\
         f(n-1) + f(n-2) & \text{~~if } n \geq 2\\
% LocalWords:  tikzpicture eq lr TikZ


  • #1 Nilay, August 4, 2009 at 11:25 a.m.


    i tried to compile this code on my machine. And i get following error message . Can you pl. help me ?

    ! Undefined control sequence.
    \pgfsetplottension ...ttension {\pgf@sys@tonumber
                                                      \pgf@x }
    l.104 \pgfsetplottension{0.5}
  • #2 Kjell Magne Fauske, August 4, 2009 at 2:20 p.m.

    What version of PGF are you using Nilay? I assume it works with PGF 2.0 or later, but I have only tested it using a recent CVS build of PGF

  • #3 Andrew Mertz, August 4, 2009 at 8:30 p.m.

    I have tested it with TeX Live 2008 using PGF 2.0.

  • #4 Vinny, June 6, 2010 at 8:16 a.m.

    I have the same error message. I have a clean installation of teTeX in CentOS 5.5 and I'm using the latest build of pgf/tikz. Can anyone help?

Adding comments is currently not enabled.

Scientific and technical areas


Preorder discount 35%
coupon code LXCB35

LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

click here to read whole story

Creative Commons License