1

I'm pretty new to LaTeX, so sorry for the beginner question. I'm trying to align on equals signs within tabular, however a few rows are blank and don't have equals signs. How can I do this? Thanks!

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

Table Output

David
  • 13

1 Answers1

1

Here is one approach. I split the columns using = into two columns. To get the spacing right (and save typing) I used @{$\null=\null$} between the columns. Unfortunately, this meant having to use \multicolumn every time I didn't want a = to appear.

The only remaining problem was that when the label is too large, the extra space is added to the right half. To fix that, I calculated the difference and added extra space to the left half using \makebox. Now the equal signs are centered.

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

demo


If you really want to center the fields...

Note: I used a tabular in \sbox3 mostly to show what you have to do it you are not sure which entry is the largest. Actually, 2.22222 and 2.22 would be close enough (2 is usually the widest digit).

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

demo2

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Great! Is there a way to maintain the text being centered, like under the "Experimental Difference" column? (I see that you're splitting left/right on the equals sign, so I understand that might not be possible) – David Mar 22 '19 at 03:16
  • Possible, but you need to locate the largest item to the left of the = and the largest item to the right (not necessarily in the same row). I was hoping centering the = was good enough. After all, siunitx only centers the decimal point. – John Kormylo Mar 22 '19 at 03:25
  • Ah, I see. Thank you! I'll mark this as solved. – David Mar 22 '19 at 03:27
  • BTW, - and $-$ are significantly different symbols. Alas, I tend to forget that myself. – John Kormylo Mar 22 '19 at 16:41