20

http://www.ctan.org/tex-archive/macros/latex/contrib/hyperref/ has this note copied below:

amsmath
-------------
The environments equation and eqnarray are not supported too well. For example, there might be spacing problems (eqnarray isn't recommended anyway, see CTAN:info/l2tabu/, the situation for equation is unclear, because nobody is interested in investigating). Consider using the environments that package amsmath provide, e.g. gather for equation. The environment equation can even redefined to use gather:

\usepackage{amsmath}
\let\equation\gather
\let\endequation\endgather`

However, gather and equation environment have the spacing difference as in the answer by Will in align vs equation. Thus my question:

In a document using both hyperref and amsmath, what is the best replacement for equation?

Leo Liu
  • 5,445

4 Answers4

24

i'm writing on behalf of the maintainers of amsmath. we do maintain a list of bugs reported for both amsmath and friends and the ams document classes, and are now actively examining the list in preparation for an upgrade of the entire collection.

the spacing problem mentioned in this thread appears to be a bug, though not identical to any that have already been reported (a vertical spacing mismatch between equation and multline is on our list), so this example will be examined and added to the list if verified.

Update:

Maintenance of this package was transferred to the LaTeX Project team in 2016. Bug reports can be opened (category amslatex) at https://latex-project.org/bugs/.

  • 1
    This vertical spacing problem is not a bug in the amsmath package. It is related to the way the equation environment is redefined by the hyperref package in case of the fleqn option being set. By the way, the answer providing a patch that tries to fix this spacing problem had been posted by me before I became a registered user. – mhp Aug 23 '11 at 19:42
  • @mhp -- thanks, good information. we'll put it on our list to look into anyway; maybe we can set a trap for hyperref. – barbara beeton Aug 24 '11 at 12:50
15

You can see a spacing problem with the following example. The two equations have different spacing with hyperref, but the same spacing (as they should) without it:

\documentclass[fleqn]{article}

\usepackage{amsmath}
\usepackage{hyperref}

\begin{document}

This is a test to check the spacing of the equation environment. Too much?
\begin{equation}
a=b
\end{equation}
This is a test to check the spacing of the equation environment.

This is a test to check the spacing of the equation environment. Too much?%
\begin{equation}
a=b
\end{equation}
This is a test to check the spacing of the equation environment.

\end{document} 

But this problem is imo not enough to stop using the equation environment. The other math environments produce too much space if the sentence before the displayed math is short:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

This is a test to check the spacing of the equation environment. Too much? Foo.
\begin{equation}
a=b
\end{equation}
This is a test to check the spacing of the equation environment.

This is a test to check the spacing of the align environment. Too much? Foo.
\begin{align}
a=b
\end{align}
This is a test to check the spacing of the align environment.

\end{document} 

which is a much more common problem.

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
Martin Heller
  • 11,391
  • 1
    If you compile the top example with pdflatex you will see a difference in spacing if you compile with and without hyperref. – Martin Heller Aug 05 '10 at 21:07
  • Oh I see! Thank you. So do you think adding a % is good enough to fix the spacing issue? – ShreevatsaR Aug 05 '10 at 21:19
  • 2
    No, the percent was just to prevent triggering the problem in this specific case. Not a general fix.

    But you can fabricate other examples where adding hyperref changes the way text is typeset. That is not specific to the equation environment.

    However, I think the benefits of using the equation environment for single displayed equations outweigh the the slight risk of running into this kind of spacing modifications caused be hyperref.

    – Martin Heller Aug 05 '10 at 21:54
  • @Martin: thank you for the great answer. It seems the note on hyperref needs some update. Is this bug in hyperref or amsmath? – Leo Liu Aug 06 '10 at 04:32
  • Interestingly, I just discovered if microtype is also used, the spacing difference goes away. Can this be a workaround? – Leo Liu Aug 06 '10 at 04:43
  • Not in the general case. However, microtype normally improves the output quality and is a good idea for that reason. Hyperref inserts whatsits when creating a hyperlink and that can influence the way text is typeset. Microtype gives LaTeX some more flexibility by allowing character protusion and font expansion which also influences the formatting. In this case microtype alleviates the problem, but it is not a general fix. – Martin Heller Aug 06 '10 at 07:39
  • 1
    Thanks. For now I'll continue using equation ;) – Leo Liu Aug 06 '10 at 17:54
3

Put the following code somewhere in your document preamble:

\makeatletter
\AtBeginDocument{%
  \if@fleqn%
  \let\old@equation\equation%
  \renewcommand{\equation}{\ifhmode\unskip\fi\old@equation}%
  \fi%
}
\makeatother

This should fix the vertical spacing problem that arises when using AMSLaTeX's equation environment together with the hyperref package.

0

My rule of thumb is to always use align everywhere for math. Works with hyperref just fine =)

To suppress too much whitespace do not place a double carridge-return after the environment.

bla:
\begin{align}
a^2+b^2=c^2
\end{align}
More text straight away
Dima
  • 12,427
  • 8
  • 40
  • 53
  • The spacing difference is between text and the following equation. – Leo Liu Aug 05 '10 at 06:39
  • If you have blank line after bla there will be more space. This way there is "little" space. This is subjective of course. – Dima Aug 05 '10 at 10:43
  • 4
    It looks like you haven't tried the test file provided by Will Robertson or you failed to notice the difference. 'hello' is further away from equations produced by align than equation. – Leo Liu Aug 05 '10 at 12:30