8

I'm trying to tell longtable to only issue a page break at one specific point (by using \\*. Putting a \midrule anywhere near that point causes a page break after the midrule, and not where I want it to be.

Consider this MWE:

\documentclass[12pt,a4paper]{article}

\usepackage{longtable,booktabs}

\begin{document}

\begin{longtable}{c}
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\*
Last line on this page \\
bla \\*%\midrule
bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
\end{longtable}

\end{document}

It produces the correct result:

enter image description here

My problem comes up after uncommenting the \midrule:

enter image description here

I tried preventing the page break after the \midrule by inserting \nopagebreak before and/or after it, but to no avail.

P.S.: I know that right fter a rule would usually be a desired point for a page break in a table. However, I am using rules before and after certain lines to create some kind of seperator between blocks of rows. Thus, the rule must stay on top of the following line and the page brak should occur right after the last \\* before the rule.

2 Answers 2

9

The problem is not confined to booktabs: Also with a standard \hline you would get a break. longtable redefines \hline so that it actually inserts two lines with a break point between them. This way a line at the bottom of a page is repeated on the top of the next page. You could define you a command which changes the penalty used by the \hline and the width temporarly:

\documentclass[12pt,a4paper]{article}

\usepackage{longtable,booktabs}

\makeatletter

\newlength\oriarrayrulewidth  
\newcount\orilowpenalty
\newcommand\nobreakmidrule{%
 \noalign{\global\oriarrayrulewidth\arrayrulewidth\relax
          \global\orilowpenalty\@lowpenalty\relax  
          \global\@lowpenalty=\numexpr-10000\relax%
          \global\arrayrulewidth\lightrulewidth\relax}
 \hline
 \noalign{\global\@lowpenalty=\orilowpenalty\relax%
          \global\arrayrulewidth\oriarrayrulewidth\relax}}

\makeatother

\begin{document}

\begin{longtable}{c}
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\*
Last line on this page \\
bla \\*\nobreakmidrule
bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
\end{longtable}

\end{document}
2
  • I have the same problem and this looks interesting, but why are the redefinitions \global? I worry about introducing regressions elsewhere in my document.
    – meide
    Jun 22, 2019 at 21:37
  • +1: Thanks. Looks like a bug in longtable to me then? Do you agree? If yes, I would try to create a bug report. Feb 18, 2022 at 14:08
7

easiest is to force a break where you want a break

\documentclass[12pt,a4paper]{article}

\usepackage{longtable,booktabs}

\begin{document}

\begin{longtable}{c}
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\*
bla \\*
Last line on this page \\
bla \\\pagebreak\midrule
bla \\*
bla \\* bla \\* bla \\* bla \\* bla \\* bla \\* bla \\*
\end{longtable}

\end{document}
1
  • Thanks! Pretty obvious, but I didn't think of it. :) This will do for now.
    – einbandi
    Jun 25, 2014 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .