How to fix hypertargets directing links to the line after the target? It looks like a bug

If the hypertarget is in a text-mode location, links to it work normally (they show the target line at the top of the page). However, if the hypertarget is in math mode (of any kind: in-line or in display-math or in equation arrays), then it will direct to the line after the target.

MWE

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{hyperref}

\begin{document}
\hypertarget{test1}{target1 in text mode}

Filler text.

$3 + 4 = 7 \hypertarget{test2}{}$ More filler text.

Link 2 actually directs to this line, rather than the above line, where we would expect it to do the latter.

\begin{eqnarray}
x * y = z \hypertarget{test3}{} \\
a + b = c % Link 3 actually directs to this line, rather than the above line, where we would expect it to do the latter. \\
\end{eqnarray}

\hyperlink{test1}{Clicking on this link shows target1 at the top of the page, as expected.}

\end{document}


This seems like a bug to me, but is there a better way to do this?

• Interestingly, it seems like someone has since approved my draft to the Wikibooks article. – James Ray Jan 26 '18 at 8:22
• Tagging one other who have answered questions for the hypertarget tag: @Mike. Unfortunately I can't tag more, e.g. AlexG. – James Ray Jan 26 '18 at 8:27
• Hi, welcome. You can only tag other people who have commented on (or edited) your post, so Mike wouldn't have been notified of that comment. – Torbjørn T. Jan 26 '18 at 9:05
• @ShreevatsaR, initially the question was off-topic as it didn't have a MWE and was about editing a Wikibooks article, but I have since edited it. – James Ray Jan 26 '18 at 15:56
• @JamesRay Great, thanks for clarifying. The question has been reopened now, so feel free to post an answer. – ShreevatsaR Jan 26 '18 at 18:28

This is what you can do:

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{hyperref}

\makeatletter
\makeatother

\begin{document}

Filler text.

$3 + 4 = 7 \linkdest{test2}{}$ More filler text.

Link 2 actually directs to this line, rather than the above line, where we would expect it to do the latter.

\begin{eqnarray}
x * y = z \linkdest{test3}{} \\
a + b = c % Link 3 actually directs to this line, rather than the above line, where we would expect it to do the latter. \\
\end{eqnarray}

\hyperlink{test1}{Clicking on this link shows target1 at the top of the page, as expected.}