Tables – How to Simply Align Table Content Both Vertically and Horizontally

tables

My table needs to align its content both vertically and horizontally.

Here is the code:

\documentclass[twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs, array}
\usepackage{lipsum}
\begin{document}
\begin{table*}
    \centering
      \begin{tabular}{>{\centering\arraybackslash}m{1cm}>{\centering\arraybackslash}m{1cm}}
        \toprule 1 & 2 \\ \midrule 3 & 4 \\ \bottomrule
      \end{tabular}
    \caption{table 1}
\end{table*}

\lipsum[1-15] % filler text
\end{document}

enter image description here

To the best of my understanding, m should align the contents vertically and these >{\centering\arraybackslash} segments should align it horizontally. However, if you measure the space above the digits and the space beneath them, you will see that there is more space beneath them.

The simple task is:

  • align cell content both horizontally and vertically, and
  • make column width scale automatically (with its content).

I found many similar questions here, but none of them actually addresses the problem on the elementary level.

Best Answer

The classic tool is the cellspace package: you prefix the column specifier with the letter S by default (or C if you load siunitx, or whatever letter you please with the [columntype=some letter] loading option):

\documentclass[twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage{booktabs, array}
\usepackage{lipsum}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}

\begin{document}
\begin{table*}
    \centering
      \begin{tabular}{>{\centering\arraybackslash}S{m{1cm}}>{\centering\arraybackslash}S{m{1cm}}}
        \toprule 1 & 2 \\ \midrule 3 & 4 \\ \bottomrule
      \end{tabular}
    \caption{table 1}
\end{table*}

\lipsum[1-15] % filler text

\end{document} 

enter image description here

Related Question