1

I am currently automating a report generation and I am now trying to obtain a consistent look on my arrays. I am currently working on a part where the pages need to be set in landscape mode, so I am using pdflscape. Moreover, to handle page breaks in arrays, I went for the longtable package, alond with booktabs for the pretty look.

Basically, my arrays can contain from 3 to 10 columns, the 2nd one is the one containing lots of text, the others hardly anything. Here is a MWE showing the tiniest, and the biggest one.

\documentclass[10pt]{report}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{pdflscape}

\usepackage{geometry}
 \geometry{
 a4paper,
 left=20mm,
 right=20mm,
 top=20mm,
 bottom=20mm,
 }
\pagenumbering{gobble}
\begin{document}

\begin{landscape}

\begin{longtable}{lp{\dimexpr.5\textwidth}p{1.5cm}}
\toprule
C1 & C2 & C3 \\
\midrule
data & data & data \\
\bottomrule
\end{longtable}



\begin{longtable}{lp{\dimexpr.5\textwidth}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}p{1.5cm}}
\toprule
C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9 & C10 \\
\midrule
data & data & data & data & data & data & data & data & data & data \\
\bottomrule
\end{longtable}
\end{landscape}
\end{document}

My problem here is that I would like the 1st column to be always "anchored" at the same horizontal padding, whatever the number of column (basically align the array to the left) and that my 2nd column adapt to the text width, as shown on this wonderful paint :

Example

The 2nd column will always contain more than "data" obviously, so I think this choice is justified. How should I proceed to make my 2nd column to be that wide ?

  • 1
    \begin{longtable}[l] (If you mean what I think you mean?) – David Carlisle Oct 20 '14 at 16:24
  • 1
    can you fix your first column as p{some value} then it is easy to adjust your \dimexpr to be \textwidth-width of other columns otherwise it's a bit trickier – David Carlisle Oct 20 '14 at 16:26
3

If you fix the with of the columns, then the width of the second column can be calculated, just subtracting the total width of the other columns, taking account the \tabcolsep padding on either side of each cell.

\documentclass[10pt]{report}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{pdflscape}

\usepackage{geometry}
 \geometry{
 a4paper,
 left=20mm,
 right=20mm,
 top=20mm,
 bottom=20mm,
 }
\pagenumbering{gobble}
\begin{document}

\begin{landscape}

\begin{longtable}{p{1.5cm}p{\dimexpr\columnwidth-1.5cm-(1.5cm)*1-6\tabcolsep\relax}*{1}{p{1.5cm}}}
\toprule
C1 & C2 & C3 \\
\midrule
data & data & data \\
\bottomrule
\end{longtable}



\begin{longtable}{p{1.5cm}p{\dimexpr\columnwidth-1.5cm-(1.5cm)*8-20\tabcolsep\relax}*{8}{p{1.5cm}}}
\toprule
C1 & C2 & C3 & C4 & C5 & C6 & C7 & C8 & C9 & C10 \\
\midrule
data & data & data & data & data & data & data & data & data & data \\
\bottomrule
\end{longtable}
\end{landscape}
\end{document}
  • That's perfect. However, I am getting underful \vbox (badness 10000) detected on the \end{longtable} lines. Any idea ? Anyway I will validate your answer. – user17424 Oct 20 '14 at 16:39
  • 1
    @NNzz hmm yes so do I:-) It's booktab's rules (if you use \hrule you don't get the warning) longtable and and (pdf)lscape and bookmarks don't seem to quite agree on who's going to redefine the rules in what order, no time to look now, but the warning seems largely spurious – David Carlisle Oct 20 '14 at 16:52

Your Answer

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