在表格中对齐标题和数学

在表格中对齐标题和数学

我正在尝试将数学公式以表格形式排列在标牌上=。这种方法很好用,但我希望标题居中显示在数学公式上方。到目前为止,效果并不理想:

\documentclass{article}

\usepackage{amsmath}
\usepackage{booktabs}

\newcommand*{\pdiff}[2]{\mathop{}\frac{\partial #1}{\partial #2}}

\begin{document}
\begin{tabular}{c r @{\(=\)} l c c}
  \toprule
  Variable & Partial &&  Sensitivity coefficient & Contribution to error in g\\
  \midrule
  \(m_1\) & \(\pdiff{g}{m_1}\) & \(-\frac{4dm_2}{t^2(m_1-m_2)^2}\) & -409.7 &
    -0.4097\\
  \(m_2\) & \(\pdiff{g}{m_2}\) & \(\frac{4dm_1}{t^2(m_1-m_2)^2}\) & 441.8 &
    0.4418\\
  \(d\) & \(\pdiff{g}{d}\) & \(\frac{2(m_1+m_2)}{t^2(m_1-m_2)}\) & 5.889 &
    0.0177\\
  \(t\) & \(\pdiff{g}{t}\) & \(-\frac{4d(m_1+m_2)}{t^3(m_1-m_2)}\) & -5.677 &
    -2.2708\\
  \bottomrule
\end{tabular}
\end{document}

答案1

那这个呢?

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}

\newcommand\pdiff[2]{\frac{\partial #1}{\partial #2}}

\begin{document}
\begin{tabular}{c r @{\({}={}\)} l S S}
  \toprule
  \bfseries Variable & \multicolumn{2}{c}{\bfseries Partial} &  {\bfseries Sensitivity} & {\bfseries Contribution}
  \\
  &\multicolumn{2}{c}{} & {\bfseries coefficient} & {\bfseries to error in $g$}\\
  \midrule
  \(m_1\) & \(\pdiff{g}{m_1}\) & \(-\frac{4dm_2}{t^2(m_1-m_2)^2}\) & -409.7 &
    -0.4097\\
  \(m_2\) & \(\pdiff{g}{m_2}\) & \(\frac{4dm_1}{t^2(m_1-m_2)^2}\) & 441.8 &
    0.4418\\
  \(d\) & \(\pdiff{g}{d}\) & \(\frac{2(m_1+m_2)}{t^2(m_1-m_2)}\) & 5.889 &
    0.0177\\
  \(t\) & \(\pdiff{g}{t}\) & \(-\frac{4d(m_1+m_2)}{t^3(m_1-m_2)}\) & -5.677 &
    -2.2708\\
  \bottomrule
\end{tabular}

\end{document}

注意变化:

  • @{\(=\)}@{\({}={}\)}获得适当的间距
  • 使用多列
  • 粗体标题(非必需)
  • 用于数值数据的S列的使用siunitx
  • {...}在标题栏中使用S(有必要抑制 siunitx 格式)
  • 长列标题分为两行

答案2

在这里,我使用 TABstacks 设置每一列,这样我就可以得到各种对齐方式……不仅在方程式列中,而且在小数列中也是如此。此外,我将堆栈的基线跳跃设置为 2.5 \normalbaselineskip

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{tabstackengine}
\newcommand*{\pdiff}[2]{\mathop{}\dfrac{\partial #1}{\partial #2}}
\begin{document}
\setstackgap{L}{2.5\normalbaselineskip}
\stackMath
\setstackEOL{\cr}
\savestack\colA{\Longstack{m_1\cr m_2\cr d\cr t}}
\savestack\colB{\alignLongstack{%
\pdiff{g}{m_1} =& -\dfrac{4dm_2}{t^2(m_1-m_2)^2} \cr
\pdiff{g}{m_2} =& \dfrac{4dm_1}{t^2(m_1-m_2)^2} \cr
\pdiff{g}{d} =& \dfrac{2(m_1+m_2)}{t^2(m_1-m_2)} \cr
\pdiff{g}{t} =& -\dfrac{4d(m_1+m_2)}{t^3(m_1-m_2)}
}}
\savestack\colC{\alignLongstack{-409.&7\cr441.&9\cr5.&889\cr-5.&677}}
\savestack\colD{\alignLongstack{-0.&4097\cr0.&4418\cr0.&0177\cr-2.&2708}}
\begin{tabular}{c c c c}
  \toprule
  Variable & Partial &  Sensitivity & Contribution to \\
&& coefficient&error in g\\
  \midrule
\colA&\colB&\colC&\colD\\
  \bottomrule
\end{tabular}
\end{document}

在此处输入图片描述

答案3

对于我的练习和参见类似但略有不同的方法如何排版带有数学内容的表格:

\documentclass{article}
    \usepackage{amsmath}
    \usepackage{array,booktabs,makecell}
\renewcommand\theadfont{\bfseries}
    \usepackage{siunitx}

    \begin{document}
    \begin{center}% if table is in float environment, than replace with \centering
\begin{tabular}{>{$\displaystyle}c<{$} 
                >{$\displaystyle}r<{$} 
                    @{\;} 
                >{$\displaystyle}l<{$} S S}
    \toprule
\multicolumn{1}{c}{\thead{Variable}}  
    &   \multicolumn{2}{c}{\thead{Partial}} 
            &   {\thead{Sensitivity\\ 
                        coefficient}}  
                        &   {\thead{Contribution\\
                                   to error in $g$}} \\
  \midrule
m_1 &   \pardif{g}{m_1} =
        &   -\frac{4dm_2}{t^2(m_1-m_2)^2}             
            &   -409.7  &  -0.4097                  \\ 
    \addlinespace
m_2 & \pardif{g}{m_2} =
        &   \frac{4dm_1}{t^2(m_1-m_2)^2}
            & 441.8     &   0.4418                  \\
    \addlinespace
d   &   \pardif{g}{d} = 
        &   \frac{2(m_1+m_2)}{t^2(m_1-m_2)} 
            &   5.889   &   0.0177                  \\
    \addlinespace
t   &   \pardif{g}{t} = 
        &  -\frac{4d(m_1+m_2)}{t^3(m_1-m_2)} 
            & -5.677    &  -2.2708                  \\
  \bottomrule

\end{tabular}
    \end{center}
    \end{document}

对于列标题,我使用\theadfrommakecell并将前两个表格行合并为一行。我还改变了排版数学的方式,对我来说现在更漂亮了...我稍微简化了新的偏微分命令,所以我更改了它的名称。为了增加行之间的距离,我使用了\addlinespacefrombooktabs包。结果是:

在此处输入图片描述

相关内容