2

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?

7
  • 3
    You did not take into account the value of tabcolsep, the small horizontal white space that is added to the left and right of each cell's contents.
    – leandriis
    Oct 26 at 17:10
  • 2
    Since you don't need linebreaks inside of cells, it would be easier to use regular c type columns in combination with tabular* and \extracolsep
    – leandriis
    Oct 26 at 17:11
  • Okay thankyou, how can I take this into account? What is it's value, and can I set it to zero for this table only?
    – Connor
    Oct 26 at 17:11
  • @leandriis If you would write an answer showing any of these methods I'd happily accept it, I'm just interested in doing the most expedient least easily broken thing!
    – Connor
    Oct 26 at 17:12
  • @leandriis It seems \def is local? Is that correct? I have some def statements and they don't change anything else outside of the table environment they are in.
    – Connor
    Oct 26 at 17:15
8

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}
4
  • Thanks! What does \extracolsep{\fill}} mean?
    – Connor
    Oct 26 at 17:49
  • 2
    @Connor - @{\extracolsep{\fill}} is a well known LaTeX trick -- actually, well known primarily to frequent LaTeX document writers -- to get the width of the tabular* environment occupy the intended width (here: \textwidth). In words, it inserts extra whitespace in the intercolumn spaces. For for more on tabular* see, e.g., this answer to the query How to force a table into page width? (Shameless self-citation alert.)
    – Mico
    Oct 26 at 17:54
  • Awesome thankyou! How come you set tabcolsep to zero anyway? Why is it not a problem in the tabular* environment?
    – Connor
    Oct 26 at 17:58
  • 1
    @Connor - The total amount of whitespace between columns is 2\tabcolsep+\fill, where \fill is infinitely stretchable. Setting \tabcolsep to zero is just a automatic setting of mine, an old habit if you prefer. If it's not needed, no harm done; oftentimes, though, it is needed, especially when one has a table with many columns. (8 or more is "many" in my view.)
    – Mico
    Oct 26 at 19:05
3

Here are two alternative versions. The first one uses fixed with m type columns as the original code did but instead of using 0.2\textwidth as the width, I substracted 2\tabcolsep from each cell's width using \dimexpr 0.2\textwidth-2\tabcolsep. Since the contents won't spread over multiple lines, you could also use regular c type columns and make sure that the table is as wide as the textwidth using tabular* instead of tabular. You can then evenly distribute the extra width using @{\extracolsep{\fill}}as in the second example table.

Both examples result in the following output: (Red lines indicate margins.)

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{chemformula}

\begin{document}

\begin{table}
    \def\arraystretch{1.5}
    \begin{tabular}{ *{5}{>{\centering\arraybackslash}m{\dimexpr 0.2\textwidth-2\tabcolsep}} }
    \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 \\
    \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}

\begin{table}
    \def\arraystretch{1.5}
    \begin{tabular*}{\linewidth}{c @{\extracolsep{\fill}} *{4}{c}}
    \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 \\
    \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}

\end{document}
1

A variation of nice (+1) @Mico answer.

  • used is tabularray package
  • columns have equal width (used are X column type)
  • a vertical space of 1ex is inserted after every third rows in table body
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,siunitx}
  \ExplSyntaxOn
\NewChildSelector{eachthree}
  {
    \int_step_inline:nnnn {2}{3}{\l_tblr_childs_total_tl}
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
\ExplSyntaxOff

\begin{document}
    \begin{table}[t]
    \def\arraystretch{1.5}
    \setlength\tabcolsep{0pt}
\begin{tblr}{colspec = {@{} X[l] 
                        X[c,si={table-format=1.4}]
                        X[c,si={table-format=1.6}]
                        X[c,si={table-format=1.4}]
                        X[c] @{}},
             row{2-Z}  = {rowsep=0pt},
             row{eachthree} = {abovesep=1ex},
            }
    \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 \\
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 \\
    \bottomrule
\end{tblr}
\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}

enter image description here

0

Working off the helpful comment from @leandriss, I fixed the problem by adding a \def which changes the tabcolsep value to zero. The code is now the follwing:

\begin{table}[t]
    \centering
    \def\arraystretch{1.5}
    \def\tabcolsep{0}
    \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}

The only addition to the code is on the 4th line, and is \def\tabcolsep{0} which sets the table column separation to zero within the table environment. I am not certain of this, but it seems \def is local to this environment, and so changes made to global variables are only effective within the table environment. This addition gives me a table of the expected width, which can be seen below:

Aligned Table.

Thankyou @Mico for the edit to the column designations.

5
  • 2
    Sorry, but setting \tabcolsep to zero is not a good idea here, unless you can guarantee that the contents of all cells will never exceed 0.2\textwidth.
    – Mico
    Oct 26 at 17:34
  • @Mico In this instance where I have defined them all to be 0.2\textwidth wide I guess that is a guarantee I'm bound to make either way! But regardless, I am a rookie, and I would love a better answer, if you'd be willing to write one or point me in the right direction I'd be very grateful!
    – Connor
    Oct 26 at 17:35
  • 1
    You can also set *{5}{>{\centering\arraybackslash}m{0.2\textwidth-2\tabcolsep}}, this will keep the gap between the columns. Oct 26 at 17:43
  • @JasperHabicht: This code requires the calc package or alternatively \dimexpr.
    – leandriis
    Oct 26 at 17:45
  • @leandriis True! Thank you for pointing this out! Oct 26 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.