10

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?
11
  • this question is very similar but does not give automatic solutions
    – danjjl
    Apr 4, 2012 at 21:27
  • The whole section?
    – egreg
    Apr 4, 2012 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, 2012 at 21:29
  • 1
    So your question is very unclear: three pages can't fit on a page, can they?
    – egreg
    Apr 4, 2012 at 21:31
  • 3
    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. ? Apr 4, 2012 at 22:13

3 Answers 3

10

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}
0
0

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.
9
  • 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, 2012 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. Apr 10, 2012 at 21:05
  • 1
    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). Apr 16, 2012 at 21:07
  • 1
    @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. Apr 16, 2012 at 21:22
  • 1
    @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. Apr 16, 2012 at 21:41
0

If the sections will never be longer than one page, you could wrap them in some unbreakable environment (e.g. minipage) and they will automatically be moved to the next page if they don't fit.

\documentclass{article}

\usepackage{lipsum}

\begin{document}

\noindent\begin{minipage}{\textwidth}
\section{title}
\lipsum[1-2]
\end{minipage}

\bigskip
\noindent\begin{minipage}{\textwidth}
\section{title}
\lipsum[1]
\end{minipage}

\bigskip
\noindent\begin{minipage}{\textwidth}
\section{title}
\lipsum[1-2]
\end{minipage}

\bigskip
\noindent\begin{minipage}{\textwidth}
\section{title}
\lipsum[1-2]
\end{minipage}

\end{document} 

You must log in to answer this question.

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