0

Is there a way in LaTeX to declare a command or environment that will "refuse" to be broken across pages unless absolutely necessary?

Use case: I have a number of documentation items (all generated via Doxygen's XML) and several of them are just a handful of lines long. In some cases up to four of these items could fit on one (A4) page.

Now one such documentation item starts a new \section or \subsection (depending on the context) and it should not break between the (sub)section heading and the contents. That is, title and content must always be kept on the same page. The only exception to that rule would be that title and content combined would not fit a single page if they started on a new page.

I guess what I am looking for could be described as "vertical badness" value and I need a way to measure items vertically and let them flow onto the next page if necessary.

  • 1
    Put the environment inside a box? –  May 07 '19 at 15:11
  • @JouleV that even works with a section and subsection? – 0xC0000022L May 07 '19 at 15:21
  • Without code I can't test... Please add an example code –  May 07 '19 at 15:22
  • Or use one continuous page in the output, cf. https://tex.stackexchange.com/questions/19237/automatically-increase-pdf-page-height. – Marijn May 07 '19 at 15:27
  • 1
    latex always prevents a page break after a heading, although of course normally it would allow the text to break – David Carlisle May 07 '19 at 15:54
  • Honestly speaking, LaTeX never breaks a page unless it is absolutely needed. – Sveinung May 07 '19 at 16:20
  • @Sveinung for many of my documents (papers and posters mostly, but also others) I absolutely disagreed with LaTeX about page breaks - the algorithms are a heuristic that can be wrong, and even when it is right, it can still be undesired by the author. "Computer says NO" is not a compelling argument :) – Marijn May 07 '19 at 17:38
  • 2
    @Marijn I forgot the :) – Sveinung May 07 '19 at 20:02
  • @DavidCarlisle did not know that. Is there a list that collects all that not-so-common "common" wisdom? I mean lurking (and occasionally asking) here helps a lot, but by no means do I feel as comfortable with LaTeX as with any of the other programming languages I use. – 0xC0000022L May 07 '19 at 20:18
  • @JouleV I tried using a minipage and the result was that the very few boxes that were bigger than the page height did not break ... which is suboptimal, so to speak. – 0xC0000022L May 09 '19 at 14:24

1 Answers1

1

I don't think that there is a good answer to your request. As David Carlise said that a sectional heading and following text are always on the same page and JouleV suggested putting your text into a box. Consider the following (less any of my typos):

\documentclass{article}
\newcommand{\stuff}{Bunch of stuff \\ more stuff \\ yet more.}
\begin{document} 
\mbox{}
\vspace{0.85\textheight} % move near bottom of page
\section{One}
\begin{minipage}{\textwidth}  % box some text
  \stuff
\end{minipage}

\clearpage % next page

\mbox{}
\vspace{0.85\textheight}
\section{two}
  \stuff

\end{document}

In section One the text is boxed and stays on the same page but in section Two the text is not boxed and continues on the following page. If you change the \vspace to o.86\textheight then section One starts on the second page and page 1 is empty.

In essence just use:

\section{...}
 your text
Peter Wilson
  • 28,066
  • I tried boxing the content, using a minipage just because that was easiest to get quickly. Alas, it really complicates things when the content is too big (vertically) to fit a page. What I am looking for is something that will keep together the content but fail gracefully if it can't. Thanks anyway for the answer and +1. – 0xC0000022L May 09 '19 at 14:23