表格按等号对齐

表格按等号对齐

我对 LaTeX 还很陌生,所以很抱歉问了个初学者的问题。我试图在表格中按等号对齐,但是有几行是空白的,没有等号。我该怎么做?谢谢!

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, adjustbox, mathtools}
\begin{document}

\begin{table}[htbp]
\begin{adjustbox}{width=\columnwidth,center}
  \begin{tabular}{|c|c|c|c|c|}
    \hline
    Force Field & Calculated Energy & Calculated Difference & Experimental Difference & Error \\
    \hline

    \multirow{4}{*}{OPLS-AA}
        & tt = 2.55447 & && \\ \cline{2-5}
        & $\text{g}^+\text{g}^+$ = 4.19785 & $\text{g}^+\text{g}^+$-tt = 1.64338 & $\text{g}^+\text{g}^+$-tt = 1.62 & 0.02338 \\ \cline{2-5}
        & $\text{g}^+\text{g}^-$ = 5.56306 & $\text{g}^+\text{g}^-$-tt = 3.00859 & $\text{g}^+\text{g}^-$-tt = 3.51 & -0.50141 \\ \cline{2-5}
        & tg = 3.36156 & tg-tt = 0.80709 & tg-tt = 0.86 & -0.05291 \\ \cline{2-5}
    \hline

    \multirow{4}{*}{AM1}
        & tt = -37.97281 & && \\ \cline{2-5}
        & $\text{g}^+\text{g}^+$ = -36.52659 & $\text{g}^+\text{g}^+$-tt = 1.44622 & $\text{g}^+\text{g}^+$-tt = 1.62 & -0.17378 \\ \cline{2-5}
        & $\text{g}^+\text{g}^-$ = -35.85939 & $\text{g}^+\text{g}^-$-tt = 2.11342 & $\text{g}^+\text{g}^-$-tt = 3.51 & -1.39658 \\ \cline{2-5}
        & tg = -37.25846 & tg-tt = 0.71435 & tg-tt = 0.86 & -0.14565 \\ \cline{2-5}
    \hline

    \multirow{4}{*}{PDDG/PM3}
        & tt = -34.61227 & && \\ \cline{2-5}
        & $\text{g}^+\text{g}^+$ = -33.94462 & $\text{g}^+\text{g}^+$-tt = 0.66765 & $\text{g}^+\text{g}^+$-tt = 1.62 & -0.95235 \\ \cline{2-5}
        & $\text{g}^+\text{g}^-$ = -33.29225 & $\text{g}^+\text{g}^-$-tt = 1.32002 & $\text{g}^+\text{g}^-$-tt = 3.51 & -2.18998 \\ \cline{2-5}
        & tg = -34.38819 & tg-tt = 0.22408 & tg-tt = 0.86 & -0.63592 \\ \cline{2-5}
    \hline
  \end{tabular}
\end{adjustbox}
\end{table}

\end{document}

表输出

答案1

这是一种方法。我将使用 的列拆分=为两列。为了获得正确的间距(并节省输入),我在列之间使用了。不幸的是,这意味着每次我不想出现 时@{$\null=\null$}都必须使用。\multicolumn=

剩下的唯一问题是,当标签太大时,多余的空间会添加到右半部分。为了解决这个问题,我计算了差值,并使用 向左半部分添加了额外的空间\makebox。现在等号居中了。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, adjustbox, mathtools}
\begin{document}

\begin{table}[htbp]
\begin{adjustbox}{width=\columnwidth,center}
  \sbox0{Calculated Difference}%
  \sbox1{Experimental Difference}%
  \sbox2{$\null=\null$}%
  \dimen0=\dimexpr \wd0 - \wd2\relax
  \dimen1=\dimexpr \wd1 - \wd2\relax
  \begin{tabular}{|c|r@{$\null=\null$}l|r@{$\null=\null$}l|r@{$\null=\null$}l|c|}
    \hline
    Force Field & \multicolumn{2}{c|}{Calculated Energy} & \multicolumn{2}{c|}{Calculated Difference} & \multicolumn{2}{c|}{Experimental Difference} & Error \\
    \hline
    \multirow{4}{*}{OPLS-AA}
        & tt & 2.55447 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & 4.19785 & \makebox[0.5\dimen0][r]{$\text{g}^+\text{g}^+$-tt} & 1.64338
          & \makebox[0.5\dimen1][r]{$\text{g}^+\text{g}^+$-tt} & 1.62 & 0.02338 \\ \cline{2-8}% note \makebox
        & $\text{g}^+\text{g}^-$ & 5.56306 & $\text{g}^+\text{g}^-$-tt & 3.00859 & $\text{g}^+\text{g}^-$-tt & 3.51 & -0.50141 \\ \cline{2-8}
        & tg & 3.36156 & tg-tt & 0.80709 & tg-tt & 0.86 & -0.05291 \\ \cline{2-8}
    \hline

    \multirow{4}{*}{AM1}
        & tt & -37.97281 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & -36.52659 & $\text{g}^+\text{g}^+$-tt & 1.44622 & $\text{g}^+\text{g}^+$-tt & 1.62 & -0.17378 \\ \cline{2-8}
        & $\text{g}^+\text{g}^-$ & -35.85939 & $\text{g}^+\text{g}^-$-tt & 2.11342 & $\text{g}^+\text{g}^-$-tt & 3.51 & -1.39658 \\ \cline{2-8}
        & tg & -37.25846 & tg-tt & 0.71435 & tg-tt & 0.86 & -0.14565 \\ \cline{2-8}
    \hline
    \multirow{4}{*}{PDDG/PM3}
        & tt & -34.61227 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & -33.94462 & $\text{g}^+\text{g}^+$-tt & 0.66765 & $\text{g}^+\text{g}^+$-tt & 1.62 & -0.95235 \\ \cline{2-8}
        & $\text{g}^+\text{g}^-$ & -33.29225 & $\text{g}^+\text{g}^-$-tt & 1.32002 & $\text{g}^+\text{g}^-$-tt & 3.51 & -2.18998 \\ \cline{2-8}
        & tg & -34.38819 & tg-tt & 0.22408 & tg-tt & 0.86 & -0.63592 \\ \cline{2-8}
    \hline
  \end{tabular}
