Longtable: Prevent page break after specific rows

longtable

  • I strongly assume that I miss the obvious.
  • I have a longtable and want to prevent a page break after specific rows.
  • After looking at the manual and looking at this question, I assumed that \\* or \nopagebreak will do the job.
  • However, somehow I do not get the following MWE to work as expected: Some Header C should not be "alone" at the end of a page.
  • What am I missing?

enter image description here

\documentclass{article}
% "margin = 52mm" is used to control the page break for the MWE.
\usepackage[a4paper, margin = 52mm, landscape]{geometry}
\usepackage{longtable}
\usepackage{booktabs}

\begin{document}

\begin{longtable}{p{0.25\linewidth}p{0.40\linewidth}p{0.25\linewidth}}
% Inspired by % https://tex.stackexchange.com/questions/194150
% --- First Head ---
\caption{Caption.}\\\toprule
\textbf{A} & \textbf{B} & \textbf{C}\\\toprule
\endfirsthead
% --- Other Head ---
\caption*{\textit{Continued from previous page.}}\\\toprule
\textbf{A} & \textbf{B} & \textbf{C}\\\toprule
\endhead
% --- Other Foots ---
\multicolumn{3}{r}{\textit{Continued on next page.}} \\
\endfoot
% --- Last Foot ---
\bottomrule
\endlastfoot
% --- Actual Contents ---
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\multicolumn{3}{l}{\textbf{Some Header A}}\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\multicolumn{3}{l}{\textbf{Some Header B}}\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\multicolumn{3}{l}{\textbf{Some Header C (Should not be before a page break.)}}\\*\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\multicolumn{3}{l}{\textbf{Some Header D}}\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\
\end{longtable}
 
\end{document}

enter image description here


Screenshot from the actual table:

enter image description here

Best Answer

Here is an alternative solution with longtblr environment of tabularray package:

\documentclass{article}
% "margin = 54mm" is used to control the page break for the MWE.
\usepackage[a4paper, margin = 54mm, landscape]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}

\begin{longtblr}[
  caption = {Caption},
]{
  colspec = {t{0.25\linewidth}t{0.40\linewidth}t{0.25\linewidth}},
  rowhead = 1,
  row{1} = {font=\bfseries},
}
\toprule
  A & B & C \\
\toprule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\SetCell[c=3]{l}\textbf{Some Header A} & & \\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\SetCell[c=3]{l}\textbf{Some Header B} & & \\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\SetCell[c=3]{l}\textbf{Some Header C (Should not be before a page break.)} & & \\*\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
\SetCell[c=3]{l}\textbf{Some Header D} & & \\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\\midrule
a & b & c\\
\bottomrule
\end{longtblr}
 
\end{document}

enter image description here

Related Question