不理想的手写解决方案

不理想的手写解决方案

我想自动将表格的某些列与 tikz 图片中的某些线条 y 坐标对齐,但不知道该怎么做。通过调整列宽和位置将表格移动到位显然是不可取的。

作为参考,这是我当前的输出 未对齐表格的图片

对于此代码

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

我希望它看起来像这样 近对齐表

图像中的垂直线位于\Delta f列的中央。

到目前为止,我发现的所有问题都是希望将整个表格与整个 tikz 图片对齐,而不是将其中的一部分对齐。

不理想的手写解决方案

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

手动对齐表格

通过手动进行一些测量和调整,我能够让这个案例完美对齐。但是,正如我之前所说,我不想单独调整每个案例。

尽管如此,手动进行这种调整还是揭示了其原理:

  • 我们给出了图形垂直线之间的水平距离
  • 测量应对齐的列之间的空间
  • 添加填充列,并赋予所需尺寸和测量尺寸之间的差值作为宽度
  • 移动节点来对齐整个表格

答案1

如果没有 [scale=2],这会容易得多。

这个想法是将 tikzmarks 放在未补偿的表格内,并使用它们来计算三个补偿长度。

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

对齐表格列

答案2

我认为最简单的方法是有三个表:

在此处输入图片描述

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

相关内容