1

I want to align some columns of a table automatically with some lines y-coordinates in a tikz picture and have no idea how to go about it. Moving the table into place via tweaking column widths and positions is obviously undesirable.

For reference, this is my current output Picture with unaligned table

for this code

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[scale=2, declare function={poly(\x) = 212/424 * (424 + 52 * \x - 854 * \x ^ 2 + 613 * \x ^ 3 - 126 * \x ^ 4);}]
  \draw[domain=-3:-2] plot (\x,0);
  \newcommand{\plotScale}{1/212}
  \node at (-2.5,-0.25) {\tiny$f_0(x) = 0$};
  \draw[domain=-2:0, samples=100] plot ({\x}, { \plotScale * poly(-\x)});
  \node at (-1,-0.25) {\tiny{$f_1(x) =\begin{aligned} 212 - 26 x - 427 x ^ 2 \\ - \frac{613}{3} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=0:2, samples=100] plot ({\x},{ \plotScale * poly(\x)});
  \node at (1,-0.25) {\tiny{$f_2(x) = \begin{aligned}212 + 26 x - 427 x ^ 2 \\ + \frac{613}{2} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=2:3] plot (\x,0);
  \node at (2.5,-0.25) {\tiny$f_3(x) = 0$};
  \newcommand{\verticalLine}[1]{\draw (#1,{ \plotScale * poly(abs(#1)) }) -- (#1,-0.5);}
  \verticalLine{-2}
  \verticalLine{0}
  \verticalLine{2}

  \node at (0,-1) {\tiny{
  \begin{tabular}{c|c@{$+$}c@{$=$}c|c@{$+$}c@{$=$}c|c@{$+$}c@{$=$}c}
   & \multicolumn{3}{c}{action point 0 at $x=-2$} & \multicolumn{3}{c}{action point 1 at $x=0$} & \multicolumn{3}{c}{action point 2 at $x=2$} \\
   $k$ & $f_0^{(k)}(-2)$ & $\Delta f_0^{(k)} $ & $f_1^{(k)}(-2)$ & $f_1^{(k)}(0)$ & $\Delta f_1^{(k)} $ & $f_2^{(k)}(0)$ & $f_2^{(k)}(2)$ & $\Delta f_2^{(k)} $ & $f_3^{(k)}(2)$ \\
   \hline
   0 & 0 & 0 & 0 & 212 & 0 & 212 & 0 & 0 & 0 \\
   1 & 0 & 20 & 20 & -26 & 52 & 26 & -20 & 20 & 0\\
   2 & 0 & -200 & -200 & -854 & 0 & -854 & -200 & 200 & 0\\
   3 & 0 & 1185 & 1185 & -1839 & 3678 & 1839 & -1185 & 1185 & 0 \\
   4 & 0 & -1512 & -1512 & -1512 & 0 & -1512 & -1512 & 1512 & 0\\
  \end{tabular}}};
\end{tikzpicture}
\end{document}

and I want it to look like this Nearl aligned table

where the vertical lines from the image are centered above the \Delta f columns.

So far, all questions that I could find, wanted to align whole tables with whole tikz pictures and not parts of them.

Undesirable handwritten solution

  \node at (-0.225,-1) {\tiny{
  \begin{tabular}{c|c@{$+$}c@{$=$}cp{0.35cm}c@{$+$}c@{$=$}cp{0.55cm}c@{$+$}c@{$=$}c}
   & \multicolumn{3}{c}{action point 0 at $x=-2$} && \multicolumn{3}{c}{action point 1 at $x=0$} && \multicolumn{3}{c}{action point 2 at $x=2$} \\
   $k$ & $f_0^{(k)}(-2)$ & $\Delta f_0^{(k)} $ & $f_1^{(k)}(-2)$ && $f_1^{(k)}(0)$ & $\Delta f_1^{(k)} $ & $f_2^{(k)}(0)$ && $f_2^{(k)}(2)$ & $\Delta f_2^{(k)} $ & $f_3^{(k)}(2)$ \\
   \hline
   0 & 0 & 0 & 0 && 212 & 0 & 212 && 0 & 0 & 0 \\
   1 & 0 & 20 & 20 && -26 & 52 & 26 && -20 & 20 & 0\\
   2 & 0 & -200 & -200 && -854 & 0 & -854 && -200 & 200 & 0\\
   3 & 0 & 1185 & 1185 && -1839 & 3678 & 1839 && -1185 & 1185 & 0 \\
   4 & 0 & -1512 & -1512 && -1512 & 0 & -1512 && -1512 & 1512 & 0\\
  \end{tabular}}};

Manually aligned table

Doing some measurements and tweaks by hand I was able to get perfect alignment for this case. However, as I said earlier I don't want to tweak each case on its own.

Nevertheless, doing this tweaking by hand showed the mechanics:

  • we are given the horizontal distances between the graphics vertical lines
  • measure the space between the columns that should be aligned
  • add filler columns and give them the difference between wanted and measured size as width
  • shift the node to align the whole table
3

This would have been a lot easier without the [scale=2].

The idea is to put tikzmarks inside an uncompensated tabular and use them to compute the three compensation lengths.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\newlength{\firstcol}
\newlength{\secondcol}
\newlength{\thirdcol}

\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}[remember picture,scale=2,declare function={poly(\x) = 212/424 * (424 + 52 * \x - 854 * \x ^ 2 + 613 * \x ^ 3 - 126 * \x ^ 4);}]
  \draw[domain=-3:-2] plot (\x,0);
  \newcommand{\plotScale}{1/212}
  \node at (-2.5,-0.25) {\tiny$f_0(x) = 0$};
  \draw[domain=-2:0, samples=100] plot ({\x}, { \plotScale * poly(-\x)});
  \node at (-1,-0.25) {\tiny{$f_1(x) =\begin{aligned} 212 - 26 x - 427 x ^ 2 \\ - \frac{613}{3} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=0:2, samples=100] plot ({\x},{ \plotScale * poly(\x)});
  \node at (1,-0.25) {\tiny{$f_2(x) = \begin{aligned}212 + 26 x - 427 x ^ 2 \\ + \frac{613}{2} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=2:3] plot (\x,0);
  \node at (2.5,-0.25) {\tiny$f_3(x) = 0$};
  \newcommand{\verticalLine}[1]{\draw (#1,{ \plotScale * poly(abs(#1)) }) -- (#1,-0.5);}
  \verticalLine{-2}
  \verticalLine{0}
  \verticalLine{2}

%trial run
  \coordinate (start) at (-3,-.5);
  \node[below right,opacity=0] at (start){\tiny{%
  \begin{tabular}{c|c@{$+$}c@{$=$}c|c@{$+$}c@{$=$}c|c@{$+$}c@{$=$}c}
   & \multicolumn{3}{c}{action point 0 at $x=-2$} & \multicolumn{3}{c}{action point 1 at $x=0$} & \multicolumn{3}{c}{action point 2 at $x=2$} \\
   $k$ & $f_0^{(k)}(-2)$ & $\Delta f_0^{(k)} $ & $f_1^{(k)}(-2)$ & $f_1^{(k)}(0)$ & $\Delta f_1^{(k)} $ & $f_2^{(k)}(0)$ & $f_2^{(k)}(2)$ & $\Delta f_2^{(k)} $ & $f_3^{(k)}(2)$ \\
   \hline
   &&\tikzmark{first}&&&\tikzmark{second}&&&\tikzmark{third}
  \end{tabular}}};

%desired column locations [scale=2]
  \coordinate (A) at (-2,-0.5);
  \coordinate (B) at (0,-0.5);
  \coordinate (C) at (2,-0.5);

\begin{scope}[scale=0.5]% compensate for [scale=2]
  \coordinate (D) at (pic cs:first);
  \coordinate (E) at (pic cs:second);
  \coordinate (F) at (pic cs:third);
\end{scope}

  \pgfextractx{\firstcol}{\pgfpointdiff{\pgfpointanchor{D}{center}}{\pgfpointanchor{A}{center}}}%
  \pgfextractx{\secondcol}{\pgfpointdiff{\pgfpointanchor{E}{center}}{\pgfpointanchor{B}{center}}}%
  \pgfextractx{\thirdcol}{\pgfpointdiff{\pgfpointanchor{F}{center}}{\pgfpointanchor{C}{center}}}%
  \advance \thirdcol by -\secondcol
  \advance \secondcol by -\firstcol

  \node[below right] at ($(start) + (\firstcol,0)$) {\tiny{%
  \begin{tabular}{c|c@{$+$}c@{$=$}cp{\secondcol}c@{$+$}c@{$=$}cp{\thirdcol}c@{$+$}c@{$=$}c}
   & \multicolumn{3}{c}{action point 0 at $x=-2$} && \multicolumn{3}{c}{action point 1 at $x=0$} && \multicolumn{3}{c}{action point 2 at $x=2$} \\
   $k$ & $f_0^{(k)}(-2)$ & $\Delta f_0^{(k)} $ & $f_1^{(k)}(-2)$ && $f_1^{(k)}(0)$ & $\Delta f_1^{(k)} $ & $f_2^{(k)}(0)$ && $f_2^{(k)}(2)$ & $\Delta f_2^{(k)} $ & $f_3^{(k)}(2)$ \\[2pt]
   \hline
   0 & 0 & 0 & 0 && 212 & 0 & 212 && 0 & 0 & 0 \\
   1 & 0 & 20 & 20 && -26 & 52 & 26 && -20 & 20 & 0\\
   2 & 0 & -200 & -200 && -854 & 0 & -854 && -200 & 200 & 0\\
   3 & 0 & 1185 & 1185 && -1839 & 3678 & 1839 && -1185 & 1185 & 0 \\
   4 & 0 & -1512 & -1512 && -1512 & 0 & -1512 && -1512 & 1512 & 0\\
  \end{tabular}}};  

\end{tikzpicture}
\end{document}

align tabular columns

  • What exactly would have been easier without the scale? – Nobody Jun 15 '15 at 14:18
  • The pic cs: coordinates would have been in the right location. The fix is simple enough. Finding the fix took a while. – John Kormylo Jun 16 '15 at 2:36
  • Although only a minor point: The alignment does not work on the first run of LaTex (where no .aux file is present). Anyways +1 for the solution. – Nobody Jun 17 '15 at 8:12
1

Easiest I think to have three tables:

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[scale=2, declare function={poly(\x) = 212/424 * (424 + 52 * \x - 854 * \x ^ 2 + 613 * \x ^ 3 - 126 * \x ^ 4);}]
  \draw[domain=-3:-2] plot (\x,0);
  \newcommand{\plotScale}{1/212}
  \node at (-2.5,-0.25) {\tiny$f_0(x) = 0$};
  \draw[domain=-2:0, samples=100] plot ({\x}, { \plotScale * poly(-\x)});
  \node at (-1,-0.25) {\tiny{$f_1(x) =\begin{aligned} 212 - 26 x - 427 x ^ 2 \\ - \frac{613}{3} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=0:2, samples=100] plot ({\x},{ \plotScale * poly(\x)});
  \node at (1,-0.25) {\tiny{$f_2(x) = \begin{aligned}212 + 26 x - 427 x ^ 2 \\ + \frac{613}{2} x ^ 3 - 63 x ^ 4\end{aligned}$}};
  \draw[domain=2:3] plot (\x,0);
  \node at (2.5,-0.25) {\tiny$f_3(x) = 0$};
  \newcommand{\verticalLine}[1]{\draw (#1,{ \plotScale * poly(abs(#1)) }) -- (#1,-0.5);}
  \verticalLine{-2}
  \verticalLine{0}
  \verticalLine{2}

  \node at (-2,-1) {\tiny
  \begin{tabular}{c|c@{$+$}c@{$=$}c|}
   & \multicolumn{3}{c}{action point 0 at $x=-2$}\\
   $k$ & $f_0^{(k)}(-2)$ & $\Delta f_0^{(k)} $ & $f_1^{(k)}(-2)$ \\
   \hline
   0 & 0 & 0 & 0 \\
   1 & 0 & 20 & 20 \\
   2 & 0 & -200 & -200 \\
   3 & 0 & 1185 & 1185 \\
   4 & 0 & -1512 & -1512 \\
  \end{tabular}};
  \node at (0,-1) {\tiny
  \begin{tabular}{c|c@{$+$}c@{$=$}c|}
   & \multicolumn{3}{c}{action point 1 at $x=0$} \\
   $k$ & $f_1^{(k)}(0)$ & $\Delta f_1^{(k)} $ & $f_2^{(k)}(0)$ \\
   \hline
   0 &  212 & 0 & 212 \\
   1 & -26 & 52 & 26 \\
   2 & -854 & 0 & -854 \\
   3 & -1839 & 3678 & 1839 \\
   4 & -1512 & 0 & -1512 \\
  \end{tabular}};
  \node at (2,-1) {\tiny
  \begin{tabular}{c|c@{$+$}c@{$=$}c}
   & \multicolumn{3}{c}{action point 2 at $x=2$} \\
   $k$ & $f_2^{(k)}(2)$ & $\Delta f_2^{(k)} $ & $f_3^{(k)}(2)$ \\
   \hline
   0 & 0 & 0 & 0 \\
   1 & -20 & 20 & 0\\
   2 & -200 & 200 & 0\\
   3 & -1185 & 1185 & 0 \\
   4 & -1512 & 1512 & 0\\
  \end{tabular}};
\end{tikzpicture}
\end{document}
  • I started out with this solution but I dislike the repeated k column and it still does not solve the automatic alignment problem. As I said, I don't want to shift the tables around by hand until they fit. – Nobody Jun 14 '15 at 11:59
  • @Nobody yes I was going to suggest dropping the k column, I'm not sure what you mean by "by hand" here: the placement is automatic, in that the coordinates used for the tables are just the coordinates you used for the labels in the plot, namely -2, 0 and 2. – David Carlisle Jun 14 '15 at 12:04
  • The given coordinates align the node but not the table. My goal is to have the center of the \Delta f columns exactly below the vertical lines, which is not the case in your solution. With automatic I mean to tell LaTeX to achieve exactly this: center column 3 below coordinate (-2,0). Ultimately, this should provide proper alignment even after changing some of the coordinates without me needing to realign. – Nobody Jun 14 '15 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.