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?
danjjl
  • 265

3 Answers3

11

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

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} 
ian
  • 304
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.
  • 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 might well be a good estimate for . 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
  • @StephanLehmke: I know. And I know that this is probably a lot easier in DocScape than in LaTeX. :-) – Martin Schröder Apr 10 '12 at 21:34
  • 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). – Frank Mittelbach Apr 16 '12 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. – Stephan Lehmke Apr 16 '12 at 21:22
  • @Stephan I understand the difference that you outline, but this doesn't really help if you have formatting dependencies on formatting of components unless those dependencies are strictly linear or strictly local. But if that is the case then you can (with some adjustments) use something like an aux file as a simple form of database. – Frank Mittelbach Apr 16 '12 at 21:28
  • @Stephan btw I wouldn't call it a "simple" penalty-based solution but an "elegant one ... only that I can't really take creadit for it. It is due to Plass/Knuth from "Breaking paragraphs into lines" or some other paper on the penalty/glue/box algebra as they called it. Might even be recorded in the TeXbook in some shape or form in appendix D(irty tricks). – Frank Mittelbach Apr 16 '12 at 21:37
  • 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. – Stephan Lehmke Apr 16 '12 at 21:41
  • @FrankMittelbach Elegant indeed. I didn't mean "simple" in a derogative sense. Btw. "simple" is your own wording :-) It's extremely impressive this question can be resolved that way. But as long as you're considering one page break at a time, penalties will only take you so far... – Stephan Lehmke Apr 16 '12 at 21:44