一阶和二阶差分 xy 值表

一阶和二阶差分 xy 值表

我想编写一页笔记,其布局如下图所示。我的问题是使用 LaTeX 执行此操作的最有效方法是什么。我认为 tikz 有很多节点和边,但我想知道在 LaTeX 中是否有更有效地使用表格的方法。

差异

答案1

你可以使用minipages将页面分解成各个组件。为了自动绘制,我使用了包裹collcell将 放置\tikzmark在所需位置,然后绘制适当的标记:

在此处输入图片描述

参考:

代码:

\documentclass{article}
\usepackage{showframe}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{collcell}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\ExtraSpaceH}{0.3em}% Extra horizontal space for red cells
\newcommand*{\ExtraSpaceF}{0.1em}% Extra horizontal space for blue cells

\newcounter{MarkCounterH} % Counter for uniquefying the \tikzmarks
\newcounter{MarkCounterF}
\newcommand{\TikzMarkPrefix}{}
\newcommand{\SetTikzMarkPrefix}[1]{%
    \setcounter{MarkCounterH}{0}%
    \setcounter{MarkCounterF}{0}%
    \xdef\TikzMarkPrefix{#1}%
}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}

\newcommand{\HalfShift}[1]{%
    \smash{%
        \raisebox{-1ex}{%
            \hspace*{\ExtraSpaceH}%
            \tikzmark{\TikzMarkPrefix-H-\arabic{MarkCounterH}}%
            \color{red}%
            \makebox[\widthof{$+99$}][r]{$#1$}%
        }%
        \stepcounter{MarkCounterH}%
    }%
}
\newcommand{\FullShift}[1]{%
    \smash{%
        \tikzmark{\TikzMarkPrefix-F-\arabic{MarkCounterF}}%
        \hspace*{\ExtraSpaceF}%
        \color{blue}%
        \makebox[\widthof{$+99$}][r]{$#1$}%
        \stepcounter{MarkCounterF}%
    }%
}

\newcommand{\ConnectRows}[3][]{%
    \foreach \x in {1,...,\numexpr\arabic{MarkCounter#3}-1\relax} {%
        \tikz[overlay,remember picture]
            \draw [red, ultra thick, #1]
                ($(#2-#3-\x)+(-0.8em,+1.7ex)$) --
                ($(#2-#3-\x)+(-0.1em,0.6ex)$) --
                ($(#2-#3-\x)+(-0.8em,-0.5ex)$);
    }%
}%

\newcommand{\DrawAxis}[1][]{%
    \begin{tikzpicture}[overlay,remember picture]
        \draw [#1]
            ($(current page.center)-(0.5\linewidth,0)$) --
            ($(current page.center)+(0.5\linewidth,0)$) ;
        \draw [#1]
            ($(current page.center)-(0,0.506\textheight)$) --
            ($(current page.center)+(0,0.496\textheight)$);
    \end{tikzpicture}%
}

\newcolumntype{F}{>{\collectcell\FullShift}{r}<{\endcollectcell}}
\newcolumntype{H}{>{\collectcell\HalfShift}{r}<{\endcollectcell}}

\newenvironment{MyMinipage}[2][t]{%
    \begin{minipage}[#1][0.5\textheight]{0.47\linewidth}\centering%
    \SetTikzMarkPrefix{#2}%
}{%
    \end{minipage}%
}%

\begin{document}
\begin{MyMinipage}{Quadrant2}  
    $\begin{array}{c | c H}
        \multicolumn{3}{c}{y=x} \\
        \toprule
        x & y & \multicolumn{1}{c}{\color{red}\Delta y}\\
        \hline
        0 & 0 & +1 \\
        1 & 1 & +1 \\
        2 & 2 & +1 \\
        3 & 3 & +1 \\
        4 & 4 & +1 \\
        5 & 5 & +1 \\
        6 & 6  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows{Quadrant2}{H}%
\end{MyMinipage}%
\hfill
\begin{MyMinipage}{Quadrant1}
    $\begin{array}{c | c H F}
        \multicolumn{4}{c}{y=x^2} \\
        \toprule
        x & y & \multicolumn{1}{c}{\color{red}\Delta y} & \multicolumn{1}{c}{\color{blue}\Delta^2 y}\\
        \hline
        0 &  0 & + 1 \\
        1 &  1 & + 3 & +2\\
        2 &  4 & + 5 & +2\\
        3 &  9 & + 7 & +2\\
        4 & 16 & + 9 & +2\\
        5 & 25 & +11 & +2\\
        6 & 36  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows[red]{Quadrant1}{H}%
    \ConnectRows[blue]{Quadrant1}{F}%
\end{MyMinipage}%
%
\DrawAxis[thick, gray]
%
\begin{MyMinipage}{Quadrant3}
$\begin{array}{c | c H}
    \multicolumn{3}{c}{y=x^3} \\
    \toprule
\end{array}$
\end{MyMinipage}%
\hfill
\begin{MyMinipage}{Quadrant4}
$\begin{array}{c | c H}
    \multicolumn{3}{c}{y=\sqrt{x}} \\
    \toprule
\end{array}$
\end{MyMinipage}%
\end{document} 

答案2

使用合适的循环可以实现计算的自动化。

注意:我已逐字复制Peter Grill 的回答,修改一些东西来获得更宽的列,但肯定有更好的方法来做这些修改。

我注意到表格必须有,代码的行数似乎相同Peter Grill 的回答才能正常工作。

至少编译两次。

连续差分

\documentclass{article}
%\usepackage{showframe}
\usepackage{mathtools}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{booktabs}
\usepackage{collcell}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{xinttools}% for \xintFor loop
\usepackage{xintexpr}% for computations, but xintexpr knows only sqrt beyond
                     % basic operations. On the other hand it computes
                     % (expandably) with
                     % exact precision and arbitrarily long numbers.

\newcommand*{\ExtraSpaceH}{0.3em}% Extra horizontal space for red cells
\newcommand*{\ExtraSpaceF}{0.1em}% Extra horizontal space for blue cells

\newcounter{MarkCounterH} % Counter for uniquefying the \tikzmarks
\newcounter{MarkCounterF}
\newcommand{\TikzMarkPrefix}{}
\newcommand{\SetTikzMarkPrefix}[1]{%
    \setcounter{MarkCounterH}{0}%
    \setcounter{MarkCounterF}{0}%
    \xdef\TikzMarkPrefix{#1}%
}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node[baseline] (#1) {};}

\newcommand{\HalfShift}[1]{%
    \smash{%
        \raisebox{-1ex}{%
            \hspace*{\ExtraSpaceH}%
            \tikzmark{\TikzMarkPrefix-H-\arabic{MarkCounterH}}%
            \color{red}%
            \makebox[\widthof{$+99999$}][r]{$#1$}%
        }%
        \stepcounter{MarkCounterH}%
    }%
}
\newcommand{\FullShift}[1]{%
    \smash{%
        \tikzmark{\TikzMarkPrefix-F-\arabic{MarkCounterF}}%
        \hspace*{\ExtraSpaceF}%
        \color{blue}%
        \makebox[\widthof{$+99999$}][r]{$#1$}%
        \stepcounter{MarkCounterF}%
    }%
}

\newcommand{\ConnectRows}[3][]{%
    \foreach \x in {1,...,\numexpr\arabic{MarkCounter#3}-1\relax} {%
        \tikz[overlay,remember picture]
            \draw [red, ultra thick, #1]
                ($(#2-#3-\x)+(-0.8em,+1.7ex)$) --
                ($(#2-#3-\x)+(-0.1em,0.6ex)$) --
                ($(#2-#3-\x)+(-0.8em,-0.5ex)$);
    }%
}%

\newcommand{\DrawAxis}[1][]{%
    \begin{tikzpicture}[overlay,remember picture]
        \draw [#1]
            ($(current page.center)-(0.5\linewidth,0)$) --
            ($(current page.center)+(0.5\linewidth,0)$) ;
        \draw [#1]
            ($(current page.center)-(0,0.506\textheight)$) --
            ($(current page.center)+(0,0.496\textheight)$);
    \end{tikzpicture}%
}

\newcolumntype{F}{>{\collectcell\FullShift}{r}<{\endcollectcell}}
\newcolumntype{H}{>{\collectcell\HalfShift}{r}<{\endcollectcell}}

\newenvironment{MyMinipage}[2][t]{%
    \begin{minipage}[#1][0.5\textheight]{0.47\linewidth}\centering%
    \SetTikzMarkPrefix{#2}%
}{%
    \end{minipage}%
}%

\begin{document}
\newcommand\Y[1]{(#1)}
\newcommand\DeltaOneY[1]{\Y{#1+1}-\Y{#1}}
\newcommand\DeltaTwoY[1]{\Y{#1+1}-2*\Y{#1}+\Y{#1-1}}

\begin{MyMinipage}{Quadrant2}  
    $\begin{array}{c | c H}
        \multicolumn{3}{c}{y=x} \\
        \toprule
        x & y & \multicolumn{1}{r}{\color{red}\Delta y}\\
        \hline
        \xintFor* #1 in {\xintSeq {0}{17}}
        \do { #1 & #1 &\edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax}%  
% \xinttheiiexpr as we know result and input are integers
% \temp is defined to avoid computing it twice
% but sign check and decision to add a + prefix 
% could be left to \num of sinunitx package for example
              \xintifSgn {\temp}{}{}{+}\temp \\}   
        18 & 18  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows{Quadrant2}{H}%
\end{MyMinipage}%
\hfill
\begin{MyMinipage}{Quadrant1}
    \renewcommand\Y[1]{(#1)^2}%
    $\begin{array}{c | c H F}
        \multicolumn{4}{c}{y=x^2} \\
        \toprule
        x & y & \multicolumn{1}{r}{\color{red}\Delta y} & \multicolumn{1}{r}{\color{blue}\Delta^2 y}\\
        \hline
        0 &  0 & + 1 \\
        \xintFor* #1 in {\xintSeq {1}{17}}
        \do { #1 & \xinttheiiexpr \Y{#1}\relax
                 &\edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp \\
        }% end of row loop
        18 &  \xinttheiiexpr \Y{18}\relax  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows[red]{Quadrant1}{H}%
    \ConnectRows[blue]{Quadrant1}{F}%
\end{MyMinipage}%
%
\DrawAxis[thick, gray]
%
\begin{MyMinipage}{Quadrant3}
    \renewcommand\Y[1]{(#1)^3}%
    $\begin{array}{c | c H F}
        \multicolumn{4}{c}{y=x^3} \\
        \toprule
        x & y & \multicolumn{1}{r}{\color{red}\Delta y} & \multicolumn{1}{r}{\color{blue}\Delta^2 y}\\
        \hline
        0 &  0 & + 1 \\
        \xintFor* #1 in {\xintSeq {1}{17}}
        \do { #1 & \xinttheiiexpr \Y{#1}\relax
                 &\edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp \\
        }% end of rowloop
        18 & \xinttheiiexpr \Y{18}\relax  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows[red]{Quadrant3}{H}%
    \ConnectRows[blue]{Quadrant3}{F}%
\end{MyMinipage}%
\hfill
\begin{MyMinipage}{Quadrant4}
    \renewcommand\Y[1]{sqrt(#1)}%
    $\begin{array}{c | c H F}
        \multicolumn{4}{c}{y=\sqrt{x}} \\
        \toprule
        x & y & \multicolumn{1}{r}{\color{red}\Delta y} & \multicolumn{1}{r}{\color{blue}\Delta^2 y}\\
        \hline
        0 &  0 & + 1 \\
        \xintFor* #1 in {\xintSeq {1}{17}}
        \do { #1 & \xinttheexpr round(\Y{#1},3)\relax 
                 &\edef\temp{\xinttheexpr round(\DeltaOneY{#1},3)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#1},3)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp \\
        }% end for row loop
        18 & \xinttheexpr round(\Y{18},3)\relax  \\
        \bottomrule
    \end{array}$\par
    \ConnectRows[red]{Quadrant4}{H}%
    \ConnectRows[blue]{Quadrant4}{F}%
\end{MyMinipage}%
\end{document} 

答案3

代码最初集中在连续行的部分自动创建,直至第四个差异。

没有使用 TikZ,在这次更新中,我进一步模拟了Peter Grill 的回答在 LaTeX 图片的帮助下定义列类型,并对其进行扩展pict2e(所有这些只是为了得到这个直角形状!\bm\rangle几乎一样好......)

我定义了一个\connector宏:我更愿意将它保存在保存框中,这样只需计算一次,但似乎我必须至少对每种使用的颜色都这样做。懒惰让我把代码留在了糟糕的状态,每次都要重新计算图片。我花了一些功夫让事物垂直居中。

第一行带有列标题,这让我有些头疼,不知道该把东西放在哪里:左对齐,还是居中?

以 0.1 为步长的逐次差分 sqrt

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\usepackage{array}
\usepackage{booktabs}
\usepackage{xinttools}% for \xintFor loop
\usepackage{xintexpr}% for computations, but xintexpr knows only sqrt beyond
                     % basic operations. On the other hand it computes
                     % (expandably) with
                     % exact precision and arbitrarily long numbers.
\usepackage{pict2e}
\usepackage{picture}% to use dimensional units in LaTeX pictures

\newsavebox{\cellbox}
\newlength{\cellheight}

% The \Connector is computed each time, which is a waste. 
% However I use various colors: do I need one save box per color ?
% \newsavebox{\connectbox}
% \newcommand\ResetConnectBox {%
%     \sbox{\connectbox}{% etc

\newcommand\Connector {%
    % unfortunately it seems one can not use TeX dimensions in polyline
    \setlength{\unitlength}{1sp}%
    \setlength{\cellheight}
     {\dimexpr\arraystretch\ht\strutbox+\arraystretch\dp\strutbox\relax }%
    \begin{picture}(\arraycolsep,\cellheight)
     \linethickness{2pt}\roundjoin
     \polyline 
       (0,\number\dimexpr0.9\cellheight\relax)
       (\number\dimexpr \arraycolsep\relax,\number\dimexpr .5\cellheight\relax)
       (0,\number\dimexpr .1\cellheight\relax)
    \end{picture}%
    \hspace{.5\arraycolsep}%
}


%% THIS IS FOR DRAWING THE CONNECTORS AND LOWERING THE VALUES
%% OF THE ODD SUCCESSIVE DIFFERENCES
\makeatletter
\newcolumntype{H}[1]{%
       >{\begin{lrbox}{\cellbox}$\color{#1}}%
       l%
       <{$\end{lrbox}%
   \smash{%
          \raisebox{\dimexpr-\height+\fontdimen22\textfont2\relax}
            {\makebox[0pt][r]{\color{#1}$\vcenter{\hbox{\Connector}}$}%
             \usebox{\cellbox}}%
          }}%
}

\newcolumntype{L}[1]{%
       >{\begin{lrbox}{\cellbox}$\color{#1}}%
       l%
       <{$\end{lrbox}%
          \makebox[0pt][r]{\color{#1}$\vcenter{\hbox{\Connector}}$}%
          \usebox{\cellbox}}%
}

\makeatother

\newcommand\Y[1]{(#1)}

% successive difference of a function of an integer variable (step=1)

\newcommand\DeltaOneY[1]{\Y{#1+1}-\Y{#1}}
\newcommand\DeltaTwoY[1]{\Y{#1+1}-2*\Y{#1}+\Y{#1-1}}
\newcommand\DeltaThreeY[1]{\Y{#1+2}-3*\Y{#1+1}+3*\Y{#1}-\Y{#1-1}}
\newcommand\DeltaFourY[1]{\Y{#1+2}-4*\Y{#1+1}+6*\Y{#1}-4*\Y{#1-1}+\Y{#1-2}}

\newcommand{\FourIntegerDeltas}[1]{%
         \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaThreeY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaFourY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\ThreeIntegerDeltas}[1]{%
                  \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaThreeY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\TwoIntegerDeltas}[1]{%
                  \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }


% optional argument is the nb of digits, default 4 (after decimal mark)
\newcommand{\FourFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaThreeY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaFourY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\ThreeFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaThreeY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\TwoFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }


\begin{document}\pagestyle{empty}


% \[\begin{array}{c|cH{red}}
%         \multicolumn{3}{c}{y=x} \\
%         \toprule
%         x & y & \multicolumn{1}{c}{\color{red}\Delta y}\\
%         \hline
%         \xintFor* #1 in {\xintSeq {0}{17}}
%         \do { #1 & #1 &\edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax}%  
% % \xinttheiiexpr as we know result and input are integers
% % \temp is defined to avoid computing it twice
% % but sign check and decision to add a + prefix 
% % could be left to \num of sinunitx package for example
%               \xintifSgn {\temp}{}{}{+}\temp \\}   
%         18 & 18  \\
%         \bottomrule
%   \end{array}\]


\[\renewcommand\Y[1]{(#1)^5}%
 \begin{array}{c|rH{red}L{blue}H{green}L{magenta}}
        \multicolumn{6}{c}{y=x^5} \\
        \toprule
        x & y & \multicolumn{1}{l}{\color{red}\Delta y} & 
                \multicolumn{1}{l}{\color{blue}\Delta^2 y} &
                \multicolumn{1}{l}{\color{green}\Delta^3 y} &
                \multicolumn{1}{l}{\color{magenta}\Delta^4 y} \\
        \hline
        0 &  0 & +1\\
        1 & \xinttheiiexpr \Y{1}\relax & \ThreeIntegerDeltas {1}\\
        \xintFor* #1 in {\xintSeq {2}{38}}
           \do { #1 &\xinttheiiexpr \Y{#1}\relax &\FourIntegerDeltas {#1}\\ }
        39 & \xinttheiiexpr \Y{39}\relax & \TwoIntegerDeltas {39}\\
        40 & \xinttheiiexpr \Y{40}\relax  \\
       \bottomrule
    \end{array}
\]

\[
\renewcommand\Y[1]{sqrt((#1)/10)}% x=#1/10
\begin{array}{c|rH{red}L{blue}H{magenta}L{green}}
        \multicolumn{6}{c}{y=\sqrt{x}} \\
        \toprule
        x & y & \multicolumn{1}{l}{\color{red}\Delta y} & 
                \multicolumn{1}{l}{\color{blue}\Delta^2 y} &
                \multicolumn{1}{l}{\color{magenta}\Delta^3 y} &
                \multicolumn{1}{l}{\color{green}\Delta^4 y} \\
        \hline
        0 &  0 & +\xinttheexpr round(\Y{1},6)\relax\\
        0.1 & \xinttheexpr round(\Y{1},6)\relax & \ThreeFixedPtDeltas[6]{1}\\
        \xintFor* #1 in {\xintSeq {2}{40}}\do
             {\xintTrunc{1}{#1/10} &\xinttheexpr round(\Y{#1},6)\relax 
                &\FourFixedPtDeltas [6]{#1}\\ }
        4.1 & \xinttheexpr round(\Y{41},6)\relax & \TwoFixedPtDeltas[6]{41}\\
        4.2 & \xinttheexpr round(\Y{42},6)\relax  \\
       \bottomrule
    \end{array}
\]

\end{document}

第一个版本:

连续差分 x^5

逐次差分 sqrt

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}
\usepackage{array}
\usepackage{booktabs}
\usepackage{xinttools}% for \xintFor loop
\usepackage{xintexpr}% for computations, but xintexpr knows only sqrt beyond
                     % basic operations. On the other hand it computes
                     % (expandably) with
                     % exact precision and arbitrarily long numbers.

\newsavebox{\cellbox}
\makeatletter
\newcolumntype{H}[1]{>{\begin{lrbox}{\cellbox}$\color{#1}\mathopen\rangle}l%
                  <{$\end{lrbox}%
                    \smash{\raisebox{-.5\ht\@arstrutbox}{\usebox{\cellbox}}}}}
\newcolumntype{L}[1]{>{\begin{lrbox}{\cellbox}$\color{#1}\mathopen\rangle}l%
                  <{$\end{lrbox}\usebox{\cellbox}}}

\makeatother

\newcommand\Y[1]{(#1)}

% successive difference of a function of an integer variable (step=1)

\newcommand\DeltaOneY[1]{\Y{#1+1}-\Y{#1}}
\newcommand\DeltaTwoY[1]{\Y{#1+1}-2*\Y{#1}+\Y{#1-1}}
\newcommand\DeltaThreeY[1]{\Y{#1+2}-3*\Y{#1+1}+3*\Y{#1}-\Y{#1-1}}
\newcommand\DeltaFourY[1]{\Y{#1+2}-4*\Y{#1+1}+6*\Y{#1}-4*\Y{#1-1}+\Y{#1-2}}

\newcommand{\FourIntegerDeltas}[1]{%
                  \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaThreeY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaFourY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\ThreeIntegerDeltas}[1]{%
                  \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaThreeY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\TwoIntegerDeltas}[1]{%
                  \edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheiiexpr \DeltaTwoY{#1}\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }


% optional argument is the nb of digits, default 4 (after decimal mark)
\newcommand{\FourFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaThreeY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaFourY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\ThreeFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaThreeY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }

\newcommand{\TwoFixedPtDeltas}[2][4]{%
                  \edef\temp{\xinttheexpr round(\DeltaOneY{#2},#1)\relax }%  
                  \xintifSgn {\temp}{}{}{+}\temp 
                 &\edef\temp{\xinttheexpr round(\DeltaTwoY{#2},#1)\relax }%
                  \xintifSgn {\temp}{}{}{+}\temp }


\begin{document}

% \[\begin{array}{c|cH{red}}
%         \multicolumn{3}{c}{y=x} \\
%         \toprule
%         x & y & \multicolumn{1}{c}{\color{red}\Delta y}\\
%         \hline
%         \xintFor* #1 in {\xintSeq {0}{17}}
%         \do { #1 & #1 &\edef\temp{\xinttheiiexpr \DeltaOneY{#1}\relax}%  
% % \xinttheiiexpr as we know result and input are integers
% % \temp is defined to avoid computing it twice
% % but sign check and decision to add a + prefix 
% % could be left to \num of sinunitx package for example
%               \xintifSgn {\temp}{}{}{+}\temp \\}   
%         18 & 18  \\
%         \bottomrule
%   \end{array}\]


\[\renewcommand\Y[1]{(#1)^5}%
 \begin{array}{c|cH{red}L{blue}H{green}L{magenta}}
        \multicolumn{6}{c}{y=x^5} \\
        \toprule
        x & y & \multicolumn{1}{l}{\color{red}\Delta y} & 
                \multicolumn{1}{l}{\color{blue}\Delta^2 y} &
                \multicolumn{1}{l}{\color{green}\Delta^3 y} &
                \multicolumn{1}{l}{\color{magenta}\Delta^4 y} \\
        \hline
        0 &  0 & +1\\
        1 & \xinttheiiexpr \Y{1}\relax & \ThreeIntegerDeltas {1}\\
        \xintFor* #1 in {\xintSeq {2}{38}}
           \do { #1 &\xinttheiiexpr \Y{#1}\relax &\FourIntegerDeltas {#1}\\ }
        39 & \xinttheiiexpr \Y{39}\relax & \TwoIntegerDeltas {39}\\
        40 & \xinttheiiexpr \Y{40}\relax  \\
       \bottomrule
    \end{array}
\]

\[
\renewcommand\Y[1]{sqrt((#1)/10)}% x=#1/10
\begin{array}{c|cH{red}L{blue}H{magenta}L{green}}
        \multicolumn{6}{c}{y=\sqrt{x}} \\
        \toprule
        x & y & \multicolumn{1}{l}{\color{red}\Delta y} & 
                \multicolumn{1}{l}{\color{blue}\Delta^2 y} &
                \multicolumn{1}{l}{\color{magenta}\Delta^3 y} &
                \multicolumn{1}{l}{\color{green}\Delta^4 y} \\
        \hline
        0 &  0 & +\xinttheexpr round(\Y{1},6)\relax\\
        0.1 & \xinttheexpr round(\Y{1},6)\relax & \ThreeFixedPtDeltas[6]{1}\\
        \xintFor* #1 in {\xintSeq {2}{40}}\do
             {\xintTrunc{1}{#1/10} &\xinttheexpr round(\Y{#1},6)\relax 
                &\FourFixedPtDeltas [6]{#1}\\ }
        4.1 & \xinttheexpr round(\Y{41},6)\relax & \TwoFixedPtDeltas[6]{41}\\
        4.2 & \xinttheexpr round(\Y{42},6)\relax  \\
       \bottomrule
    \end{array}
\]

\end{document}

相关内容