Dave94705
2016-02-23 05:31:54 UTC
I wish to display Pascal's Triangle using tikz. While ultimately, I hope to pretty it up, for now I just want to have a lines showing connecting points with the appropriate numbers at each node. I use the tikz math library to calculate the numbers but I cannot figure out how to display them where I want (print command doesn't take specified position as far as I can see). I stole the idea for using a matrix \c from page 641 of the TikzAndPGF manual -- also tried it with the int \c; line commented out.
Below LaTeX code draws the connecting lines as expected, but displays the values of \y,\x rather than \c{\y,\x} as I wanted.
Any suggestions for how display \c{\y,\x} at desired location or other ideas to accomplish what I am (clearly?) trying to do would be appreciated. Thanks, ds
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
function binomCoef(\n,\i) {% don't use factorial ratios as individual factorials get too big too quickly
int \l,\m;
let \m = 1;
if \i == 0 then {return 1 \relax;};
if \i == \n then {return 1 \relax;};
for \l in {1,...,\i} then \m = (\m * \l)/(\i - \l);
return \m \relax;
};
int \c;
function fillBinomCoefs(\n){
int \i,\j;
for \i in {0,...,\n}
for \j in {0,...,\i}
\c{\i,\j} = binomCoef(\i,\j);
return 1;
};
}
%
\begin{tikzpicture}[scale=2, evaluate={int \n, \l; let \n = 2; let \l = fillBinomCoefs(\n);}]
\foreach \y in {0,...,\n}{
\foreach \x in {0,...,\y}{
\draw ({\y-2*\x},-\y) node {\Large \c{\y,\x}};
\draw ({\y-2*\x},-\y) -- +(-1,-1);
\draw ({\y-2*\x},-\y) -- +(+1,-1);
};
}
\end{tikzpicture}
\end{document}
Below LaTeX code draws the connecting lines as expected, but displays the values of \y,\x rather than \c{\y,\x} as I wanted.
Any suggestions for how display \c{\y,\x} at desired location or other ideas to accomplish what I am (clearly?) trying to do would be appreciated. Thanks, ds
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\tikzmath{
function binomCoef(\n,\i) {% don't use factorial ratios as individual factorials get too big too quickly
int \l,\m;
let \m = 1;
if \i == 0 then {return 1 \relax;};
if \i == \n then {return 1 \relax;};
for \l in {1,...,\i} then \m = (\m * \l)/(\i - \l);
return \m \relax;
};
int \c;
function fillBinomCoefs(\n){
int \i,\j;
for \i in {0,...,\n}
for \j in {0,...,\i}
\c{\i,\j} = binomCoef(\i,\j);
return 1;
};
}
%
\begin{tikzpicture}[scale=2, evaluate={int \n, \l; let \n = 2; let \l = fillBinomCoefs(\n);}]
\foreach \y in {0,...,\n}{
\foreach \x in {0,...,\y}{
\draw ({\y-2*\x},-\y) node {\Large \c{\y,\x}};
\draw ({\y-2*\x},-\y) -- +(-1,-1);
\draw ({\y-2*\x},-\y) -- +(+1,-1);
};
}
\end{tikzpicture}
\end{document}