我想修改这是帕斯卡三角形的例子允许底部有空白行供学生填写:
以下是我迄今为止尝试过的:
\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}
我能够删除颜色并\rows
为行数创建自定义变量。但是我不知道如何删除最后三行的文本。提前感谢您的帮助!
答案1
您需要根据以下值更改节点文本\newn
:
定义最后一行非空行:
\def\BlankRowAfter{6}
进而
\ifnum\newn>\BlankRowAfter\relax
\def\NodeText{}%
\else
\def\NodeText{\tiny $\mathbf{\binomialCoefficient{\newn}{\k}}$}%
\fi
如果需要则确定\NodeText
为空白。
代码:
\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}
答案2
我喜欢 TikZ 自带的按键系统。
在下面的代码中,针对路径分别尝试了三种样式:
every binom path = {k}{n}
以及 binom path row n
binom path col k
every binom node = {k}{n}
, 和 。binom node row n
binom node col k
这样,您就可以定义特定行或列的样式,也可以通过在样式中测试#1
和来为每个元素定义样式。在这种情况下,我们设置,和,以便将设置为空,并且其他人无法通过禁用键再次重置它。#2
every binom ....
binom node row 7
… 8
… 9
node contents
node contents
我还添加了二项式系数的可扩展 eTeX 评估。
我们还regular polygon
可以使用节点来绘制六边形,但这并不会使其变得更容易。
代码
\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}