Tx · LaTeX & TikZ

How to Draw Pascal’s Triangle in TikZ

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the row count

    Set the outer loop to run from row zero through the last row you want to display.

  2. 2. Compute each entry

    Use the binomial coefficient for row n and position k to generate each number.

  3. 3. Place the nodes

    Center each row by shifting its positions around the vertical axis.

  4. 4. Adjust the spacing

    Change the horizontal and vertical spacing to fit the triangle in your document.

You can draw Pascal’s triangle without typing every coefficient by hand. The example below uses nested \foreach loops: the outer loop selects a row, and the inner loop visits each position in that row. PGF math calculates the binomial coefficients, while the coordinates center each row beneath the one above it.

Complete compilable example

This document draws rows 0 through 7. It uses only the tikz package and can be compiled with a standard LaTeX engine such as pdfLaTeX.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \foreach \n in {0,...,7} {
    \foreach \k in {0,...,\n} {
      \pgfmathtruncatemacro{\coefficient}{
        factorial(\n)/(factorial(\k)*factorial(\n-\k))
      }
      \node[font=\small, inner sep=1pt]
        at ({(\k-\n/2)*1.0},{-\n*0.65})
        {\coefficient};
    }
  }
\end{tikzpicture}
\end{document}

The standalone class makes a cropped PDF that is convenient for a figure. If you are adding the triangle to an existing document, omit the \documentclass and \begin{document} lines and put the tikzpicture in your document body; keep \usepackage{tikz} in the preamble.

How the code works

Pascal’s triangle has entries given by binomial coefficients. The entry in row nn and position kk, counting both from zero, is

(nk)=n!k!(n−k)!.\binom{n}{k}=\frac{n!}{k!(n-k)!}.

For example, row 4 has positions k=0,1,2,3,4k=0,1,2,3,4, so its entries are 1,4,6,4,11,4,6,4,1. The code evaluates this formula with PGF math’s factorial function. \pgfmathtruncatemacro stores the result as an integer macro, which can then be used as the node text.

The outer loop, \foreach \n in {0,...,7}, chooses the row number. In each row, the inner loop runs from 0 through \n, giving exactly n+1n+1 entries. That is why the left and right edges of the triangle are always 1: they correspond to k=0k=0 and k=nk=n.

The horizontal coordinate is (k - n/2) * 1.0. Subtracting half the row number shifts the row so its midpoint lies at x=0x=0. The vertical coordinate, -n * 0.65, places each successive row lower on the page. The negative sign is needed because TikZ’s positive yy direction points upward.

Worked example: row 5

When the outer loop reaches n=5n=5, the inner loop evaluates six positions. For k=2k=2, for instance, the calculation is

(52)=5!2!3!=10.\binom{5}{2}=\frac{5!}{2!3!}=10.

The complete row is 1,5,10,10,5,11, 5, 10, 10, 5, 1. Its horizontal coordinates are −2.5,−1.5,−0.5,0.5,1.5,2.5-2.5, -1.5, -0.5, 0.5, 1.5, 2.5 cm because the code uses a spacing multiplier of 1.0. The matching vertical coordinate for every node in that row is −3.25-3.25 cm.

Changing the appearance

To draw more rows, change the final value in the outer loop, but keep it at 7 or below with this factorial-based calculation. The standard PGF math numeric range cannot accommodate 8!=403208! = 40320, so the calculation can fail starting at row 8 even though the resulting binomial coefficients fit in range. For larger rows, use a coefficient recurrence or a tool designed for arbitrary-precision integer calculations.

The two coordinate multipliers control the layout. Increase 1.0 to spread entries farther apart horizontally, or decrease it to make the triangle narrower. Change 0.65 to alter the distance between rows. If the widest rows look crowded, increase the horizontal spacing or use a smaller font. The inner sep=1pt option keeps the nodes from adding much extra padding around each number.

You can also add a style to the nodes. For example, \node[draw,circle,inner sep=2pt] puts each coefficient in a circle. Keep the node text in braces and avoid changing the loop variables: TikZ uses them to calculate both the coefficient and its position.

Common mistakes

  • Starting at row 1: Pascal’s triangle is conventionally indexed from row 0, which contains only 1. Starting at 1 omits that top entry and changes the coefficient formula’s interpretation.
  • Using a fixed inner loop: A loop that always runs from 0 to 8 creates extra entries in the shorter rows. Use \n as the upper limit so each row has exactly one more entry than its row number.
  • Forgetting to center rows: Placing entry kk at horizontal position kk aligns all rows at their left edges. Subtracting half the row number centers them.
  • Using positive vertical coordinates: Positive values stack rows upward. Use a negative vertical coordinate to make the triangle grow downward.
  • Assuming every output is exact for any size: PGF math uses finite-precision arithmetic, and this factorial calculation reaches its numeric range limit at row 8. For larger rows, use a method or tool designed for arbitrary-precision integer calculations rather than relying on factorials in PGF math.

References

  1. PGF/TikZ Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.