# Spacing differs when chapter with equations extends one page

I have come across a weird phenomenom: When I compile the following code, the spacing between chapter header and the text with the equations is different than initially defined. As you can see the spacing between the chapter header of chapter 2 and the text there is smaller (as defined). But this will just occur when Chapter 1 extends one page. When Chapter 1 with the equations is just one page long (i.e. when I comment out the last two equations), the defined spacing is intact. Does anyone know how to fix that?

My code:

% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% Preamble
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

\documentclass[12pt,a4paper,twoside]{report} %openright

%Schriftart Arial - ACHTUNG: Compile with XeLaTeX (not PDFLaTeX)!
\usepackage{fontspec}
\defaultfontfeatures{Scale = MatchLowercase}
\setmainfont{Arial}[Scale = 1.0]

%Seitenlayout
\usepackage[top=2.5cm, bottom=2cm, left=2.5cm, right=2.5cm,paper=a4paper]{geometry}

%Deutsche Umlaute
\usepackage{ngerman}

%Zitierungen in der richtigen Reihenfolge
\usepackage{cite}

%Längenangaben für den Abstand zwischen zwei Absätzen.
\usepackage{parskip}

%Zeilenabstand ändern
\usepackage[onehalfspacing]{setspace}

%Farbige Texte etc
\usepackage{color}

%Graphiken und Bilder
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{psfrag}
\usepackage{wrapfig}

%Tabellenumgebung
\usepackage{tabularx}
\usepackage{booktabs} %\toprule, \midrule, \bottomrule

%Mathematische Symbole und Zeichen
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathrsfs}

%Algorithm Package
\usepackage[ruled,vlined]{algorithm2e}

%Kopfzeile
\usepackage{fancyhdr}

%Kapitelanzeige und -platzierung im Text
\usepackage[clearempty]{titlesec}
\titleformat{\chapter}{\normalfont\fontsize{16pt}{0}\bfseries}{\thechapter.}{9pt}{}
\titleformat{\section}{\normalfont\fontsize{14pt}{0}\bfseries}{\thesection}{9pt}{}
\titleformat{\subsection}{\normalfont\fontsize{12pt}{0}\bfseries}{\thesubsection}{9pt}{}
\titlespacing*{\chapter}{0pt}{-22pt}{4pt}

%Verzeichnisse
\usepackage[notindex, nottoc, numbib]{tocbibind}

%Formatierung Inhaltsverzeichnis
\usepackage{titletoc}
\contentsmargin{2em}

% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% Formatierung der Kopfzeile
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

\pagestyle{fancy}
\fancyhf{}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}

\fancypagestyle{plain}{
\fancyhf{}
}

% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% Anfang von Dokumententext
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
\begin{document}

% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
% Beginn des Inhalts
% = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
\pagenumbering{arabic}

\chapter{Einleitung}

Dies ist ein Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

\section{Mathematische Gleichungen}

Dies ist ein Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

\begin{align}\label{eq:Fuehrungsubertragungsfunktion1}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion2}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion3}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion4}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion5}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion6}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion7}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion8}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion9}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion10}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}
\begin{align}\label{eq:Fuehrungsubertragungsfunktion11}
G_w(s) = \frac{G_S(s)G_R(s)}{1+G_S(s)G_R(s)}
\end{align}

\chapter{Kapitelüberschrift}
Text
\section{Überschrift 2}
Text
\subsection{Überschrift 3}
Text
\subsection{Überschrift 3}
Text
\section{Überschrift 2}

\end{document}


Image 1: Chapter 1 extends one page, spacing of Chapter 1 is off (0.9 cm):

Image 2: Chapter 1 extends one page, spacing of Chapter 2 is intact (0.7 cm):

Image 3: Chapter 1 does not extend one page, spacing of Chapter 1 is intact (0.7 cm):

• Why \usepackage{ngerman}? It's obsolete and unmaintained. Much better is \usepackage[ngerman]{babel}. Do you really need psfrag? May 31 '19 at 21:23
• Thank you for the notes! I exchanged the language package and omitted psfrag, since I indeed don't need it in my current document, it came from the template I was given. Thanks! May 31 '19 at 22:18

This is normal behavior... but is much aggravated by one package you are using: setspace (see details below). Also note that, according to egreg:

Consecutive align environments are a capital sin.

And there are many on your first page. :-) You would normally use one such environment where each line is terminated by \\, except the last one; or several such environments, but only if there is text between them.

I'll first explain the “normal behavior” that is causing what you observed in general terms, as it applies to most documents. There are many possible remedies, which I'll describe generally too (the subject is vast), but in the end I'll show how to find and fix what is most problematic in your particular document.

