2

I would like to modify this Pascal's Triangle example to allow blank rows at the bottom for students to fill out:

enter image description here

Here is what I have tried so far:

\documentclass[border=10pt]{standalone}% \usepackage[dvipsnames]{xcolor} \usepackage{tikz} \usepackage{ifthen} \makeatletter \newcommand\binomialCoefficient[2]{% % Store values \c@pgf@counta=#1% n \c@pgf@countb=#2% k % % Take advantage of symmetry if k > n - k \c@pgf@countc=\c@pgf@counta% \advance\c@pgf@countc by-\c@pgf@countb% \ifnum\c@pgf@countb>\c@pgf@countc% \c@pgf@countb=\c@pgf@countc% \fi% % % 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 \ifnum\c@pgf@countd<\c@pgf@countb% \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% \repeatpgfmathloop% \the\c@pgf@countc% } \makeatother \begin{document} \newdimen\R \R=.4cm \newcommand\mycolor{white} \begin{center} \begin{tikzpicture}[line width=.8pt] \def\rows{9} \foreach \k in {0,...,\rows} { \begin{scope}[shift={(-60:{sqrt(3)*\R*\k})}] \pgfmathtruncatemacro\ystart{\rows-\k} \foreach \n in {0,...,\ystart} { \pgfmathtruncatemacro\newn{\n+\k} % \ifthenelse{\k=0}{\def\mycolor{pink}}{} % \ifthenelse{\k=1}{\def\mycolor{yellow}}{} % \ifthenelse{\k=2}{\def\mycolor{blue}}{} % \ifthenelse{\k=3}{\def\mycolor{green}}{} % \ifthenelse{\k=8 \AND \n < 4}{\def\mycolor{red}}{} % \ifthenelse{\k=9 \AND \n = 3}{\def\mycolor{purple}}{} \begin{scope}[shift={(-120:{sqrt(3)*\R*\n})}] \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}}$}; \end{scope} } \end{scope} } \end{tikzpicture} \end{center} \end{document} 

enter image description here

I was able to get rid of the color and make a custom variable \rows for number of rows. However I did not figure out how to remove the text for the last three rows. Thanks in advance for your help!

2 Answers 2

4

You need to change the node text based on the value of \newn:

enter image description here

Define which is the last non-blank row:

\def\BlankRowAfter{6} 

and then

\ifnum\newn>\BlankRowAfter\relax \def\NodeText{}% \else \def\NodeText{\tiny $\mathbf{\binomialCoefficient{\newn}{\k}}$}% \fi 

determines \NodeText to be blank if needed.

Code:

\documentclass{article}% \usepackage[dvipsnames]{xcolor} \pagecolor{white} \usepackage{tikz} \usepackage{ifthen} \makeatletter \newcommand\binomialCoefficient[2]{% % Store values \c@pgf@counta=#1% n \c@pgf@countb=#2% k % % Take advantage of symmetry if k > n - k \c@pgf@countc=\c@pgf@counta% \advance\c@pgf@countc by-\c@pgf@countb% \ifnum\c@pgf@countb>\c@pgf@countc% \c@pgf@countb=\c@pgf@countc% \fi% % % 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 \ifnum\c@pgf@countd<\c@pgf@countb% \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% \repeatpgfmathloop% \the\c@pgf@countc% } \makeatother \begin{document} \newdimen\R \R=.4cm \newcommand\mycolor{white} \begin{center} \begin{tikzpicture}[line width=.8pt] \def\rows{9} \def\BlankRowAfter{6} \foreach \k in {0,...,\rows} { \begin{scope}[shift={(-60:{sqrt(3)*\R*\k})}] \pgfmathtruncatemacro\ystart{\rows-\k} \foreach \n in {0,...,\ystart} { \pgfmathtruncatemacro\newn{\n+\k} % \ifthenelse{\k=0}{\def\mycolor{pink}}{} % \ifthenelse{\k=1}{\def\mycolor{yellow}}{} % \ifthenelse{\k=2}{\def\mycolor{blue}}{} % \ifthenelse{\k=3}{\def\mycolor{green}}{} % \ifthenelse{\k=8 \AND \n < 4}{\def\mycolor{red}}{} % \ifthenelse{\k=9 \AND \n = 3}{\def\mycolor{purple}}{} \ifnum\newn>\BlankRowAfter\relax \def\NodeText{}% \else \def\NodeText{\tiny $\mathbf{\binomialCoefficient{\newn}{\k}}$}% \fi \begin{scope}[shift={(-120:{sqrt(3)*\R*\n})}] \draw[top color=\mycolor!20,bottom color=\mycolor!60] (30:\R) \foreach \x in {90,150,...,330} { -- (\x:\R)} -- cycle (90:0) node {\NodeText}; \end{scope} } \end{scope} } \end{tikzpicture} \end{center} \end{document} 
0
4

I like the key system that TikZ comes with.

In the code below, three styles each get tried for the path:

  • every binom path = {k}{n}, binom path row n and binom path col k as well as
  • every binom node = {k}{n}, binom node row n and binom node col k.

This allows you define styles for specific rows or columns but also for each element specifically by testing #1 and #2 inside the every binom .... styles. In this case, we set binom node row 7, … 8 and … 9 so that the node contents is set empty and noone else can reset it again by disabling the node contents key.

I've also added an expandable eTeX evaluation of the binomial coefficient.

There's also the regular polygon node which we can use for drawing the hexagons but it doesn't make it much easier.

Code

\documentclass[tikz]{standalone}\usepackage{libertine} \newcommand*\binomialCoefficient[2]{% #1 = n, #2 = k \expanded{\noexpand\binomialCoefficientRec{\pgfintmin{#1-#2}{#2}}}{1}{#1}{1}} \newcommand*\binomialCoefficientRec[4]{% #1 = k, #2 = i, #3 = n, #4 = result \ifnum#2>#1 #4\else \expanded{\noexpand\binomialCoefficientRec{#1}{\pgfinteval{#2+1}}{#3} {\pgfintdivtruncate{#4*(#3+1-(#2))}{#2}}}\fi} \begin{document} \tikz[ thick, no text/.style={node contents=, node contents/.code=}, every binom node/.style 2 args={node contents={$\binomialCoefficient{#2}{#1}$}}, no binom text/.style={binom node row #1/.style={no text}}, no binom text/.list={7, 8, 9} ] \foreach \n in {0,..., 9} \foreach \k in {0, ..., \n} \draw[ shift={([y=(-120:1)]\k,\n)}, every binom path/.try={\k}{\n}, binom path row \n/.try, binom path col \k/.try] node[node contents=, every binom node/.try={\k}{\n}, binom node row \n/.try, binom node col \k/.try] plot[sharp cycle, samples at={0, ..., 5}](30+\x*60:sqrt 3/3); \end{document} 

Output

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.