[Tex/LaTex] fleqn document class option, long text lines, and hyperref package

equationshyperrefspacing

When the fleqn option is used, the vertical spacing before an equation placed right after a "long" text line (exactly \linewidth I would say) is improperly set up. See below:

enter image description here

From the mathmode user guide, this could probably be fixed through the \topsep length.
Is it the right way to proceed? The problem is reproduced by the code below:

\documentclass[12pt,fleqn]{book}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
\chapter{plo}
\section{plo}
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaem oeiu eoiu eoiu oeee.
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaemm
\begin{equation}
\cos\pi=-1
\end{equation}
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaem oeiu eoiu eoiu oeee.
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùae
\begin{equation}
\cos\pi=-1
\end{equation}
\end{document}

There is something wrong involving hyperref, amsmath, and fleqn. Removing one of those removes the problem.

Best Answer

The last line before the first equation ends with a space. This is usually not a problem, because TeX ends the text before the displayed equation similar to a paragraph end. That means, the last space is removed and \parfillskip is added.

However, package hyperref has to insert whatsits somewhere at some time. It hooks into \refstepcounter, that is called right before displaying the equation to update the equation counter. hyperref uses this to add an anchor that is needed as link target, if the equation if referenced.

However, then the last space of the previous text cannot be removed, because the whatsit/anchor goes inbetween, hiding the space. Because the text fills the previous line, the result is a line break, followed by an invisible line with the anchor. The space is removed at the start of the new line, but the invisible line remains, consuming space.

As quick workaround, the space can be removed by a percent line:

...aemm%
\begin{equation}

Better is a fix that does remove the space automatically and fixes the position of the anchor, because the position at the end of the previous line is not a good place.

\documentclass[12pt,fleqn]{book}
\usepackage{amsmath}
\usepackage[colorlinks]{hyperref}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\equation}{\incr@eqnum\mathdisplay@push}{%
  \ifhmode
    \unskip
    \vadjust{%
      \incr@eqnum
      \global\let\gfix@incr@eqnum\incr@eqnum
      \global\let\gfix@currentlabel\@currentlabel
      \global\let\gfix@currentHref\@currentHref
    }%
    \let\incr@eqnum\gfix@incr@eqnum
    \let\@currentlabel\gfix@currentlabel
    \let\@currentHref\gfix@currentHref
  \else
    \incr@eqnum
  \fi
  \mathdisplay@push
}{}{}
\makeatother

\begin{document}
\chapter{plo}
\section{plo}
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaem oeiu eoiu
eoiu oeee.
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaemm
\begin{equation}
\label{eq:first}
\cos\pi=-1
\end{equation}
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùaeoiuaem oeiu eoiu
eoiu oeee.
eoiu oieu oeiuoe ueoi ueoi ueoiu eoiu oeu oe iupaêio umùae
\begin{equation}
\label{eq:second}
\cos\pi=-1
\end{equation}
Equations \ref{eq:first} and \ref{eq:second}.
\end{document}

Result