0

So i'm keeping my maths- and physics-formulas together in simple formularies in LaTeX. Right now i'm using the article class and sections / subsections for my structure, where each subsection / subsubsection stands for one formula with description:

\documentclass{article}

\begin{document}

\section{Ordinary Differential Equations}

\subsection{Solution Procedure for Ordinary Differential Equations
Some Text that describes how to solve odes
\subsection{Different Topic}
...

\end{document}

Everything is working fine and looking nice, however there's one thing that's annoying me:

I want to look on my formulas as whole! LaTeX is constructed to break subsections over multiple pages whenever the page is full, so i have to keep skimming over two pages because some formula / description was broken over automatically!

Is there something to add to my preamble that tells LaTeX to print subsections, so that the whole subsection is displayed on the same page? If not so, is there some other option (other documentclass and such) that i was missing out previously? Manual pagebreaks or newpage-commands seem to be just an ugly hack.

Bonus: if my desired behaviour is possible, what would LaTeX do, if one subsection doesn't fit on a single page?

All help is highly appreciated. Thanks in advance!

3
  • You could grab the contents of a subsection (which might be hard -- what is the end of a subsection) and check its size. If that is bigger than a full \textheight just print it using default pagebreaks, if it's smaller than the remaining space on the page print it, else \clearpage and print it.
    – Skillmon
    Apr 5, 2018 at 15:44
  • Desired behaviour is that the page is broken when the next subsection does not fit on the current page! So, i don't have a problem with multiple subsections on the same page but with broken subsections over multiple pages!
    – Tim Hilt
    Apr 5, 2018 at 15:49
  • The above would do that. It just wouldn't break the page if the next subsection wouldn't fit only on the next full page either.
    – Skillmon
    Apr 5, 2018 at 15:51

1 Answer 1

0

Grabbing the contents of a subsection seems not very reliable/stable without any change to the document. Therefore I built an environment (ssecenv) that typesets its contents in a box and checks the vertical space needed against the space left on the page.

The environment takes an optional star, an optional argument, and a mandatory argument -- just like the \subsection command.

\documentclass[]{article}

\usepackage{letltxmacro}
\usepackage{duckuments}
\usepackage{xparse}

\makeatletter
\newbox\tim@ssec
\newdimen\tim@spaceleft
\newdimen\tim@spaceneeded
\NewDocumentEnvironment{ssecenv}{ >{\tim@sProcessor}s >{\tim@OProcessor}O{} m }
  {%
    \par
    \setbox\tim@ssec\vbox\bgroup
      \subsection#1#2{#3}%
  }
  {%
    \egroup
    \tim@spaceleft=\textheight
    \advance\tim@spaceleft-\pagetotal
    \tim@spaceneeded=\ht\tim@ssec
    \advance\tim@spaceneeded\dp\tim@ssec
    \ifdim\tim@spaceneeded>\textheight
    \else
      \ifdim\tim@spaceleft<\tim@spaceneeded
        \clearpage
      \fi
    \fi
    \unvbox\tim@ssec
  }
\newcommand\tim@sProcessor[1]
  {%
    \IfBooleanTF{#1}{\def\ProcessedArgument{*}}{\def\ProcessedArgument{}}%
  }
\newcommand\tim@OProcessor[1]
  {%
    \if\relax\detokenize{#1}\relax
      \def\ProcessedArgument{}%
    \else
      \def\ProcessedArgument{[#1]}%
    \fi
  }
\makeatother

\begin{document}
\begin{ssecenv}[bar]{foo}
  \blindduck
\end{ssecenv}
\begin{ssecenv}*{foo}
  \blindduck
\end{ssecenv}
\begin{ssecenv}*[bar]{foo}
  \blindduck
\end{ssecenv}
\begin{ssecenv}{foo}
  \blindduck
  \blindduck
\end{ssecenv}
\end{document}

You must log in to answer this question.

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