# Bold, horizontally and vertically aligned, multiline table headers

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:

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.

• Would it be better to not use shortstack and use instead some kind of fixed columns widths? If so, how would I set that up? – POliveira Feb 16 '15 at 17:35

## 2 Answers

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}

• It does exactly what I intend! Thank you! I've only 2 questions: 1) what are those @{} for?; 2) regarding the tabcolsep parameter, how did you find the table too wide?, it seems pretty ok over here, could it be because we're using different document formats? I'm using A4 format, which is sufficiently wide for this table. – POliveira Feb 16 '15 at 18:04
• the @{} removes the padding from the inner table, as the outer table already adds padding. On your MWE as posted latex reports Overfull \hbox (2.58034pt too wide) in alignment at lines 10--18 so your table is 2.5 pt wider than \textwidth – David Carlisle Feb 16 '15 at 18:18

Another solution with the makecell package, which is designed precisely to choose both vertical and horizontal alignment and a common formatting in cells with the \thead and \makecell commands. I also loaded cellspace to have a less tight vertical spacing in tables:

\documentclass{report}

\usepackage{booktabs}
\usepackage{longtable}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}

\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}

\begin{document}

\begin{center}
\begin{longtable}{Srcrrcc}
\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}