1

edit My google-fu is apparently sub-par, this has a simple solution found here: Section should automatically start new page if the whole section does not fit on current page

I want to force each section to start on a new page if the whole section doesn't fit on the remainder of the page.

I have tried to use minipages, but they fail when the text exceeds the space available on the page. (I want to allow the sections to overflow into another page only if they are the only thing on that page).

this doens't work either:

\begin{samepage}
\section{Some title}
\lipsum[1-5]
\end{samepage}
\begin{samepage}
\section{Some title}
\lipsum[1-4]
\end{samepage}
\begin{samepage}
\section{Some title}
\lipsum[1-30]
\end{samepage}
Pownyan
  • 111

1 Answers1

0

The following does what you want. It has some important restrictions, though:

  1. No floats are possible inside Sections.
  2. Sections can't get arbitrarily long, the maximum is 16383.99998 pt in total height.

Not sure if there are more restrictions to it.

\documentclass[]{article}

\makeatletter
\newsavebox\Section@box
\newenvironment*{Section}
  {%
    \par
    \setbox\Section@box\vbox\bgroup
    \@ifnextchar[
      \Section@
      \Section@@
  }
  {%
    \egroup
    \ifdim\pagetotal=0pt
    \else
      \ifdim\pagegoal<\dimexpr\ht\Section@box+\dp\Section@box\relax
        \clearpage
      \fi
    \fi
    \unvbox\Section@box
  }
\long\def\Section@[#1]#2%
  {%
    \section[{#1}]{#2}%
  }
\newcommand\Section@@[1]
  {%
    \section{#1}%
  }
\makeatother

\usepackage{lipsum}

\begin{document}
\begin{Section}{Some title}
  \lipsum[1-5]
\end{Section}
\begin{Section}{Some title}
  \lipsum[1-4]
\end{Section}
\begin{Section}{Some title}
  \lipsum[1-30]
\end{Section}
\end{document}
Skillmon
  • 60,462