Tables – Why is My Table Flowing Over into the Right Margin and How to Fix It

centermarginsoverflowtables

I have the following code which defines a table in a paper I'm writing:

\begin{table}[t]
    \centering
    \def\arraystretch{1.5}
    \begin{tabular}{ *{5}{>{\centering\arraybackslash}m{0.2\textwidth}} }
    \hline \hline
    Atom Pair & \ch{D0} (eV) & $\alpha$ (1/\AA) & \ch{r0} (\AA) & Cut Off (\AA) \\
    \hline
    Fe-Fe & 0.764 & 1.5995 & 2.7361 & 12 \\
    Fe-Ti & 0.8162 & 1.448 & 2.914 & 12 \\
    Fe-Nd & 0.6036 & 1.6458 & 3.188 & 12 \\
    Nd-Nd & 0.312 & 0.945 & 4.092 & 12 \\
    Nd-Ti & 0.4964 & 1.440118 & 3.4309 & 12 \\
    Ti-Ti & 0.6540 & 1.2118 & 3.3476 & 12 \\
    Sm-Sm & 0.2365 & 1.16433 & 3.8485 & 12 \\
    Sm-Ti & 0.5219 & 1.98644 & 3.3129 & 12 \\
    Sm-Fe & 0.5891 & 1.48848 & 3.1394 & 12 \\
    Sm-Co & 0.5686 & 1.47399 & 3.1725 & 12 \\
    Ti-Co & 0.7527 & 1.40291 & 2.9331 & 12 \\
    Co-Co & 0.6774 & 1.64306 & 2.7093 & 12 \\
    \hline \hline
    \end{tabular}
    \caption{Table of all the values used for the Morse potentials used for the following study.}
    \label{tab:morse_potential_values_rt12}
\end{table}

To avoid any ambiguity, the \ch command is part of the chemical formula package, the documentation for which is here: https://latex-cookbook.net/chemistry/ . It seems to me that by defining each column with >{\centering\arraybackslash}m{0.2\textwidth} I should have a table that fits the width of the text section of the page. However, when I create this table I get the following:

Image of Table overflowing into margin.

As you can see from the red lines which roughly match where the margin is, the right hand side has flowed over, although the caption below hasn't. Why is this?

Best Answer

By setting the usable width of each column to 0.2\textwidth, the total width of each column becomes 0.2\textwidth+2\tabcolsep, as LaTeX by default inserts \tabcolsep of whitespace padding on each side. Since the tabular environment contains 5 columns, its total width is \textwidth+10\tabcolsep. In many document classes, the default value of \tabcolsep is 6pt; 10\tabcolsep thus amounts to 60pt, i.e., roughly .83in or 2.1cm. Having the table protrude into the right-hand margin by that amount is definitely noticeable!

One could go about fixing this situation by reducing the width of each m-type column to 0.2\textwidth-2\tabcolsep. However, I would like to recommend that you switch to a tabular* environment (with target width \textwidth), employ l, c, and S-type columns, and let LaTeX adjust the amount of intercolumn whitespace automatically so as to "fill up" the width of the text block. The S column type, which is provided by the siunitx package, is very handy for aligning numbers on their explicit or implicit decimal markers.

enter image description here

\documentclass{article}
\usepackage{array,siunitx,booktabs}
\newcolumntype{T}[1]{S[table-format=#1,group-digits=false]}

\begin{document}
    \begin{table}[t]
    \def\arraystretch{1.5}
    \setlength\tabcolsep{0pt}
    \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} 
          l T{1.4} T{1.6} T{1.4} c }
    \toprule
    Atom Pair & {$D_0$ (eV)} & {$\alpha$ (1/\AA)} & {$r_0$ (\AA)} & {Cut Off (\AA)} \\ 
    \midrule
        Fe-Fe & 0.764  & 1.5995  & 2.7361 & 12 \\
        Fe-Ti & 0.8162 & 1.448   & 2.914  & 12 \\
        Fe-Nd & 0.6036 & 1.6458  & 3.188  & 12 \\
        Nd-Nd & 0.312  & 0.945   & 4.092  & 12 \\ \addlinespace
        Nd-Ti & 0.4964 & 1.440118& 3.4309 & 12 \\
        Ti-Ti & 0.6540 & 1.2118  & 3.3476 & 12 \\
        Sm-Sm & 0.2365 & 1.16433 & 3.8485 & 12 \\
        Sm-Ti & 0.5219 & 1.98644 & 3.3129 & 12 \\ \addlinespace
        Sm-Fe & 0.5891 & 1.48848 & 3.1394 & 12 \\
        Sm-Co & 0.5686 & 1.47399 & 3.1725 & 12 \\
        Ti-Co & 0.7527 & 1.40291 & 2.9331 & 12 \\
        Co-Co & 0.6774 & 1.64306 & 2.7093 & 12 \\
    \bottomrule
    \end{tabular*}
    \caption{Table of all values used for the Morse potentials used in the following study.}
    \label{tab:morse_potential_values_rt12}
    \end{table}
\end{document}
Related Question