答案1
含钛钾Z 很容易创建一些计算条目的东西。这个答案定义了这样的图片。你需要说的是
\begin{tikzpicture}
\pic{difftable={0,1,3,5}{3,2,4,-1}};
\end{tikzpicture}
其中第一个列表包含,xi
第二个列表包含fi
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,patterns}
\usepackage{eqparbox}
\newbox\eqnodebox
\tikzset{equal size/.style={execute at begin
node={\setbox\eqnodebox=\hbox\bgroup},
execute at end node={\egroup\eqmakebox[#1][c]{\copy\eqnodebox}}},
equal size/.default=A,}
\newcounter{difftable}
\makeatletter
\pgfmathdeclarefunction{Dim}{1}{%
\begingroup%
\pgfutil@tempcnta0%
\@for\pgfutil@tempa:=#1\do{\advance\pgfutil@tempcnta1}%
\edef\pgfmathresult{\the\pgfutil@tempcnta}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}
\pgfmathdeclarefunction{diffquots}{2}{%
\begingroup%
\pgfutil@tempcnta1%
\pgfmathtruncatemacro{\pgfutil@tempa}{min(Dim("#1"),Dim("#2"))}%\typeout{dim=\pgfutil@tempa}%
\pgfutil@loop
\pgfmathsetmacro{\pgfutil@tempb}{{#1}[\the\pgfutil@tempcnta]-{#1}[\the\numexpr\pgfutil@tempcnta-1]}%
\pgfmathsetmacro{\pgfutil@tempc}{{#2}[\the\pgfutil@tempcnta]-{#2}[\the\numexpr\pgfutil@tempcnta-1]}%
\ifdim\pgfutil@tempc pt=0pt\relax
\tikzerror{You asked me to divide by zero. Terminating.}%
\fi
\pgfmathsetmacro{\pgfutil@tempd}{\pgfutil@tempb/\pgfutil@tempc}%
\pgfmathsetmacro{\pgfutil@tempe}{{#2}[\the\pgfutil@tempcnta]/2+{#2}[\the\numexpr\pgfutil@tempcnta-1]/2}%
\ifnum\pgfutil@tempcnta=1\relax
\edef\pgfutil@tempf{\pgfutil@tempd}%
\edef\pgfutil@tempg{\pgfutil@tempe}%
\else
\edef\pgfutil@tempf{\pgfutil@tempf,\pgfutil@tempd}%
\edef\pgfutil@tempg{\pgfutil@tempg,\pgfutil@tempe}%
\fi
\advance\pgfutil@tempcnta1%
\ifnum\pgfutil@tempcnta<\the\numexpr\pgfutil@tempa-1%
\pgfutil@repeat
\edef\pgfmathresult{"\pgfutil@tempf","\pgfutil@tempg"}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\makeatother
\tikzset{result/.style={draw,text height=1em,text depth=0.35ex,
inner xsep=0.5ex,align=center,equal size=\pgfkeysvalueof{/tikz/difftable/id}-#1},
pics/difftable/.style 2 args={code={\stepcounter{difftable}
\tikzset{difftable/id=\number\value{difftable}}
\node[result=1] (xi-1) {$x_i$}; %
\path foreach \X [count=\Y] in {#1}
{node[result=1,below={-\pgflinewidth} of xi-\Y,alias=xi-last]
(xi-\the\numexpr\Y+1) {\pgfmathprintnumber{\X}}};
%
\node[result=2,right={-\pgflinewidth} of xi-1] (fi-1) {$f(x_i)$};
\path foreach \X [count=\Y] in {#2}
{node[result=2,below={-\pgflinewidth} of fi-\Y,alias=fi-last] (fi-\the\numexpr\Y+1)
{\pgfmathprintnumber{\X}}};
%
\pgfmathsetmacro{\myresultA}{diffquots("#1","#2")}
\pgfmathsetmacro{\lstdfi}{{\myresultA}[0]}
\pgfmathsetmacro{\lstdxi}{{\myresultA}[1]}
\node[result=3,right={-\pgflinewidth} of fi-1] (dfi-1) {$DD1$};
\path foreach \X [count=\Y starting from 2] in \lstdfi
{node[result=3,alias=dfi-last] (dfi-\Y) at
(dfi-1|-fi-\Y.south east) {\pgfmathprintnumber{\X}}};
%
\pgfmathsetmacro{\myresultB}{diffquots("\lstdfi","\lstdxi")}
\pgfmathsetmacro{\lstddfi}{{\myresultB}[0]}
\pgfmathsetmacro{\lstddxi}{{\myresultB}[1]}
\node[result=4,right={-\pgflinewidth} of dfi-1] (ddfi-1) {$DD2$};
\path foreach \X [count=\Y starting from 2] in \lstddfi
{node[result=4,alias=ddfi-last] (ddfi-\Y) at
(ddfi-1|-dfi-\Y.south east) {\pgfmathprintnumber{\X}}};
%
\pgfmathsetmacro{\myresultC}{diffquots("\lstddfi","\lstddxi")}
\pgfmathsetmacro{\lstdddfi}{{\myresultC}[0]}
\pgfmathsetmacro{\lstdddxi}{{\myresultC}[1]}
\node[result=5,right={-\pgflinewidth} of ddfi-1] (dddfi-1) {$DD3$};
\path foreach \X [count=\Y starting from 2] in \lstdddfi
{node[result=5,alias=dddfi-last] (dddfi-\Y) at
(dddfi-1|-ddfi-\Y.south east) {\pgfmathprintnumber{\X}}};
%
\path[pattern=north east lines] (fi-2.north east)
foreach \X in {d,dd,ddd}
{|- (\X fi-2.north east) }
|- cycle
(fi-last.south east)
foreach \X in {d,dd,ddd}
{|- (\X fi-last.south east) }
|- cycle;
\draw[thick] (xi-1.north west) rectangle (xi-last.south-|dddfi-1.east);
}},
difftable/.cd,id/.initial=0}
\begin{document}
\begin{tikzpicture}
\pic{difftable={0,1,3,5}{3,2,4,-1}};
\end{tikzpicture}
\bigskip
\begin{tikzpicture}
\pic{difftable={0,1,3,5}{2,5,-3,1}};
\end{tikzpicture}
\end{document}