四面体数表示创建具有 n 层的三角金字塔所需的半径相等的球的数量。第 n 个四面体数由公式 C(n + 2, 3) 给出。因此,四面体数在帕斯卡三角形中显示为对角线。我希望在帕斯卡三角形中突出显示这些数字。但是,我的尝试产生的是帕斯卡三角形,其中突出显示的数字单独显示为红色。我如何修改代码,以便四面体数在三角形本身中突出显示?
以下是代码,基于 Caramdir 对这个问题关于如何以数字形式表示帕斯卡三角形中的二项式系数:
\documentclass{article}
\usepackage{tikz}
%calculate binomial coefficients
\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}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\foreach \n in {0, ..., 7} {
\foreach \k in {0,...,\n} {
\node (\n\k) at (\k-\n/2,-\n) {\(\binomialcoefficient{\n}{\k}\)};
\foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
}
\pgfmathtruncatemacro{\x}{(\n+1)/2}
\pgfmathtruncatemacro{\y}{\n/2}
}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
\n
您的算法中的四面体数是通过指标与指标之间的差等于来识别\k
的3
。
因此,只需计算这个差异并按如下方式着色:
\pgfmathparse{int(\n-\k)}
\ifnum \pgfmathresult=3
\node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\else
\node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\fi
\documentclass{article}
\usepackage{tikz}
%calculate binomial coefficients
\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}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\foreach \n in {0, ...,7} {
\foreach \k in {0,...,\n} {
\pgfmathparse{int(\n-\k)}
\ifnum \pgfmathresult=3
\node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\else
\node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\fi
% \foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
}
\pgfmathtruncatemacro{\x}{(\n+1)/2}
\pgfmathtruncatemacro{\y}{\n/2}
}
\end{tikzpicture}
\end{figure}
\end{document}
答案2
AndreC 对我来说太快了 :) 只是一种直接评估的替代方法\x
,\y
直接\n-\k
在循环中评估并避免\pgfmathtruncatemacro
\documentclass{standalone}
\usepackage{tikz}
%calculate binomial coefficients
\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}
\begin{tikzpicture}
\foreach \n in {0, ..., 11}
{
\foreach [evaluate ={
\x = int(0.5*(\n+1));
\y = int(0.5*\n) ;
\NmK = int(\n-\k) ; %n minus k, NmK
}] \k in {0,...,\n}
{
\ifnum \NmK=3
\node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\else
\node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\fi
}
}
\end{tikzpicture}
\end{document}
答案3
AndreC 和 JeT 提出的优秀解决方案都突出显示了 C(n + 2, n - 1) 项,其中 n 至少为 1。由于我感兴趣的是突出显示 C(n + 2, 3) 项,同样 n 至少为 1,因此我修改了 AndreC 的代码以突出显示对角线,如下所示:
\documentclass{article}
\usepackage{tikz}
%calculate binomial coefficients
\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}
\begin{figure}[h]
\centering
\begin{tikzpicture}
\foreach \n in {0, ...,7} {
\foreach \k in {0,...,\n} {
\ifnum \k=3
\node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\else
\node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\fi
% \foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
}
\pgfmathtruncatemacro{\x}{(\n+1)/2}
\pgfmathtruncatemacro{\y}{\n/2}
}
\end{tikzpicture}
\end{figure}
\end{document}