[Tex/LaTex] Textwidth longtable with centered headers

longtable

I'm trying to create a longtable which spans the entire \textwidth. Additionally, although the contents of the table use different alignments, all of the header cells should be centered. Based upon e.g. this question, I have come up with the following code:

\documentclass[letterpaper,12pt]{report}

\usepackage[margin=1in]{geometry}
\usepackage{longtable}

\setlength\LTleft{0pt}
\setlength\LTright{0pt}

\begin{document}

\noindent
\begin{longtable}{@{\extracolsep{\fill}} | l | c | c | r | r | }
\hline
\multicolumn{1}{|c|}{\textbf{Header 1}} &
    \multicolumn{1}{c|}{\textbf{Header 2}} &
    \multicolumn{1}{c|}{\textbf{Header 3}} &
    \multicolumn{1}{c|}{\textbf{Header 4}} &
    \multicolumn{1}{c|}{\textbf{Header 5}} \\
\hline
\endhead
\end{longtable}

\end{document}

When rendered with pdflatex, however, the first column is correctly centered, but the remaining columns seem to be right-aligned instead. I believe that the problem is that I'm misunderstanding how @{\extracolsep{\fill}} works in the longtable definition.

What am I doing wrong?

Best Answer

I think you have two main choices, both of which have their aesthetic merits:

  • Keep using your current longtable setup, but don't use vertical bars to separate the columns visually. Give it a try: I'm confident you won't miss those vertical rules at all.

  • Load the tabularx and ltxtable packages as well, and use the X column type for the headers of the longtable environment. The only, but fortunately fairly minor, drawback of this method is that you need to place the entire longtable material in a separate file. This drawback can be mitigated by using the filecontents package to include the contents of the longtable in the document's preamble. The macro \LTXtable{\textwidth}{<filename>} is then used to load the external longtable.

enter image description here

\RequirePackage{filecontents}
\documentclass[letterpaper,12pt]{report}
\usepackage[margin=1in]{geometry}
\usepackage{longtable,tabularx,ltxtable}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}

% same as posted by OP
\begin{filecontents*}{mytable0.tex} 
\begin{longtable}{@{\extracolsep{\fill}} | l | c | c | r | r | }
\caption{Longtable with extra ``fill'' and vertical rules}\\
\hline
\multicolumn{1}{|c|}{\textbf{Header 1}} &
    \multicolumn{1}{c|}{\textbf{Header 2}} &
    \multicolumn{1}{c|}{\textbf{Header 3}} &
    \multicolumn{1}{c|}{\textbf{Header 4}} &
    \multicolumn{1}{c|}{\textbf{Header 5}} \\
\hline
\endhead
\end{longtable}
\end{filecontents*}

\begin{filecontents*}{mytable1.tex}
\begin{longtable}{@{\extracolsep{\fill}}  l  c  c  r  r  }
\caption{Longtable with extra ``fill'' and no vertical rules}\\
\hline
\multicolumn{1}{c}{\textbf{Header 1}} &
    \multicolumn{1}{c}{\textbf{Header 2}} &
    \multicolumn{1}{c}{\textbf{Header 3}} &
    \multicolumn{1}{c}{\textbf{Header 4}} &
    \multicolumn{1}{c}{\textbf{Header 5}} \\
\hline
\endhead
\end{longtable}
\end{filecontents*}

\begin{filecontents*}{mytable2.tex}
\begin{longtable}{ | l | c | c | r | r | }
\caption{Longtable, ``centered X'' column types}\\
\hline
    \multicolumn{1}{|Y|}{\textbf{Header 1}} &
    \multicolumn{1}{ Y|}{\textbf{Header 2}} &
    \multicolumn{1}{ Y|}{\textbf{Header 3}} &
    \multicolumn{1}{ Y|}{\textbf{Header 4}} &
    \multicolumn{1}{ Y|}{\textbf{Header 5}} \\
\hline
\endhead
\end{longtable}
\end{filecontents*}
\begin{document}
\LTXtable{\textwidth}{mytable0.tex} % initial look

\LTXtable{\textwidth}{mytable1.tex}

\LTXtable{\textwidth}{mytable2.tex}
\end{document}
Related Question