2

I have a chapter with many sections, but they are of varying lengths. I want a new section to appear right after the previous one finishes (which happens normally) -unless- two sections end up on the same page. Right now, I'm manually adding \newpage if two sections appear on the same page. Is there a global way to do this? I'm working in the book class.

\documentclass[12pt]{book}
\begin{document}
\chapter{thefirstchapter}
\section{long section}
\section{long section}
\section{short section}
\newpage
\section{long section}
\section{short section}
\newpage
\section{long section}
\end{document}

2 Answers 2

1

The following approach uses TeX's \label-\ref system to check whether a \section already appears on the current page. It does by by creating/examining a macro called \section-on-page-<pnum> (where <pnum> is the page of the section being set). If this macro exists for a given <pnum> (checked via \ifcsname <csname>\endcsname), a \section already exists on that <pnum> and issues a \newpage. If it doesn't exist, it sets the \section like before and (re)creates \section-on-page-<pnum>.

\documentclass{book}

\usepackage{lipsum,refcount}

\NewCommandCopy{\oldsection}{\section}
\RenewDocumentCommand{\section}{ s o m }{%
  \label{before-section-\thesection}% Set \label for section
  % Check if there is already a section on the current page by
  % examining the existence of \section-on-page-<pnum>
  \ifcsname section-on-page-\getpagerefnumber{before-section-\thesection}\endcsname
    \newpage% Action if a section already exists
  \fi
  \IfBooleanTF{#1}
    {\oldsection*{#3}}% \section*{..}
    {\IfValueTF{#2}
      {\oldsection[#2]{#3}}% \section[.]{..}
      {\oldsection{#3}}% \section{..}
    }%
  \label{section-\thesection}% Set new label for section (could be on a following page)
  % Update \section-on-page-<pnum>
  \expandafter\xdef\csname section-on-page-\getpagerefnumber{section-\thesection}\endcsname{Section on page \getpagerefnumber{section-\thesection}}%
}

\begin{document}

\chapter{A chapter}
\section{Long section}\lipsum[1-10]
\section{Long section}\lipsum[1-10]
\section{Short section}\lipsum[1]
\section{Long section}\lipsum[1-10]
\section{Short section}\lipsum[1]
\section{Long section}\lipsum[1-10]

\end{document}

Before:

enter image description here

After:

enter image description here

Page numbers of a \reference is extracted using the functionality provided by refcount.

Since the \label-\ref system is used, changes in the document layout may require multiple runs. The construction of the document, and therefore also the conditional issuing of \newpage, is sequential. So, one \newpage can cause a ripple effect throughout the document. Keep compiling until all references have settled.

0

Based on this entry, I started to use \clearpage{} instruction instead of \newpage{}. Because the former not only starts a new page, but ensures (still floating) objects like tables, and figures of the old section to come up before entering a new section. (Otherwise it may be distracting the reader if for example an experimental section contains tables from the theoretical section because LaTeX's floating algorithm assumes «and now here is enough space for it».)

By this the new section starts on top of a new page.

Or, you split your document -- each section its own file, eventually brought together into a main document with the \include{file} pattern.

3
  • I read the link but don't understand what they mean by float. Anyway, \newpage works fine for what I am doing. But I don't want to have to manually add it in. See, I have 200 sections. Is there some way to say "if two section titles appear on one page, insert \newpage between them"? Commented Mar 16, 2020 at 21:52
  • I start to see. Conceptualy, one could address the problem about LaTeX's counter; one of the default counters is about page(s), to reset this counter per each section's start to zero, and to read-out the page count at the end of each section. If the value were less than 1, then probably a \newpage instruction should follow to separate this from the next section to follow. While there are if-loops in TeX (tex.stackexchange.com/questions/286902/latex-if-condition), I do not know if this approach equally breaks the page counting in the bottom of the page as well.
    – Buttonwood
    Commented Mar 16, 2020 at 22:35
  • 1
    I don't think this answers the question Commented Mar 17, 2020 at 0:58

You must log in to answer this question.

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