Subsection on new page if it doesnt fit

I have many subsections in my document. Each subsection is quite short (each subsection consists of five to ten lines). I want to have more than one subsection on each page but I don't want a subsection to be splitted over two pages. If a subsection doesn't fit on the remaining of a page I want it to be placed on the next page.

Is there a page layout which does a \newpage automatically when a subsection doesn't fit on the page?

• Unfortunately, a subsection 'environment' is not a floating environment, but perhaps, it is possible to use the float package to define a new floating environment which acts as subsection, starting at new pages if needed, but this would also mean, that due to some reasons, say subsection 1.1 could appear after subsection 1.2, which is probably not desired by you? It is in my opinion also not a feature connected with a particular page layout. – user31729 Feb 25 '14 at 9:25
• What about sections? Should they start in a new page? – egreg Feb 25 '14 at 13:02
• A very partial solution, reducing the number of manual page breaks could be loading the titlesec package with the nobottomtitles option and syaing, e.g.: \renewcommand{\bottomtitlespace}{5\baselineskip}. This would ensure the shortest subsections couldn't be split. It would also be the case for sections. – Bernard Feb 25 '14 at 14:38
• Does tex.stackexchange.com/a/152278/15925 help? – Andrew Swann Feb 26 '14 at 11:31

I can't see such a style without section starting off in a new page. So here it is: we add \clearpage before all \section commands (after the first) and \filbreak before every \subsection.

\documentclass{article}
\usepackage{etoolbox}

\preto{\section}{\clearpageafterfirst}
\preto{\subsection}{\filbreak}
\newcommand{\clearpageafterfirst}{%
\gdef\clearpageafterfirst{\clearpage}%
}

\usepackage{lipsum} % to generate dummy text for the MWE

\begin{document}

\title{Title}
\author{author}
\maketitle

\section{A section}
\subsection{A subsection}\lipsum[1-2]
\subsection{A subsection}\lipsum[2]
\subsection{A subsection}\lipsum[3-4]
\subsection{A subsection}\lipsum[4]
\subsection{A subsection}\lipsum[5]
\subsection{A subsection}\lipsum[6]
\subsection{A subsection}\lipsum[7]
\subsection{A subsection}\lipsum[8]
\subsection{A subsection}\lipsum[9]
\subsection{A subsection}\lipsum[10]
\subsection{A subsection}\lipsum[11]

\section{B section}

\subsection{A subsection}\lipsum[12]
\subsection{A subsection}\lipsum[13]
\subsection{A subsection}\lipsum[14]
\subsection{A subsection}\lipsum[15]
\subsection{A subsection}\lipsum[16]
\subsection{A subsection}\lipsum[17]
\subsection{A subsection}\lipsum[18]
\subsection{A subsection}\lipsum[19]
\subsection{A long subsection}\lipsum[20-25]
\subsection{A subsection}\lipsum[26]

\end{document}


If a subsection is longer than one page it will go to the next one without problems.

If you don't want that sections start on a new page it's slightly more complicated:

\documentclass{article}
\usepackage{etoolbox}

\newtoggle{aftersection}
\preto{\section}{\filbreak\global\toggletrue{aftersection}}
\preto{\subsection}{\iftoggle{aftersection}{\global\togglefalse{aftersection}}{\filbreak}}
\newcommand{\clearpageafterfirst}{%
\gdef\clearpageafterfirst{\clearpage}%
}

\usepackage{lipsum} % to generate dummy text for the MWE

\begin{document}

\title{Title}
\author{author}
\maketitle

\section{A section}
\subsection{A subsection}\lipsum[1-2]
\subsection{A subsection}\lipsum[2]
\subsection{A subsection}\lipsum[3-4]
\subsection{A subsection}\lipsum[4]
\subsection{A subsection}\lipsum[5]
\subsection{A subsection}\lipsum[6]
\subsection{A subsection}\lipsum[7]
\subsection{A subsection}\lipsum[8]
\subsection{A subsection}\lipsum[9]

