[Tex/LaTex] Vertical space between section header and text is too big

sectioningspacing

\documentclass[12pt,a4paper]{mwrep}
\usepackage{polski}
\usepackage[utf8]{inputenc}
\usepackage{amsfonts}
\usepackage[draft]{graphicx}
\usepackage{amsmath}
\usepackage[nodisplayskipstretch]{setspace} \setstretch{1.5}
% Make all figure use 'ht' position by default:
\makeatletter
\def\fps@figure{ht}
\makeatother

\begin{document}
\section{Lorem ipsum}
\emph{Lorem ipsum} dolor sit amet, consectetur adipiscing elit. Vestibulum in scelerisque velit. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero.
\begin{equation}
Z=
\begin{bmatrix} z_{11} & z_{12} & \cdots & z_{1n} \\
        z_{21} & z_{22} & \cdots & z_{2n} \\
        \vdots & \vdots & \ddots & \vdots \\
        z_{n1} & z_{n2} & \cdots & z_{nn}
\end{bmatrix}
\end{equation}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in scelerisque velit. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero.
\begin{gather}
E = mc^2 \\
a^2+b^2=c^2
\end{gather}

\emph{Lorem ipsum} dolor sit amet, consectetur adipiscing elit. Vestibulum in scelerisque velit. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero. Praesent ac mi libero.
\begin{figure}
\centering
\setlength\fboxsep{124pt}
\setlength\fboxrule{1pt}
\fbox{\includegraphics[scale=0]{dummy.jpg}}
\caption{Dummy caption}
\end{figure}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \textbf{In sed tortor sit amet magna eleifend egestas.}
\begin{equation}
P=
\begin{bmatrix} 0.1 & 0.3 & 0.0 & 0.6 \\
                0.2 & 0.4 & 0.1 & 0.3 \\
        0.0 & 0.8 & 0.0 & 0.2 \\
        0.2 & 0.0 & 0.2 & 0.6
\end{bmatrix}
\end{equation}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum in scelerisque velit. Praesent ac mi libero.

\section{Sed eu neque ligula.}
\emph{Sed eu neque ligula.} Mauris gravida risus et eros aliquet viverra. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam vel nisi eu mauris fermentum dictum. Ut erat dui, aliquam nec cursus vel, scelerisque a ipsum. Curabitur ut est est. Aenean quis felis a orci sodales varius nec non est. Sed in augue eget orci elementum interdum vel et velit. Vestibulum sed condimentum lacus. 
\end{document} 

This is sample layout of my document. Sorry It's so long but I was not able to reproduce the problem on smaller sample.

The problem is huge space between section header and text. Why bolded sentence (In sed tortor sit amet magna eleifend egestas.) is not on single page? It would look better and vertical space between header and text would be smaller.

How can I fix this and this vertical space?

Using usepackage[headsep=0pt]{geometry} as in How to remove vertical space between header and body of text? didn't help.

I need figures to be at ht. If there is no way to aviod vertical space, I'd rather want it at the bottom of the page instead of the way it is now.

Best Answer

That sentence falls in the second page because LaTeX sets

\predisplaypenalty=10000

in order that a math display doesn't go on the top of a new page, but is accompanied by at least one line of text.

The figure has nothing to do with this: the result is the same if you remove it, because of the big matrix which can't be squeezed in the previous page. Use the

\raggedbottom

declaration before \begin{document}. The final result will be poor, but in a double spaced document the huge space between lines will gather the reader's attention and cover your tracks. :)

Related Question