5

I would like to code a pages of notes that has the layout shown in the picture below. My question is the most efficient way to do this with LaTeX. I would think tikz with lots of nodes and edges, but I wonder if there is a more efficiently using tables within LaTeX.

differences

1
  • Does anyone know how to do this such that the arrows and numbers for the first differences are BESIDE the table (rather than in the table)? Commented Dec 8, 2023 at 23:27

3 Answers 3

7

You could just use minipages to break up the page into its components. To automate the drawing, I used the collcell package to place a \tikzmark at the desired location and then draw the appropriate markers:

enter image description here

References:

Code:

\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} 
3
  • Awesome answer @Peter Grill....... Commented May 25, 2014 at 7:12
  • Wonderful, thank you! Is it possible to accept both this and @jfbu's final answer? Commented May 25, 2014 at 17:16
  • @AbbasJaffary: Unfortunately you can only accept one answer. And which one you accept is really up to you. I won't be offended if you don't accept mine, so feel free to accept the best one for you. Commented May 25, 2014 at 21:21
4

using a suitable loop it is possible to automatize the computations.

Nota bene: I have copied verbatim Peter Grill's answer, modifiying some things to get wider columns, but there is surely a better way to do these modifications.

I have noticed that the tables must have, it seems the same number of rows for the code from Peter Grill's answer to work correctly.

Compile at least twice.

successive differences

\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
  • in this answer, I did not consider at all the OP's question about the most efficient way; the most efficient way would not use TikZ and all the tikzmarks. But here I only wanted to extend the already given answer by Peter Grill to automatic computations of rows.
    – user4686
    Commented May 25, 2014 at 11:33
  • all computations done by xintexpr should be with the math engine of your choice (expandability is not need here anyhow).
    – user4686
    Commented May 25, 2014 at 11:35
  • in the case of square root the column of second differences is computed with 16 digits precision before rounding: it is not the first differences of the already rounded first differences (as one can guess from the last cell at the bottom right)
    – user4686
    Commented May 25, 2014 at 11:43
2

The code concentrated initially on a partially automatized creation of the successive rows, up to the fourth differences.

No TikZ is used, and in this update I go a bit a further emulating the look as in the Peter Grill's answer in defining column types with the help of LaTeX pictures, as extended by pict2e (all that just to get this right angle shape! \bm\rangle would have been almost as fine...)

I define a \connector macro: I would prefer to have it in a savebox hence compute it only once, but it seems I must do that at least for each used color. Laziness made me leave the code in its deplorable state which computes each time the picture anew. I went to some lengths to have nice vertical centering of things.

The first line with the column headers causes me some headache about where to put things: flush left, centered?

successive differences of sqrt by step of .1

\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}

first version:

successive differences x^5

successive differences 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}
2
  • This has the exact layout of what I was looking for. Thank you. I would center the column headings. Commented May 25, 2014 at 17:13
  • @AbbasJaffary I have a variant which computes only once the "connector" boxes, one for each colour. Then they are used in the tabular specification simply as @{\usebox{connectboxred} for example. However it is a bit painful to remove them in places where they should not appear.
    – user4686
    Commented May 25, 2014 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .