# Incorrect spacing after minus sign with combination of amsmath and hyperref

When no packages are loaded, no space is inserted after the minus sign of a negative number:

\documentclass{article}
\begin{document}
$$-1 = -1 % output: -1 = -1$$
\end{document}


However, when both amsmath and hyperref are used, a space is inserted in the negative number which starts the content of an equation environment:

\documentclass{article}
\usepackage{amsmath}
\usepackage{hyperref}
\begin{document}
$$-1 = -1 % output: - 1 = -1$$
\end{document}


In the latter example, the minus sign seems to be treated as subtraction instead of negative.

How can I get the same result as the former example when the two packages are loaded? I've found that align environments do not have this problem, but I hear that using align instead of equation is bad.

Output example with for the second case:

• good catch, it appears you have found an hyperref bug. – user4686 Mar 29 '15 at 14:19
• and there is also an amsmath bug with the multline environment, which manifests itself as in your MWE, although it does not have the same cause (being present without hyperref). See my answer for proposed patches. – user4686 Mar 29 '15 at 15:24

edit adds a patch to another, pure amsmath, problem; see bottom of answer

Not an easy one. First of all indeed hyperref should always be loaded after amsmath because it does things depending on whether amsmath is loaded, but does not delay this to At Begin Document.

The problem of a unary minus is already taken care of by hyperref's redefinition of \equation (code comment near lines 7553 of hyperref.pdf)

\mathopen is needed in case the equation starts with an unary minus, for example.

but this redefinition is done only if amsmath is not loaded. In the case hyperref detects amsmath it modifies the amsmath definition in a different way, moving the \refstepcounter and ensuing hyperlink business to the start of the formula, to avoid putting things in the surrounding vertical list. However it appears that hyperref forgot in that case to also add the \mathopen thing.

Here is a proposed patch.

not tested in any thorough way it would be better to get hyperref to patch only equation etc...

\documentclass{article}
\usepackage{amsmath}
% always load hyperref after amsmath
\usepackage{hyperref}
\makeatletter
\def\hyper@refstepcounter#1{%
\edef\This@name{#1}%
\ifx\This@name\name@of@eq
\@ifundefined{theHequation}{%
\make@stripped@name{\theequation}%
\let\theHequation\newname
}{}%
\fi
\HyCnt@ProvideTheHCounter{#1}%
\hyper@makecurrent{#1}%
\ifmeasuring@
\else
%%  \ifmmode\mathopen\bgroup\fi  %% first version of the patch (line I)
\hyper@anchorstart{\@currentHref}\hyper@anchorend
}%
%%  \ifmmode\egroup\fi           %% first version of the patch (line II)
\fi
\ifmmode\mathopen{}\fi           %% second version: THIS LINE ADDED IN PATCH
}
\makeatother
\begin{document}
$$-1 = -1 % output is now OK$$
\end{document}


A similar problem non-related to hyperref exists in amsmath, with the \multline environment, as pointed out in comments. I can propose the following patch, with no guarantee as this is a job for someone who knows all the innards of amsmath already.

The patches are done with the help of etoolbox.

about this patch I am not sure why amsmath has \displaystyle{} and not \displaystyle alone which would not create the problem; an alternative patch would be to simply replace \displaystyle{} by \displaystyle, but there might be some reason for the \displaystyle{} (compare the \shoveleft definition in amsmath.sty).

\documentclass{article}
\usepackage{amsmath}

% un deuxième problème a émergé qui n'est pas lié à hyperref.

\usepackage{etoolbox}

\begin{document}\thispagestyle{empty}

Before the patch:
\begin{multline}
-1 = -1\\ -2 = -2
\end{multline}

\makeatletter
\patchcmd
{\multline@}{\displaystyle{}}{\displaystyle{}\mathopen{}}
{\typeout{multpatch SUCCESS}}
{\typeout{multpatch FAILURE}}

\patchcmd
{\mmeasure@}{\displaystyle{}}{\displaystyle{}\mathopen{}}
{\typeout{mmpatch SUCCESS}}
{\typeout{mmpatch FAILURE}}

\makeatother

After the patch
\begin{multline}
-1 = -1\\ -2 = -2
\end{multline}
\end{document}


• This should be done also when \ifmeasuring@ is true or the lengths will be computed in a wrong way. Why not simply adding \mathopen{} after the trailing \fi? – egreg Mar 29 '15 at 14:02
• @egreg The location of Hy@raisedlink in the false branch is the original location from hyperref.sty. – user4686 Mar 29 '15 at 14:03
• Well, it doesn't matter, does it? – egreg Mar 29 '15 at 14:05
• @egreg Perhaps, but I wasn't into adventuring into other modifications; could not exclude the hyperref authors had their reasons. I have modified the proposed patch, adding the \mathopen{} after the conditional. – user4686 Mar 29 '15 at 14:09
• Then a simpler fix might be adding \mathopen{} at the end of \equation. – egreg Mar 29 '15 at 14:40

Force the unary minus to be of type \mathord either via \mathord{-}, or {-}:

\documentclass{article}
\usepackage{amsmath,hyperref}
\begin{document}
$${-}1 = -1 % output: -1 = -1$$
\end{document}


Most likely this stems from hyperref's insertion of content at the start of the horizontal list for (hyper-) referencing. As such the first - is treated as a binary operator. The bracing reverts it back to the view that resembling the unary operator.

• It's interesting that this problem occurs with the equation and multline environments, but not with the align and gather environments. I would assume (though I can't prove it) that this is a bug in hyperref. – Mico Mar 29 '15 at 8:55
• @Mico It doesn't happen for the alignment environments, because the stepping of the equation counter doesn't happen at the start of the (sub)formulas, which are typeset as cells in a \halign. – egreg Mar 29 '15 at 14:07

Try to use eqnarray instead of equation! It solves your problem. :)

\begin{eqnarray}
-1 = -1 % output: -1 = -1
\end{eqnarray}