\section{B section}

\subsection{A subsection}\lipsum[12]
\subsection{A subsection}\lipsum[13]
\subsection{A subsection}\lipsum[14]
\subsection{A subsection}\lipsum[15]
\subsection{A subsection}\lipsum[16]
\subsection{A subsection}\lipsum[17]
\subsection{A subsection}\lipsum[18]
\subsection{A subsection}\lipsum[19]
\subsection{A long subsection}\lipsum[20-25]
\subsection{A subsection}\lipsum[26]

\end{document}


One can enjoy reading about \filbreak in the TeXbook. See also suppress pagebreaks in titletoc's titlecontents* blocks

One approach could be fix a variable spacing above the subsections with the titlesec package and left to LaTeX to do the best. In the MWE below is from 2.8ex to 9ex so LaTeX use a spacing of 3ex when possible, otherwise is enlarged more or less, or even reduced in a barely perceptible extent, in order to obtain the best results.

You can combine this also with a variable paragraph skips to obtain a better result (as showeed in the MWE).

Note that paragragh/subsection splitting is almost avoided completely if you apply huge spacing ranges, but I do not think it's a good idea for more than 3 or 4 lines. You should play with these values in your final text to find a reasonable range and allow paragraph breaks in some cases (i.e, check that there are not ugly page breaks after a few lines in your pages). See also How do I prevent widow/orphan lines?.

In any case, remember that you can always force large unbreakable blocks using a minipage environment or a \parblock command (manually for some final tuning or in a macro for general use, as in the MWE).

\documentclass{article}
\usepackage{lipsum} % to generate dummy text for the MWE

\usepackage{titlesec}
\titlespacing*{\subsection}{0em}{3ex plus 9ex minus .2ex}{1ex plus .0ex minus .0ex}

\setlength{\parskip}{1ex plus 3ex minus 0ex}

\newcommand\monoliticsubsec[2]{
\noindent
\begin{minipage}{\textwidth}
\subsection{#1}#2
\end{minipage}}

\begin{document}
\section{A section}
\subsection{A subsection}\lipsum[1-2]
\subsection{A subsection}\lipsum[2]
\subsection{A subsection}\lipsum[3-4]
\subsection{A subsection}\lipsum[4]
\subsection{A subsection}\lipsum[5]
\monoliticsubsec{A unbreakable section}{\lipsum[1-3]}
\subsection{A subsection}\lipsum[6]
\subsection{A subsection}\lipsum[7]
\subsection{A subsection}\lipsum[8]
\subsection{A subsection}\lipsum[9]
\subsection{A subsection}\lipsum[10]
\subsection{A subsection}\lipsum[11]
\subsection{A subsection}\lipsum[12]
\subsection{A subsection}\lipsum[13]
\subsection{A subsection}\lipsum[14]
\subsection{A subsection}\lipsum[15]
\subsection{A subsection}\lipsum[16]
\subsection{A subsection}\lipsum[17]
\subsection{A subsection}\lipsum[18]
\subsection{A subsection}\lipsum[19]
\subsection{A long subsection}\lipsum[20-25]

\end{document}


If you have a fairly consistent (maximum) subsection length, then a rudimentary approach would be to use the needspace package to insert a page break if there is less than (say) 12\baselineskip available on the page.

\documentclass{article}
\usepackage{needspace}% http://ctan.org/pkg/needspace

\let\oldsubsection\subsection
\renewcommand{\subsection}{\needspace{12\baselineskip}\oldsubsection}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
\newcommand{\dummytext}{\lorem{} \lorem{} \lorem{} \lorem{} \lorem}

\begin{document}
\section{A section}
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext
\section{Another section}\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext\dummytext
\subsection{A subsection}\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext\dummytext
\subsection{A subsection}\dummytext\dummytext\dummytext\dummytext

\end{document}


12\baselineskip is approximately 10 regular lines of paragraph text plus a \subsection heading.