Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Paul Gessler Jul 8 at 4:27
1  
I have a follow-up question. Is it possible to do Mark's solution with symbols in Pascal's triangle? So, I could create the same figure, but instead of numbers, I would generate Pascal's triangle with ${\n \choose \k}$ at each node. –  Natalya Jul 21 at 1:57

2 Answers 2

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

share|improve this answer
    
This worked great in my document, thank you, Mark! –  Natalya Jul 21 at 1:51

Run with lualatex (the number of rows is not limited):

\documentclass[border=5]{standalone}
\usepackage{luacode,array}
\def\PascalTriangle#1{\directlua{PascalTriangle(#1)}}
\begin{luacode}
function nextrow(t)
  local ret = {}
  t[0], t[#t+1] = 0, 0
  for i = 1, #t do ret[i] = t[i-1] + t[i] end
  return ret
end

function PascalTriangle(n)
  tex.sprint("\\tabular{@{}lc@{}}")
  t = {1}
  tex.sprint("Row 0: & 1 \\\\")
  for i=1,n do
    t = nextrow(t)
    tex.print("Row "..i..": & ")
    for j=1,i+1 do tex.print("\\makebox[2em]{"..tostring(t[j]).."}") end 
    -- tex.sprint(tostring(unpack(t))) -- does not work
    tex.sprint("\\\\")
  end
  tex.sprint("\\endtabular")
end
\end{luacode}
\begin{document}
\footnotesize
\PascalTriangle{5}

\bigskip
\PascalTriangle{15}

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.