0

I'm generating LaTeX code for a book with many (over 200) subsections each consisting of two longtables. I don't want to force a pagebreak before every subsection because that leaves a lot of wasted blank space after very short subsections. Having searched previous posts I'm using \needspace as recommended to only insert page breaks when the remaining space is limited. This mostly works ok, but sometimes produces a spurious longtable header, as illustrated in the following MWE where the subsection heading "0.1.2 Subsec2" is preceded by an orphan header "Row DataC" from immediately following longtable.

Removing the \needspace (by commenting it out in the \mysubsec command definition) seems to remove the spurious headers (although that sometimes leaves orphan subsection headings despite the \nopagebreak at the end of that command). Is there a solution that avoids \needspace triggering these spurious longtable headers? Or some alternative way of defining a pagebreak that's conditional on the remaining space on the page?

\documentclass[a5paper]{book}

\usepackage[pass]{geometry}
\geometry{a5paper}

\usepackage{longtable}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{needspace}

\newcommand{\mysubsec}[1]{%
    \needspace{6\baselineskip}
    \subsection{#1}%
    \label{#1}%
    \nopagebreak
}

\newenvironment{mytab}[2]
{\begin{longtable}[l]{r*{#2}c}
    \toprule
    Row & \multicolumn{#2}{c}{Data#1} \\
    \midrule
    \endhead
}
{\end{longtable}}

\newcommand{\myrow}[1]{
\multirow[t]{2}{*}{#1} & a & b & c \\
& d & e & f \\
}

\begin{document}
\raggedbottom

\mainmatter
Title \clearpage

\section{Sec1}

\mysubsec{Subsec1}
\begin{mytab}{A}{3}
\myrow{1}\midrule
\myrow{2}\bottomrule
\end{mytab}

\begin{mytab}{B}{3}
\myrow{1}\midrule
\myrow{2}\midrule
\myrow{3}\bottomrule
\end{mytab}

\mysubsec{Subsec2}

\begin{mytab}{C}{3}
\myrow{1}\midrule
\myrow{2}\midrule
\myrow{3}\bottomrule
\end{mytab}

\begin{mytab}{D}{3}
\myrow{1}\midrule
\myrow{2}\midrule
\myrow{3}\bottomrule
\end{mytab}

\clearpage

\mysubsec{Subsec3}
\begin{mytab}{E}{3}
\myrow{1}\bottomrule
\end{mytab}

\begin{mytab}{F}{3}
\myrow{1}\bottomrule
\end{mytab}

\mysubsec{Subsec4}
\begin{mytab}{G}{3}
\myrow{1}\bottomrule
\end{mytab}

\begin{mytab}{H}{3}
\myrow{1}\bottomrule
\end{mytab}

\mysubsec{Subsec5}
\begin{mytab}{I}{3}
\myrow{1}\midrule
\myrow{2}\bottomrule
\end{mytab}

\begin{mytab}{J}{3}
\myrow{1}\midrule
\myrow{2}\bottomrule
\end{mytab}

\end{document}

spurious longtable header with needspace

2

1 Answer 1

1

Thank you both for your feedback and suggestions. I've tested based on these and a summary of my findings is:

In the older TeX Live 2019.20200218-1:

  • longtable.sty is 2020/01/07 v4.13
  • \needspace causes spurious longtable headers
  • \Needspace avoids the spurious longtable headers without creating orphan subsection heading "0.1.2 Subsec2"
  • orphan subsection heading "0.1.5 Subsec5" does occur if \Needspace is commented out

In the newer TeX Live 2021.20220204-1:

  • longtable.sty is 2021-09-01 v4.17

  • \needspace and \Needspace avoid the spurious longtable headers but they create orphan subsection heading "0.1.2 Subsec2" (with needspace length 6\baselineskip)

  • no orphan subsection headings occur with \[Nn]eedspace commented out

So the solution for now is to use \Needspace with the older version, because this fixes the spurious longtable headers without creating the orphan subsection headings that occur with the newer version.

You must log in to answer this question.

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