[Tex/LaTex] Table of x-y values with first and second order differences

nodestablestikz-pgf

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

Best Answer

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} 
Related Question