[Tex/LaTex] Prevent page break after \midrule in \longtable

longtablepage-breakingrules

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.

Best Answer

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}