[Tex/LaTex] Pascal’s triangle in tikz with row headers

loopstikz-pgf

I am interested in creating Pascal's triangle with Tikz and LaTeX. I would like to add a column of text on the left of the triangle, with header "Row Number" and then the entries of each row. I am new to Tikz and had trouble creating a structure like this on my own.

Like this (center justified triangle, not left aligned)

Row 0: 1
Row 1: 1
Row 2: 1 2 1
Row 3: 1 3 3 1
Row 4: 1 4 6 4 1
Row 5: 1 5 10 10 5 1
Row 6: 1 6 15 20 15 6 1
Row 7: 1 7 21 35 35 21 7 1
Row 8: 1 8 28 56 70 56 28 8 1
Row 9: 1 9 36 84 126 126 84 36 9 1
Row 10: 1 10 45 120 210 252 210 120 45 10 1

This code generated ideas, though since I am new to tikz, I wasn't sure where to go with it.

Pascal's triangle in tikz

Thanks!

Best Answer

The following should do it. By using the fpu library it is possible to go beyond the usual limit of 17 rows in PGF.

\documentclass[tikz,border=5]{standalone}
\usepgflibrary{fpu}
\begin{document}
\def\N{10}
\tikz[x=0.75cm,y=0.5cm, 
  pascal node/.style={font=\footnotesize}, 
  row node/.style={font=\footnotesize, anchor=west, shift=(180:1)}]
  \path  
    \foreach \n in {0,...,\N} { 
      (-\N/2-1, -\n) node  [row node/.try]{Row \n:}
        \foreach \k in {0,...,\n}{
          (-\n/2+\k,-\n) node [pascal node/.try] {%
            \pgfkeys{/pgf/fpu}%
            \pgfmathparse{round(\n!/(\k!*(\n-\k)!))}%
            \pgfmathfloattoint{\pgfmathresult}%
            \pgfmathresult%
          }}};
\end{document}

enter image description here

Related Question