6

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

  • 1
    |p{2cm}|p{2cm}|p{2cm}|p{2cm}|} is 4*2cm + 8*\tabcolsep + 5*\arrayrulewidth wide – David Carlisle May 16 '14 at 19:54
  • OK, now I understand better. is \tabcolstep placed in each side of each column? – torresmateo May 16 '14 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. – torresmateo May 16 '14 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 '14 at 20:06
  • I'm relatively new in LaTeX, i didn't knew about source2e until i faced this problem. Thanks!! – torresmateo May 16 '14 at 20:08
7
\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}
1

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

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.