[Tex/LaTex] Bold, horizontally and vertically aligned, multiline table headers

line-breakingtablesvertical alignment

I'm trying to print a table where the first row, containing the header, should be bold and both horizontally and vertically centered. Additionally, I'm using longtable environment from this package, since I have a really long table that spans several pages. Due to horizontal stress, I also want to fit some of the table headers in 2 lines (but not all of them), which I did using shortstack

That said, I'm still not able to align the header content vertically, given that some cells have 2 lines and others have only 1. See below a MWE:

\documentclass{report}

\usepackage{booktabs}
\usepackage{longtable}
\newcommand*{\thead}[1]{\multicolumn{1}{c}{\bfseries #1}}

\begin{document}

\begin{center}
\begin{longtable}{rcrrcc}
    \toprule
    \thead{ID} & \thead{Database name} & \thead{\shortstack{Size\\(MB)}} & \thead{\shortstack{No. of\\records}} & \thead{\shortstack{Time stamp\\1st record}} & \thead{\shortstack{Time stamp\\last record}} \\
    \midrule
    %\input{tab-metadata} Really long table
    1 & dummie & 2.1 & 33 & dummie & dummie \\
    2 & dummie & 4.3 & 67 & dummie & dummie \\
    \bottomrule
\end{longtable}
\end{center}

\end{document}

This code originates the following table:

Non-aligned table headers

I ask your help to find the simplest, most correct and most elegant way to solve this problem and get all header cells vertically centered.

Best Answer

Vertical centring of headings is most easily done with tabular and I removed center as it doesn't centre longtables. I reduced the inter column spacing by a bit as your table was slightly too wide for the page.

\documentclass{report}

\usepackage{booktabs}
\usepackage{longtable}
\newcommand*{\thead}[1]{%
\multicolumn{1}{c}{\bfseries\begin{tabular}{@{}c@{}}#1\end{tabular}}}

\begin{document}

\setlength\tabcolsep{5pt}
\begin{longtable}{@{}rcrrcc@{}}
    \toprule
 \thead{ID} &
 \thead{Database name} &
 \thead{Size\\(MB)} & 
\thead{No. of\\records} &
 \thead{Time stamp\\1st record} &
 \thead{Time stamp\\last record} \\
    \midrule
    %\input{tab-metadata} Really long table
    1 & dummie & 2.1 & 33 & dummie & dummie \\
    2 & dummie & 4.3 & 67 & dummie & dummie \\
    \bottomrule
\end{longtable}


\end{document}
Related Question