# Textwidth longtable with centered headers

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
\hline
\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?

• Mico's given a nice answer but just to comment that you should never use \noindent before longtable (or any display environment) it won't affect the indentation, just add a blank line of spurious space. \extracolsep and vertical lines don't really work together in normal tabular either so it's not all my fault they don't really work in lingtable:-) – David Carlisle Jan 10 '14 at 23:39

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.

\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
\hline
\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
\hline
\end{longtable}
\end{filecontents*}

\begin{filecontents*}{mytable2.tex}
\begin{longtable}{ | l | c | c | r | r | }
\caption{Longtable, centered X'' column types}\\
\hline
\hline
`