5

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?

  • 3
    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
4

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}
  • 1
    +1 (seems you've beat me to post on my packages several times today:-) – David Carlisle Jan 10 '14 at 23:36
  • Both of these solutions work beautifully. Thanks! – CmdrMoozy Jan 11 '14 at 18:28
  • @CmdrMoozy - Thanks for letting me know that both solutions work for you. – Mico Jan 11 '14 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.