[Tex/LaTex] Longtable does not break correctly when used with \specialrule instead of \hline

booktabslongtablepage-breakingrules

I'm writing an application that generate latex code, therefore the solution must be the most generic possible to ensure that it will generate clean pdf all the time. I would like to let my users specify the thickness of the tables (both horizontal and vertical lines) and the best solution I've found so far is to use "\specialrule" from the booktabs package.

Here is a MWE :

\documentclass{article}

\usepackage{longtable}
\usepackage{tabularx}
\usepackage{booktabs}
\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}

\begin{document}

Some text goes here

and here

and here

and here

and here


\begin{longtable}{!{\VRule[3pt]}c!{\VRule[3pt]}c!{\VRule[3pt]}c!{\VRule[3pt]}c!{\VRule[3pt]}c!{\VRule[3pt]}}
\specialrule{3pt}{0pt}{0pt}
This & is & the & nice & header \\  \specialrule{3pt}{0pt}{0pt} \endhead
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}
Portable & 1 & 676 & 21.00 & 676.00 \\  \specialrule{3pt}{0pt}{0pt}

\end{longtable}


\end{document}

My problem is that the break does not work correctly depending on the height of content before the table and the thickness of the lines. In this case, the last horizontal line of the longtable on the first page is moved on the second page.

Best Answer

\hline in longtable really needs to know a lot about how longtables are put together, in particular how the left and right margin works, and what to do at a page break. It's basically more like \cline, or rather two \clines one on top of the other to allow duplication at a break.

So basically rules from other packages are not likely to work unless they explicitly cater for longtable.

If you just need varying \arrayrulewidth you should be able to use \setlength{\arrayrulewidth}{3pt} and then a normal \hline.

booktabs does have some support for it but I note its documentation says explicitly.

A somewhat technical note: within a longtable, \hline and \hline\hline both produce a double rule (to allow for page breaks occurring at that point). But the booktabs rules do not. longtable's automatic doubling of \hline is questionable, even according to the documentation within that package. But doubled booktabs rules make almost no sense at all.

So it seems to imply that the behaviour at the page break is by design.

Related Question