I would like to typeset the top part of Pascal's triangle. To get the triangle with the names of the binomial coefficients, i.e., {n \choose k}
, I used the following code
\begin{tikzpicture}
\foreach \n in {0,...,4} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) {${\n \choose \k}$};
}
}
\end{tikzpicture}
The result is this
Now I want to be equally lazy and do something like this for the values of the binomial coefficients, i.e., replace {\n \choose \k}
in the node label with \CalculateBinomialCoefficient{\n}{\k}
where \CalculateBinomialCoefficient
is a hypothetical macro that calculates the binomial coefficient. Has anyone done something like that?
The result should look like this:
Best Answer
Here is a solution using TeX integer arithmetic. I am reusing counters defined by PGF in order to avoid having to declare new ones.
If you want, you can wrap
\pgfmathdeclarefunction
around that to have the function available in pgfmath (see Section 65 “Customizing the Mathematical Engine” in the manual (v2.10)).