Example: Pascal’s triangle and Sierpinski triangle

Published 2009-10-26 | Author: Paul Gaborit

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

Pascal's triangle and Sierpinski triangle

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.

% Author : Paul Gaborit (2009)
% under Creative Commons attribution license.
% Title : Pascal's triangle and Sierpinski triangle
% Note : 17 lines maximum


  % some colors
  % some styles
      minimum height=5mm,
      inner sep=.7mm,
      outer sep=0mm,
      text width=10mm,
      text centered,
      line width=.25mm,
      top color=#1!5,
      bottom color=#1!40,
      shading angle=0,
      rounded corners=2.3mm,
      drop shadow={fill=#1!40!gray,fill opacity=.8},
    link/.style={-latex,links,line width=.3mm},
  % Pascal's triangle
  % row #0 => value is 1
  \node[box=odd] (p-0-0) at (0,0) {1};
  \foreach \row in {1,...,16} {
     % col #0 => value is 1
    \node[box=odd] (p-\row-0) at (-\row/2,-\row) {1};
    \foreach \col in {1,...,\row} {
      % iterative formula : val = precval * (row-col+1)/col
      % (+ 0.5 to bypass rounding errors)
      % position of each value
      \coordinate (pos) at (-\row/2+\col,-\row);
      % odd color for odd value and even color for even value
      \ifnum \rest=0
        \node[box=even] (p-\row-\col) at (pos) {\value};
        \node[box=odd] (p-\row-\col) at (pos) {\value};
      % for arrows and plus sign
      \ifnum \col<\row
        \node[plus,above=0mm of p-\row-\col]{+};
        \draw[link] (p-\prow-\pcol) -- (p-\row-\col);
        \draw[link] ( p-\prow-\col) -- (p-\row-\col);
    % filling and drawing with the same color to enlarge background
    \path[draw=back,fill=back,line width=5mm,rounded corners=2.5mm]
    (  p-0-0.north west) -- (  p-0-0.north east) --
    (p-16-16.north east) -- (p-16-16.south east) --
    ( p-16-0.south west) -- ( p-16-0.north west) --



  • #1 Marcus, October 1, 2010 at 6:22 p.m.

    Excellent! That line


    was just what I needed to implement a Runge-Kutta type numerical integration for solving ODEs in my tikz-pictures.

    Thank you!

  • #2 olufemi opeyemi oyadare, January 27, 2012 at 4:13 p.m.

    i have a method of proving the fermat's last theorem via the pascal triangle. do you want to have a look?

  • #3 Kristofer, July 26, 2012 at 2:31 a.m.

    Nice illustration! You should just remove that last row as I think it's a little bit confusing since it makes it less clear that it actually is the Sierpinski triangle we have here.

    olufemi opeyemi oyadare, really? You have to show us.

  • #4 olufemi opeyemi oyadare, October 3, 2012 at 5:47 p.m.

    If you wish to have a look at the proof, google for 'newtonian triangles, for the complete paper.

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