How to Draw Pascal’s Triangle in TikZ
At a glance
- 1. Choose the row count
Set the outer loop to run from row zero through the last row you want to display.
- 2. Compute each entry
Use the binomial coefficient for row n and position k to generate each number.
- 3. Place the nodes
Center each row by shifting its positions around the vertical axis.
- 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 and position , counting both from zero, is
For example, row 4 has positions , so its entries are . 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 entries. That is why the left and right edges of the triangle are always 1: they correspond to and .
The horizontal coordinate is (k - n/2) * 1.0. Subtracting half the row number shifts the row so its midpoint lies at . The vertical coordinate, -n * 0.65, places each successive row lower on the page. The negative sign is needed because TikZ’s positive direction points upward.
Worked example: row 5
When the outer loop reaches , the inner loop evaluates six positions. For , for instance, the calculation is
The complete row is . Its horizontal coordinates are cm because the code uses a spacing multiplier of 1.0. The matching vertical coordinate for every node in that row is 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 , 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
\nas the upper limit so each row has exactly one more entry than its row number. - Forgetting to center rows: Placing entry at horizontal position 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
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.