0

Working quite a time now with tables I get the impression that sizes are not respected.

I usually do:

  \begin{longtable}[H]{|m{0.3\linewidth}|m{0.7\linewidth}|}
    \hline
    \textbf{Header1} & \textbf{Header2}\\\hline
    \endfirsthead
    \hline
    \textbf{Header1} & \textbf{Header2}\\\hline
    \endhead
    Content\\\hline
    \caption{Caption}
  \end{longtable}

The horizontal size is bigger than expected. I compare the endpoint of the line above the footer with the right margin of the table. Those 2 shall get the same x-value, which is not the case yet.

When I use tabularx the x-value is the same:

  \begin{tabularx}{\linewidth}{|lX|}
    \hline
    Content & Content \\
    \hline
  \end{tabularx}

But since I have tables that are longer than one page I need a new solution since tabularx just keeps continuing the table into the footer and does not break on a new page. As suggested here, I changed to longtable: Continuing table on multiple pages with tabularx

longtable properly breaks across pages (even repeating the header) but does not respect the size, tabularx respects the size but does not break across pages. Is there a working solution to this issue?

EDIT: For all those searching the answer here is the working code I use now (2 columns example):

\begin{longtable}{|m{0.25\dimexpr\linewidth-4\tabcolsep-3\arrayrulewidth}|m{0.75\dimexpr\linewidth-4\tabcolsep-3\arrayrulewidth}|}
    \hline
    \textbf{1} & \textbf{2}\\\hline
    \endfirsthead
    \hline
    \textbf{1} & \textbf{2}\\\hline
    \endhead
    1 & 2\\\hline
    \caption{1}
  \end{longtable}
  • 2
    [H]? Get rid of that. – Johannes_B Sep 6 '16 at 8:16
  • Load package showframe to better judge. At the left and right of every cell content, a bit of space is added. Try @{} between the different columns (column definition). – Johannes_B Sep 6 '16 at 8:17
  • 1
    longtable has no H option any the table is specified too wide {|m{0.3\linewidth}|m{0.7\linewidth}|} has a total width of \linewidth+4\tabcolsep+3\arrayrulewidth – David Carlisle Sep 6 '16 at 8:18
  • 1
    longtable respects the size that you specified, you just specified the wrong size:-) – David Carlisle Sep 6 '16 at 8:21
5

You have to leave space for the column padding and rules so

\newlength\mylength
\setlength\mylength{\dimexpr\linewidth-4\tabcolsep-3\arrayrulewidth}
 % in general you need to remove
 % 2\tabcolsep for each column and 1\arrayrulewidth for each |

....

\begin{longtable}{|m{0.3\mywidth}|m{0.7\mywidth}|}
  • So would this guys be of use? \newlength\mylengthForOneColumn \setlength\mylengthForOneColumn{\dimexpr\linewidth-3\tabcolsep-2\arrayrulewidth} \newlength\mylengthForTwoColumns \setlength\mylengthForTwoColumns{\dimexpr\linewidth-4\tabcolsep-3\arrayrulewidth} \newlength\mylengthForThreeColumns \setlength\mylengthForThreeColumns{\dimexpr\linewidth-5\tabcolsep-4\arrayrulewidth} \newlength\mylengthForFourColumns \setlength\mylengthForFourColumns{\dimexpr\linewidth-6\tabcolsep-5\arrayrulewidth} – Michael S. Sep 6 '16 at 8:52
  • @gentlesea In general, vertical rules should be avoided. They are often just distracting. – Johannes_B Sep 6 '16 at 8:55
  • @gentlesea There are packages like supertabular and ltxtable and others that probaly can do a better job. David Carlisle will know more. – Johannes_B Sep 6 '16 at 8:56
  • @gentlesea no point in defining them all in advance. (and for one column you want to subtract 2\tabcolsep not 3) just allocate \mylength once in the preamble and then set it just before the table in each case adjusting for whatever columns and rules (or @-expressions that you have that take up width. – David Carlisle Sep 6 '16 at 9:03
  • 1
    @gentlesea: If you want to predefine some common tabular preamble setups, use \newcolumntype. E.g. \newcolumntype\tabtwocol{p{2cm}p{\dimexpr\linewidth-2cm-4\tabcolsep}} – Ulrike Fischer Sep 6 '16 at 9:52

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.