2

I created a table by using the website tablesgenerator but I'm not completely satisfied regarding the centering of the header row.

Here is my code:

\begin{table}[h]
\centering
\caption{Information about downloaded and filtered NEOs databases.}
\label{tab:NEOs:databases:info}
\begin{tabular}{@{}ccrc@{}}
\toprule
\multirow{2}{*}{\textbf{\begin{tabular}[c]{@{}c@{}}Near-Earth \\ objects\end{tabular}}} & \multicolumn{2}{c}{\textbf{No. of objects}} & \multirow{2}{*}{\textbf{\begin{tabular}[c]{@{}c@{}}Date of\\ download\end{tabular}}} \\ \cmidrule(lr){2-3}
 & \textbf{downloaded} & \multicolumn{1}{c}{\textbf{filtered}} &  \\ \midrule
NEAs & \multicolumn{1}{r}{28963} & 53 & 19-05-2022 \\
NECs & \multicolumn{1}{r}{192} & 0 & 19-05-2022 \\ \bottomrule
\end{tabular}
\end{table}

I would want to get a better centering of the rows circled in red in the following image (I want that the two couples of rows have the same distance from the \toprule and from the \midrule): table Can you help me to get the vertical alignment of the circled objects?

0

3 Answers 3

4

The first argument of \multirow doesn't have to be an integer. Since the \cmidrule directive adds almost 0.5\baselineskip to the total height of the header material, I'd give \multirow{2.4}{*}{...} a try.

Separately, I'd also center-align rather than right-align the numbers in the two middle columns, while still aligning the numbers on their (implicit) decimal markers. In the code below, this is accomplished by employing the S column type that's provided by the siunitx package.

enter image description here

\documentclass{article}
\usepackage{multirow,booktabs,siunitx}
\newcolumntype{T}[1]{S[table-format=#1,group-digits=false]}
\newcommand\mytab[1]{\textbf{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}}
\begin{document}

\begin{table}[h]
\centering
\caption{Information about downloaded and filtered NEOs databases.\strut}
\label{tab:NEOs:databases:info}
\begin{tabular}{@{} c T{5.0} T{2.0} l @{}}
\toprule
\multirow{2.4}{*}{\mytab{Near-Earth \\ objects}} & 
\multicolumn{2}{c}{\textbf{No.\ of objects}} & 
\multirow{2.4}{*}{\mytab{Date of\\ download}} \\ 
\cmidrule(lr){2-3}
& {\textbf{downloaded}} & {\textbf{filtered}} \\ 
\midrule
NEAs & 28963 & 53 & 19-05-2022 \\
NECs &   192 &  0 & 19-05-2022 \\ 
\bottomrule
\end{tabular}
\end{table}

\end{document}
9
  • 1
    Thanks for your answer @Mico. The table looks very professional now. Do you know an alternative to the website indicated in my question that is able to perform your suggested alignments? I make this question because I need to import large .csv tables from separate files and so I need a webservice to accelerate the tables creation.
    – g_don
    Oct 1, 2022 at 11:51
  • 2
    @Giuseppe - My sincerely given piece of advice is not to use that website -- unless, by some miracle, you manage to generate exactly the table you want, without need to apply further tweaks. In my experience, the LaTeX code created by the website is virtually unmaintainable and untweakable by humans -- experienced LaTeX users (such as myself...) included. My main suggestion is that you familiarize yourself with the datatool package and learn how to use it to load data from csv files into a LaTeX document for inclusion in a table.
    – Mico
    Oct 1, 2022 at 12:01
  • 1
    @Giuseppe - The datatool package is part of any standard TeX distribution, including TeXLive. Overleaf provides a (sophisiticated...) front-end to TeXLive.
    – Mico
    Oct 1, 2022 at 12:06
  • 1
    @Giuseppe - Re Problem 1: To indicate, e.g., that the numbers in some column can have up to 5 ordinary digits and up to 2 decimal digits, just use T{5.2}. Re problem 2: Not sure what you're asking. For the answer I posted above, I used the l (short for "left") column type; when used in a tabular environment, l is a text mode and not a math mode column type. On your second comment: Again, not sure what you're asking. Do note that the instruction \begin{tabular}{@{} c T{5.0} T{2.0} l @{}} is executed inside the body of the document environment, not in the preamble.
    – Mico
    Oct 1, 2022 at 18:08
  • 1
    Thanks again! 1) Issue fixed by modifying the \sisetup in my preamble! 2) My issue was that the output that I have with your solution is the same that I get outside the table by typing $28963$ which is different from 28963. Anyway my problem was related to the dash line to separate the numbers in the dates: to avoid to get the minus sign, I had to use \text{-}.
    – g_don
    Oct 1, 2022 at 20:20
3

One way is to use tabularray package with libraries booktabls and siunitx:

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs,siunitx}

\begin{document}
    \begin{table}[ht]
    \centering
\caption{Information about downloaded and filtered NEOs databases.\strut}
\label{tab:NEOs:databases:info}
    \begin{tblr}{colspec = {c Q[c, si={table-format=5.0}] 
                              Q[c, si={table-format=2.0}] l},
                row{1,2} = {font=\bfseries, guard}
                }
    \toprule
\SetCell[r=2]{c}    {Near-Earth \\ objects}
        &\SetCell[c=2]{c}  No. of objects
                        &           & \SetCell[r=2]{c}  {Date of\\ download} \\
    \midrule
        & downloaded    & filtered  &               \\
\midrule
NEAs    & 28963         & 53        & 19-05-2022    \\
NECs    & 192           & 0         & 19-05-2022    \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

1

Not the prettiest solution, be certainly the simplest.

demo

\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}% caused gap below caption

\begin{document}
\begin{table}[h]
\centering
\caption{Information about downloaded and filtered NEOs databases.}
\label{tab:NEOs:databases:info}
\begin{tabular}{@{}ccrc@{}}
\toprule
  \textbf{Near-Earth} & \multicolumn{2}{c}{\textbf{No. of objects}} & \textbf{Date of} \\
  \cmidrule{2-3}
  \textbf{objects} & \textbf{downloaded} & \textbf{filtered} & \textbf{download} \\
\midrule
NEAs & \multicolumn{1}{r}{28963} & 53 & 19-05-2022 \\
NECs & \multicolumn{1}{r}{192} & 0 & 19-05-2022 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .