Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have to print several tables with distinct number of columns. I'm using lingtable because sometimes these tables contains over 300 rows each. Here's the problem.

Even defining a fixed width in cm, when having different column counts, the total width of the table is different.

Why? (I think I'm obviously not aware of some padding or margin)

How do I control the "real" width of the columns?

Here's the code I'm using right now

\documentclass{report}
\usepackage{longtable}
\usepackage{array}
\begin{document}
    \begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|p{3cm}|p{3cm}|p{1cm}|p{1cm}|}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|p{3cm}|p{3cm}|p{2cm}|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{longtable}
\end{document}

this is the result (but the sum of column width is the same!):

result

share|improve this question
1  
|p{2cm}|p{2cm}|p{2cm}|p{2cm}|} is 4*2cm + 8*\tabcolsep + 5*\arrayrulewidth wide –  David Carlisle May 16 at 19:54
    
OK, now I understand better. is \tabcolstep placed in each side of each column? –  Maluchi May 16 at 20:01
    
OK, now i finally understand, post that as an answer and I'll accept, finally found the array and source2e documentation pages. –  Maluchi May 16 at 20:03
    
If you have a modern distribution you shouldn't have to find the documentation just type texdoc array or texdoc longtable etc and the pdf should open –  David Carlisle May 16 at 20:06
    
I'm relatively new in LaTeX, i didn't knew about source2e until i faced this problem. Thanks!! –  Maluchi May 16 at 20:08

2 Answers 2

up vote 7 down vote accepted
\documentclass{report}
\usepackage{longtable}
\usepackage{array}
\newlength\mylength
\begin{document}

\setlength{\mylength}{\dimexpr 8cm -8\tabcolsep- 5\arrayrulewidth}

    \begin{longtable}{|*4{p{\dimexpr\mylength / 4\relax}|}}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{longtable}

    \begin{longtable}{|*2{p{\dimexpr 3\mylength/8}|}
                       *2{p{\dimexpr   \mylength/8}|}}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{longtable}

\setlength{\mylength}{\dimexpr 8cm -6\tabcolsep- 4\arrayrulewidth}
    \begin{longtable}{|*2{p{\dimexpr 3\mylength/8}|}
                          p{\dimexpr 2\mylength/8}|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{longtable}
\end{document}
share|improve this answer

You could use one of the extension packages which allows you to combine the facilities of tabularx with those of longtable. tabularx lets you specify the width of the tabular. On its own, it is incompatible with longtable but several packages are available which allow you to use its syntax with long tables. For example:

\documentclass{report}
\usepackage{ltablex}
  \keepXColumns
\usepackage{array}
\begin{document}
    \begin{tabularx}{110mm}{|*{4}{X|}}
            \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline
            test & test & test & test \\ \hline \hline
    \end{tabularx}

    \begin{tabularx}{110mm}{|*{2}{>{\hsize=1.5\hsize}X|}*{2}{>{\hsize=.5\hsize}X|}}
            \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline
            test & test & test & test asd ads asd asd  \\ \hline \hline
    \end{tabularx}

    \begin{tabularx}{110mm}{|*{2}{>{\hsize=1.125\hsize}X|}>{\hsize=.75\hsize}X|}
            \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline
            test & test & test \\ \hline \hline
    \end{tabularx}
\end{document}

ltablex tabulars of fixed width

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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