# Section should automatically start new page if the whole section does not fit on current page

I would like my sections to start on a new page if they do not fit on the current page. In other words: if a section fits on the current page print it, if it does not fit start printing it on the next page.

I would like this to be done automatically.

\usepackage{titlesec} seems to be the solution, but I can not get it to do the job.

• Should it be done with titlesec? how ?
• Is there a better approach?
-
this question is very similar but does not give automatic solutions –  danjjl Apr 4 '12 at 21:27
The whole section? –  egreg Apr 4 '12 at 21:28
Not sure I understand the question right, but yes (my sections are quite short (longest are 3 pages long)) –  danjjl Apr 4 '12 at 21:29
So your question is very unclear: three pages can't fit on a page, can they? –  egreg Apr 4 '12 at 21:31
Do you mean: If the next section can fit on the current page, just print it as-is. Otherwise, put in a page break and start the section on the new page. ? –  Mike Renfro Apr 4 '12 at 22:13

There is a simple solution that does require only a single pass ...

\documentclass{article}

\newcommand\mysection{\vfil\penalty-9999\vfilneg\section}

\usepackage{lipsum}

\begin{document}
\mysection{A} \lipsum[3]
\mysection{B} \lipsum[1-2]
\mysection{C} \lipsum[5]
\end{document}

-

Some ideas:

1. Set \labels at the start and end of the section and check if they are on the same page. You'll need multiple runs and you have to mark the end of sections.
2. Typeset the content of the section e.g. in a minipage and check its length. This will be quite fragile. And you will have to catch the content.
-
I am not very familiar with Latex macros. How would I check if both \labels are on the same page and insert a \newpage at the first label if they are not? don't hesitate sending me to some documentation –  danjjl Apr 10 '12 at 20:33
multiple runs in item 1 might be a slight understatement :-) Inserting a page break will make everything behind it move to new places, so <number of pages> might well be a good estimate for <number of runs>. In particular, once a page break is inserted, it doesn't make sense to look at the following labels at all. –  Stephan Lehmke Apr 10 '12 at 21:05
why should that be easier in DocScape? you have to analyse the page situation in both cases but that particular scenario can in fact be simply modeled by glue and penalties in one go. Alternative is to invoke your own OR and store the accumulated page fragment away and recombine later (that is what multicols does basically with the material in front). –  Frank Mittelbach Apr 16 '12 at 21:07
@FrankMittelbach I think what Martin refers to is that DocScape, being a data-based system, has a slightly different input model than (La)TeX. While the input of LaTeX is basically a stream with very limited lookahead capabilities, DocScape is operating on a document-structured tree and can thus easily look at the complete content of the next section, chapter or whatever. That there is a simple penalty-based solution in this case was not known at the time Martin's comment was written ;-) But there are a lot of situations in advanced layout composition where this is not the case. –  Stephan Lehmke Apr 16 '12 at 21:22
@FrankMittelbach If you mean using the aux file for "stabilizing" a complex layout in multiple runs, then this will get extremely tedious when the changes effected in the next run influence page breaking, as this will invalidate all subsequent layout decisions. All the successful examples of this are really annotations to otherwise unchanging pages like todo notes. I had a lengthy discussion on this with Manuel Montero Pineda on an analogous subject (FO layout optimization with multiple passes) and he aknowledged the things possible in DocScape are next to impossible to achieve with FO. –  Stephan Lehmke Apr 16 '12 at 21:41