# How can I tell LaTeX to only make pagebreaks on new sections?

I'm working on a document where it's important that sections are in the same page.

How can I tell LaTeX to only make a page break (if necessary) when I write a new \section without having to manually add \newpages manually?

-
What happens, if the contents of a section is longer than one page? Or what happens, if two sections would fit on one page? –  Heiko Oberdiek Sep 17 '12 at 22:14
There should be a page break inside a section only if the content of a section is longer than a page. If two sections fit in one page then there shouldn't be one (that's where the 'if necessary' comes in). I'm not very experienced in Tex and my question may be a bit confusing because I probably don't know most correct terms, so please edit it if you know better ones. –  Martín Fixman Sep 17 '12 at 22:37

Macro \section can be redefined to add a \newpage or \clearpage. The latter also forces pending float object to be placed.

% Save the old meaning of \section in \OrgSection
\newcommand*{\OrgSection}{}%
\let\OrgSection\section
% Redefine \section to add \clearpage at the beginning
\renewcommand*{\section}{%
\clearpage
\OrgSection
}

-
I think you didn't understand my explanation. If two sections fit on the same page, then there shouldn't be any page breaks. Wouldn't your code "force" a page break for every new section? –  Martín Fixman Sep 17 '12 at 23:04
@MartínFixman: Yes, that the case with this solution. Would your problem be described as follows: Insert a \newpage (or equivalent) if the start and end of a section is on different pages. This would not insert a \newpage if you have a section ending at the top of a page and then a short section that would fit on that page. –  Werner Sep 17 '12 at 23:25
@MartínFixman Yes, I had read the second sentence "... page ... there shouldn't be one" as there should not be a "page", you meant "page break". –  Heiko Oberdiek Sep 17 '12 at 23:41
@Werner Apart from complications like footnotes and floats. It is not enough to know the start and end page, also the length for the short sections need to be measured. Then it is known, whether two small sections can be put on one page. –  Heiko Oberdiek Sep 17 '12 at 23:47
What about \pagebreak[0] instead of \clearpage? This would of course not manage the floats, but maybe floats are not an issue here. –  alexurba Sep 18 '12 at 8:08

With some arrangements, it could be done with a minipage environment whenever the section does not fill more than one page. In other case the text fill the bottom margin without page breaks as in the last section of the MWE (see below).

However, I think that a general rule of thumb is suggest to LaTeX where to break the page, through a \pagebreak[3] (or some less than 3) just before of each section, and where do not break, setting high values in \clubpenalty and \windowpenalty, but let to LaTeX find the better place to break the pages. Then, if the really final result is not satisfactory at some point, you always can use the brute force (a \pagebreak[4], or a \newpage or \clearpage if there are floats, as mention Heiko Oberdiek) where (and only where) really it is needed.

\documentclass{article}
\usepackage{lipsum} % dummy text

\makeatletter
\renewcommand{\section}{
~\par\@startsection
{section}%                   % the name
{1}%                         % the level
{0mm}%                       % the indent
{2\baselineskip}%            % the before skip
{1\baselineskip}%          % the after skip
{\Large\bfseries}} % the style
\makeatother

\begin{document}

\section{Lore ipsum}
\lipsum[1]

\noindent\begin{minipage}{\textwidth}
\setlength{\parindent}{2em}
\section{Lore ipsum}
\lipsum[2-5]
\end{minipage}

\noindent\begin{minipage}{\textwidth}
\setlength{\parindent}{2em}
\section{Lore ipsum}
\lipsum[6]
\end{minipage}

\noindent\begin{minipage}{\textwidth}
\setlength{\parindent}{2em}
\section{Lore ipsum}
\lipsum[7]
\end{minipage}

% Ooops .... too long for a **mini** page
\noindent\begin{minipage}{\textwidth}
\setlength{\parindent}{2em}
\section{Lore ipsum}
\lipsum[8-15]
\end{minipage}

\end{document}

-