5

I want to create a longtable that spans several pages, with column widths set to cms by me:

\documentclass[12pt]{article}
\usepackage{longtable}
\usepackage{seqsplit}
\usepackage[a4paper,landscape,margin=1cm]{geometry}
\usepackage[utf8]{inputenc}

\begin{document}
\begin{longtable}{|p{3,53cm}|p{1,60cm}|p{0,80cm}|p{1,11cm}|p{1,16cm}|p{0,94cm}|p{1,11cm}|p{0,96cm}|p{0,57cm}|p{1,21cm}|p{1,04cm}|p{1,32cm}|p{1,26cm}|p{0,99cm}|p{1,27cm}|p{2,71cm}|p{1,41cm}|}
\hline
\textbf{ \seqsplit{one} } & \textbf{ \seqsplit{tow} } & \textbf{ \seqsplit{three} } & \textbf{ \seqsplit{four} } & \textbf{ \seqsplit{five} } & \textbf{ \seqsplit{six} } & \textbf{ \seqsplit{seven} } & \textbf{ \seqsplit{eight} } & \textbf{ \seqsplit{nine} } & \textbf{ \seqsplit{ten} } & \textbf{ \seqsplit{eleven} } & \textbf{ \seqsplit{twelve} } & \textbf{ \seqsplit{thirteen} } & \textbf{ \seqsplit{fourteen} } & \textbf{ \seqsplit{fifteen} } & \textbf{ \seqsplit{sixteen} } & \textbf{ \seqsplit{seventeen} } \\
\hline
\endhead
\hline
\seqsplit{aaaaaaaaaaaaaaaaaaaaaaaa@gmail.com} & \seqsplit{Jjjjjj} & \seqsplit{Ccccccccl} & \seqsplit{ttrrw} & \seqsplit{xxxxxxxx\ 83} & \seqsplit{ooooooo} & \seqsplit{2222\ rrrrrrr\ oo\ trr\ bbbbb\ bbbbbbb\ ttttttP\ -\ 2111\ eeeedsee\ prrr\ vvv\ ll\ eeeirrrr\ xxrrrttt} & \seqsplit{20/02/2011\ 11:39:37} & \seqsplit{0} & \seqsplit{0,00} & \seqsplit{0,00} & \seqsplit{07/07/2011\ 09:00:00} & \seqsplit{14/07/2013\ 11:00:00} & \seqsplit{700} & \seqsplit{4} & \seqsplit{xxxxooowwwaaaaaaaaapaaa/srsssssss} & \seqsplit{50,00} \hline
\end{longtable}
\end{document}

The problem with this tex is that I have seventeen columns, and the seventeenth appears out of the landscape page. However if we add the widths of the columns, is less than 23 cms. What's going on here?

0
6

You observe that the widths assigned to the columns add up to less than the width of the text block (which has width \textwidth), yet the table is too wide to fit into the text block. This is happening because there's also whitespace between the columns, in the amount of 2\tabcolsep. The default value of \tabcolsep is 6pt. Expressed in millimeters, the default amount of intercolumn whitespace comes to roughly 4.2mm. Since you have 17 columns, this makes for more than 7 cm of extra horizontal space.

What to do? You can do one or both of the following: (i) reduce some of the column widths, and (ii) reduce the amount of intercolumn whitespace via a \renewcommand instruction, e.g., \renewcommand\tabcolsep{4pt}. A combination of these instructions should get you to where you want to be.

0
5

Your column widths sum up to 23cm, converted to 654.13022pt; the text width is 788.14136pt, which is 27.7cm (A4 paper is 21cm wide and 29.7cm high in portrait mode).

However, your table is seen by TeX as 858.13022pt wide. Where does the excess come from? Let's compute

(858.13022-654.13022)/17 = 12

Indeed every column in a tabular or longtable is preceded and followed by a space of width \tabcolsep; the default value of this parameter is precisely 6pt.

You can fit your table either reducing some column width or reducing \tabcolsep:

(788.14136-654.13022)/34 = 3.9415

so doing

\begingroup\setlength{\tabcolsep}{3.9pt}
\begin{longtable}{
 |p{3,53cm}
 |p{1,60cm}
 |p{0,80cm}
 |p{1,11cm}
 |p{1,16cm}
 |p{0,94cm}
 |p{1,11cm}
 |p{0,96cm}
 |p{0,57cm}
 |p{1,21cm}
 |p{1,04cm}
 |p{1,32cm}
 |p{1,26cm}
 |p{0,99cm}
 |p{1,27cm}
 |p{2,71cm}
 |p{1,41cm}
 |}
 <table body>
 \end{longtable}\endgroup

you'll achieve a table width of 786.73001pt which fits in the text width.

How did I compute the table width? It's easy: I wrote a test file

\documentclass{article}
\begin{document}
\sbox0{%
\begin{tabular}{
 |p{3,53cm}
 |p{1,60cm}
 |p{0,80cm}
 |p{1,11cm}
 |p{1,16cm}
 |p{0,94cm}
 |p{1,11cm}
 |p{0,96cm}
 |p{0,57cm}
 |p{1,21cm}
 |p{1,04cm}
 |p{1,32cm}
 |p{1,26cm}
 |p{0,99cm}
 |p{1,27cm}
 |p{2,71cm}
 |p{1,41cm}
 |}
0 & 1 & 2 & 3 & 4 & 5 & 6 &7 &8 &9 & A & b & C & D & F & G & H
\end{tabular}}\the\wd0
\end{document}

The produced PDF file has

858.13022pt

If I add \setlength{\tabcolsep}{3.9pt} before the \sbox command I get the value

786.73001pt

In the document you'll add \begingroup and \endgroup as shown in order not to make the setting of \tabcolsep the same across the document.

1
  • Thanks for the explanation, very detailed, I appreciate it. – maraujop Jun 17 '13 at 13:38

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.