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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI'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?
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
}
\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.
\pagebreak[0]
instead of \clearpage
? This would of course not manage the floats, but maybe floats are not an issue here.
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}