3

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.}

\hyperlink{test2}{Link 2.}

\hyperlink{test3}{Link 3.}

\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
  • 1
    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
  • 1
    @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
  • 1
    @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
2

This is what you can do:

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

\makeatletter
 \newcommand{\linkdest}[1]{\Hy@raisedlink{\hypertarget{#1}{}}}
\makeatother

\begin{document}
\linkdest{test1}target1 in text mode.

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.}

\hyperlink{test2}{Link 2.}

\hyperlink{test3}{Link 3.}

\end{document}

testing a macro to modify a hypertarget to display properly

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.