\end{adjustbox}
\end{table}

\end{document}

演示


如果您确实想使字段居中...

注意:我主要使用表格来\sbox3展示如果你不确定哪个条目最大,你必须做什么。实际上,2.22222 和 2.22 已经足够接近了(2 通常是最宽的数字)。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow, adjustbox, mathtools}
\begin{document}

\begin{table}[htbp]
\begin{adjustbox}{width=\columnwidth,center}
  \sbox0{Calculated Difference}%
  \sbox1{$\null=\null$}%
  \sbox2{$\text{g}^+\text{g}^-$-tt}% left
  \sbox3{\begin{tabular}{@{}l@{}}
    1.64338\\
    3.00859\\
    0.80709\\
    1.44622\\
    2.11342\\
    0.71435\\
    0.66765\\
    1.32002\\
    0.22408
  \end{tabular}}% right
  \dimen0=\dimexpr \wd2 + \wd0 - \wd1 - \wd3\relax
  \sbox0{Experimental Difference}%
  \sbox3{\begin{tabular}{@{}l@{}}
    1.62\\
    3.51\\
    0.86
  \end{tabular}}% right
  \dimen1=\dimexpr \wd2 + \wd0 - \wd1 - \wd3\relax
  \begin{tabular}{|c|r@{$\null=\null$}l|r@{$\null=\null$}l|r@{$\null=\null$}l|c|}
    \hline
    Force Field & \multicolumn{2}{c|}{Calculated Energy} & \multicolumn{2}{c|}{Calculated Difference} & \multicolumn{2}{c|}{Experimental Difference} & Error \\
    \hline
    \multirow{4}{*}{OPLS-AA}
        & tt & 2.55447 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & 4.19785 & \makebox[0.5\dimen0][r]{$\text{g}^+\text{g}^+$-tt} & 1.64338
          & \makebox[0.5\dimen1][r]{$\text{g}^+\text{g}^+$-tt} & 1.62 & 0.02338 \\ \cline{2-8}% note \makebox
        & $\text{g}^+\text{g}^-$ & 5.56306 & $\text{g}^+\text{g}^-$-tt & 3.00859 & $\text{g}^+\text{g}^-$-tt & 3.51 & -0.50141 \\ \cline{2-8}
        & tg & 3.36156 & tg-tt & 0.80709 & tg-tt & 0.86 & -0.05291 \\ \cline{2-8}
    \hline

    \multirow{4}{*}{AM1}
        & tt & -37.97281 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & -36.52659 & $\text{g}^+\text{g}^+$-tt & 1.44622 & $\text{g}^+\text{g}^+$-tt & 1.62 & -0.17378 \\ \cline{2-8}
        & $\text{g}^+\text{g}^-$ & -35.85939 & $\text{g}^+\text{g}^-$-tt & 2.11342 & $\text{g}^+\text{g}^-$-tt & 3.51 & -1.39658 \\ \cline{2-8}
        & tg & -37.25846 & tg-tt & 0.71435 & tg-tt & 0.86 & -0.14565 \\ \cline{2-8}
    \hline
    \multirow{4}{*}{PDDG/PM3}
        & tt & -34.61227 & \multicolumn{2}{c|}{} & \multicolumn{2}{c|}{} & \\ \cline{2-8}
        & $\text{g}^+\text{g}^+$ & -33.94462 & $\text{g}^+\text{g}^+$-tt & 0.66765 & $\text{g}^+\text{g}^+$-tt & 1.62 & -0.95235 \\ \cline{2-8}
        & $\text{g}^+\text{g}^-$ & -33.29225 & $\text{g}^+\text{g}^-$-tt & 1.32002 & $\text{g}^+\text{g}^-$-tt & 3.51 & -2.18998 \\ \cline{2-8}
        & tg & -34.38819 & tg-tt & 0.22408 & tg-tt & 0.86 & -0.63592 \\ \cline{2-8}
    \hline
  \end{tabular}
\end{adjustbox}
\end{table}

\end{document}

演示2

相关内容