Example: Pascal triangle

Published 2016-12-30 | Author: M.H. Ahmadi

A calculated Pascal triangle built with hexagons.

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

Pascal 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. En français: TeXnique.fr.

% Pascal triangle
% Author: M.H. Ahmadi 
    % Store values 
    \c@pgf@counta=#1% n
    \c@pgf@countb=#2% k
    % Take advantage of symmetry if k > n - k
    \advance\c@pgf@countc by-\c@pgf@countb%
    % Recursively compute the coefficients
    \c@pgf@countc=1% will hold the result
    \c@pgf@countd=0% counter
    \pgfmathloop% c -> c*(n-i)/(i+1) for i=0,...,k-1
        \multiply\c@pgf@countc by\c@pgf@counta%
        \advance\c@pgf@counta by-1%
        \advance\c@pgf@countd by1%
        \divide\c@pgf@countc by\c@pgf@countd%
\begin{tikzpicture}[line width=.8pt]
  \foreach \k in {0,...,12}{
      \foreach \n in {0,...,\ystart}{
        \ifthenelse{\k=8 \AND \n < 4}{\def\mycolor{purple}}{}
        \ifthenelse{\k=9 \AND \n = 3}{\def\mycolor{purple}}{}
           \draw[top color=\mycolor!20,bottom color=\mycolor!60] 
             (30:\R) \foreach \x in {90,150,...,330} {
                -- (\x:\R)}
                -- cycle (90:0)
                   node {\tiny $\mathbf{\binomialCoefficient{\newn}{\k}}$};


Adding comments is currently not enabled.

Scientific and technical areas


LaTeX Beginners Guide

Limited discount 50%
coupon code tDRet6Y

Creative Commons License