4

I have an issue about the vertical and horizontal centering for the content of each cell in a table. Ideally, I want a fixed padding for each cell.

Below the curent result :

current result

As you can see, the result is centered horizontally but not vertically.

The associated Latex code is :

\begin{table}[h!]
\begin{center}
\begingroup
\setlength{\tabcolsep}{6pt} % Default value: 6pt
\renewcommand{\arraystretch}{4} % Default value: 1
\begin{tabular}{|c|c|}
\hline
$\mathlarger{\int}_{l_{min}}^{l_{max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell$ & 6.75254172757782e-05   \\ \hline
$\Bigg[\mathlarger{\int}_{l_{min}}^{l_{max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{2}$ & 4.559681978267965e-09  \\ \hline
$\Bigg[\mathlarger{\int}_{l_{min}}^{l_{max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{-1}$  & 14809.238363029064     \\ \hline
$\dfrac{1}{ng} \quad \text{(str)}$ & 2.417598569737143e-07 \\ \hline
\end{tabular}
\endgroup
\end{center}
\end{table}  

I would like to get help to center correctly vertically and horizontally this table with a fixed padding for each cell.

2 Answers 2

6

I have found \arraystretch not always predictable in such matters. I prefer the \addstackgap macro from the stackengine package, that places a vertical buffer above and below its argument (default 3pt).

I take the liberty of replacing min and max with \min and \max, to make them upright operators.

\documentclass{article}
\usepackage{mathtools,relsize,stackengine}
%\setstackgap{S}{3pt}% CAN ADJUST THE VERTICAL GAP ADDED
\begin{document}
\begin{table}[h!]
\begin{center}
\begingroup
\setlength{\tabcolsep}{6pt} % Default value: 6pt
%\renewcommand{\arraystretch}{4} % Default value: 1
\begin{tabular}{|c|c|}
\hline
\addstackgap{$\mathlarger{\int}_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell$} & 6.75254172757782e-05   \\ \hline
\addstackgap{$\Bigg[\mathlarger{\int}_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{2}$} & 4.559681978267965e-09  \\ \hline
\addstackgap{$\Bigg[\mathlarger{\int}_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{-1}$}  & 14809.238363029064     \\ \hline
\addstackgap{$\dfrac{1}{ng} \quad \text{(str)}$} & 2.417598569737143e-07 \\ \hline
\end{tabular}
\endgroup
\end{center}
\end{table}  
\end{document}

enter image description here

1
  • @StevenBSegletes . Thanks a lot ! it is very pretty
    – user94782
    Commented Apr 18, 2021 at 2:56
4

Here are three alternatives using cellspace, makecell or nicematrix.

I also used \displaystyle instead of \mathlarger inside of the first column, as well as an S type column from siunitx for the numbers in the last column.

All three examples result in the following output:

enter image description here

\documentclass{article}
\usepackage{mathtools}

% only used in first example:
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{0.5\tabcolsep}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}

% only used in second example:
\usepackage{makecell}
\setcellgapes{0.5\tabcolsep}

% only used in third example:
\usepackage{nicematrix}

% used for improved output in second cell
\usepackage{siunitx}
\sisetup{table-align-exponent=false}

\begin{document}
\begin{table}[h!]
\centering
\begin{tabular}{|>{\(\displaystyle}0c<{\)}|S[table-format=1.15e-1]|}
\hline
\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell & 6.75254172757782e-05   \\ \hline
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{2} & 4.559681978267965e-09  \\ \hline
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{-1}  & {\num{14809.238363029064}}    \\ \hline
\dfrac{1}{ng} \quad \text{(str)} & 2.417598569737143e-07 \\ \hline
\end{tabular}
\end{table}


\begin{table}[h!]
\centering
\makegapedcells
\begin{tabular}{|>{\(\displaystyle}c<{\)}|S[table-format=1.15e-1]|}
\hline
\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell & 6.75254172757782e-05   \\ \hline
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{2} & 4.559681978267965e-09  \\ \hline
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{-1}  & {\num{14809.238363029064}}      \\ \hline
\dfrac{1}{ng} \quad \text{(str)} & 2.417598569737143e-07 \\ \hline
\end{tabular}
\end{table}  

\begin{table}[h!]
\centering
\begin{NiceTabular}[cell-space-limits=0.5\tabcolsep]{>{\(\displaystyle}c<{\)}S[table-format=1.15e-1]}[hvlines]
\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell & 6.75254172757782e-05   \\ 
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{2} & 4.559681978267965e-09  \\ 
\Bigg[\int_{l_{\min}}^{l_{\max}}C_{\ell,\text{gal,ph}} (\ell)\text{d}\ell\Bigg]^{-1}  & {\num{14809.238363029064}}      \\
\dfrac{1}{ng} \quad \text{(str)} & 2.417598569737143e-07 \\ 
\end{NiceTabular}
\end{table} 
\end{document}

You must log in to answer this question.