What you observed is due to the fact that the LaTeX standard classes work in \flushbottom mode by default, which means that TeX is told to completely fill the body area of each page, from top to bottom (at the end of a chapter, of a part or of the document, there is an implicit \vfill to fill the remaining space, so these pages also respect the rule: they are completely filled).

When your first chapter has two pages, because of the lack of good page breakpoints, your first page is “not full enough”—in other words, it is too short—therefore vertical space on it has to stretch to fill all the available space, and TeX rightfully warns you about this:

Underfull \vbox (badness 1629) has occurred while \output is active [1]

Don't ignore warnings! The second page of your document has no problem because of the implicit \vfill at the end of the first chapter, and this is the same that happens when you shorten your first chapter to make it fit on the first page.

There are several solutions (or let's say, classes of solutions) to this problem:

• Arrange material to avoid the page being underfull (this can be done by adding legal page breakpoints or making some of them more desirable,1 adding stretchability to vertical spaces that occur on the page, rearranging material, changing layout, fonts... there are many knobs, some of which are best used in the final phase of the document preparation, or else you are likely to have to redo the same work again when the document is further modified).

• Force a page break without vertical justification using \newpage or \clearpage. Beware:

1. This may look ugly.

2. If your document layout or fonts are changed, or if you modify material before, the manually-added page break is likely to happen in quite an inappropriate place.

3. Same as above: this is to be used with parcimony, only in places where it really, really makes sense (special page layout...), and during the final phase of document preparation.

• Tell LaTeX that you don't want to have pages vertically justified. This is done with \raggedbottom, for instance in the preamble. Of course, this has serious typographical implications, as the pages of the document won't have the same height anymore. See here for an explanation of the differences between \flushbottom and \raggedbottom.

In general, the first class of solutions is preferable. An effective way is to add small, almost unnoticeable amounts of stretchability or shrinkability in vertical spaces that occur often. For instance, the default value of \parskip is 0pt plus 1pt, which means that between any two consecutive paragraphs, there is one point of stretchability (maximum) that can help achieve the page goal (that is, the height of the text body).

You can add such “little bits of vertical stretchability or shrinkability” that help achieve the page goal when you define the spacing before or after \chapter, \section, etc. For instance, one of the examples in appendices of the titlesec documentation reads:

\titlespacing{\section}{12pc}{1.5ex plus .1ex minus .2ex}{1pc}


This gives the vertical space preceding a section title a natural length of 1.5ex with 0.1ex of stretchability and 0.2ex of shrinkability (the specified amount of stretchability can be exceeded, but TeX will be reluctant to do so and will give underfull \vbox warnings if this happens; the maximum shrinkability can't be exceeded).

Other effective ways to add flexibility to your vertical spacing and help TeX find good breakpoints between pages are:

• using TeX's \smallskip, \medskip and \bigskip commands instead of \vspace{〈dimen〉} (a 〈dimen〉 is a rigid length). You can of course give \vspace a rubber length (a 〈skip〉 in TeX parlance), but \smallskip, \medskip and \bigskip are very convenient and often enough.

• using \smallbreak, \medbreak and \bigbreak: these are similar to \smallskip, \medskip and \bigskip, but they also declare that the corresponding place (end of a paragraph) is a slightly, moderately or quite desirable breakpoint between pages (the corresponding \penalty values are -50, -100 and -200 respectively);

• using display formulas,2 list environments or similar (the \topsep and/or \partopsep glue parameters contribute vertical stretchability and shrinkability; \partopsep is extra space added to \topsep when a list environment such as itemize, enumerate, etc. is started when in vertical mode [which means most of the time: is preceded by a blank line]);

• avoiding uses of unbreakable elements (boxes) that occupy significant vertical space (such as images and tables) and are directly included in the document flow, i.e.: are not inside a figure or table environment; this also applies to minipage environments and \parbox commands, to unbreakable tcolorbox or similar environments, etc.: all these, if they happen to fall at the bottom of a page, make the task of vertical justification impossible to achieve without excessive stretching of vertical space on the page, and lead to the same problem as you encountered in your question whenever \flushbottom is in use.

You may also want to read this answer of David Carlisle regarding “bad boxes” in general (horizontal or vertical ones).

Advanced users may be interested chapter 15 of the TeXbook (How TeX Makes Lines into Pages), in particular the \tracingpages parameter which allows one to understand why TeX chose to break a page at a given point (when using \tracingpages=1, TeX prints all of its page-cost calculations to the log file; these explain why the chosen breakpoint was the best feasible one according to TeX's rules).

# Problems particular to your document

## Spacing around display formulas

Normally, your first page should be extremely easy for TeX to fill because of the many display formulas it has. Indeed, as said above, display formulas are normally preceded and followed by vertical space that can stretch or shrink. However, examining your first page with \showoutput reveals that it has many \abovedisplayskip and \belowdisplayskip globs of glue, which is quite normal because of all the align environments, but all these globs of glue are completely rigid!

...\glue(\belowdisplayskip) 12.40997
...\penalty 10000
...\glue(\abovedisplayskip) 12.40997


Therefore, they don't help TeX in any way to break the page. With a more standard document such as:

\documentclass{report}
\usepackage{amsmath}

\begin{document}
\showthe\abovedisplayskip
\showthe\belowdisplayskip
\showthe\abovedisplayshortskip
\showthe\belowdisplayshortskip
\end{document}


one gets on the terminal:

> 10.0pt plus 2.0pt minus 5.0pt.
l.5 \showthe\abovedisplayskip

> 10.0pt plus 2.0pt minus 5.0pt.
l.6 \showthe\belowdisplayskip

> 0.0pt plus 3.0pt.
l.7 \showthe\abovedisplayshortskip

> 6.0pt plus 3.0pt minus 3.0pt.
l.8 \showthe\belowdisplayshortskip


All of these vertical spaces relevant to display formulas are thus stretchable normally, and all of them except \abovedisplayshortskip are also shrinkable. But not in your document! I would say that this is your main problem, even more important than the lack of flexibility around \chapter and \section, which can be arranged as I said above with \titlespacing or \titlespacing* from titlesec (see below).

Why do you have rigid lengths around display formulas? Because of the setspace package. It spoils all the nice typographical settings LaTeX has by default. :-( If you comment out your \usepackage[onehalfspacing]{setspace}, you'll see that \abovedisplayskip and \belowdisplayskip will regain stretchability and shrinkability:

...\glue(\belowdisplayskip) 12.0 plus 3.0 minus 7.0
...\penalty 10000
...\glue(\abovedisplayskip) 12.0 plus 3.0 minus 7.0


(output obtained with \showoutput before \chapter{Einleitung} and \hideoutput after \chapter{Kapitelüberschrift})

Note: you can also print the values on the terminal with \showthe\abovedisplayskip and \showthe\belowdisplayskip.

In case you really must use setspace (I know this happens...), you can add:

\setdisplayskipstretch{}


after your \usepackage[onehalfspacing]{setspace} line, as indicated in Mico's answer here. This avoids the apparently intentional nuking of the stretchability and shrinkability of the four kinds of spaces (\(above|below)display[short]skip) around display formulas done by setspace. Of course, this might make the spaces in question a bit stretchy or shrinky—I'll leave it up to you to appreciate how it looks.

More generally concerning your LaTeX code, keep in mind that it's a good idea not to load more packages than what you really need.

## Spacing around sectioning commands

As I said, since you use the titlesec package, adding some stretchability and/or shrinkability to vertical spaces preceding or following sectioning commands is easy. For instance, you could replace this line:

\titlespacing*{\chapter}{0pt}{-22pt}{4pt}


with:

\titlespacing*{\chapter}{0pt}{-22pt plus 0.1ex minus 0.1ex}{4pt plus .1ex}
\titlespacing*{\section}{0pt}{2.3ex plus 0.7ex minus .15ex}{1.2ex plus .1ex}


to add some elasticity. And you could do similarly for lower levels. But again, in your first page, the main problem is caused by the rigid \abovedisplayskip and \belowdisplayskip lengths, because you are using the setspace package.

Footnotes

1. See the optional argument of \pagebreak, or more generally TeX's \penalty command when used in vertical mode.

2. Those between $ and $, amsmath's align or gather environments, etc. Reminder: never put a blank line in the source file before $; display formulas are supposed to interrupt what Donald E. Knuth calls a paragraph-so-far. It is quite correct to continue the paragraph after $, without leaving any blank line in the source file (otherwise, you'll be starting a new paragraph).

• In case you didn't see it, after a rather general description of the problem and ways to solve it, I have investigated issues that are specific to your document, essentially your use of the setspace package, which is nuking all elasticity in the vertical spaces around your display formulas (here, the multiple align environments). May 31 '19 at 21:22
• Consecutive align environments are a capital sin. May 31 '19 at 21:29
• First of all a big thank you for the time and the so detailed answer, I am overwhelmed by your effort, thank you! May 31 '19 at 23:15
• I still have some questions: If I omit the package setspace, do you have any recommendation how to adjust the spacing between the lines but preserve the now obtained flexibilty? And should I adjust \titlespacing with plusand minuseither way? And if yes, is there a recommendation for \titlespacing*{\subsection}{...}, or should I define what I think is best? May 31 '19 at 23:22
• (Also thank you A LOT for all the links etc., I have learnt so much! :-) Also thanks to @egreg for pointing out the nonsense of the multiple align environments, this was due to the template I got and now have to adjust to my needs. I just copied, pasted and multiplied one part without rhyme or reason to demonstrate my problem, and haven't understood the syntax of align - now I remember how it works :-) :-)) May 31 '19 at 23:29