0

When I click a link to a word created with the hyperref package, I am directed to the line in the text below the link if the word is in the middle of the line. If I create link to a word which is the first on its line, I am directed to the correct line. Is this the expected behaviour? It definitely isn't optimal. Does anybody know of a workaround, other than making sure that the linked word is the first on a line? Here is an example:

\documentclass{minimal}
\usepackage{lipsum}
\usepackage{color}
\usepackage{hyperref}
\setlength{\parindent}{0pt}
\begin{document}
\lipsum[1] 
The target is \hypertarget{target}{\textcolor{red}{here}}. 
\lipsum[2]

This is a \hyperlink{target}{\textcolor{blue}{link}}.

\lipsum[3] 
\hypertarget{newtarget}{\textcolor{red}{Here}} is a new target. 
\lipsum[4]

This is a new \hyperlink{newtarget}{\textcolor{blue}{link}}.
\end{document}
Rasmus
  • 141
  • 2
    this seems to be a duplicate of the "related" link https://tex.stackexchange.com/questions/77919/vertical-position-after-navigating-to-hyperref-hypertargets-from-hyperlinks-seem?rq=1 (the target anchor is placed on the baseline of the point that you mark) – David Carlisle Mar 03 '18 at 21:41

1 Answers1

2

You might find this useful.

\documentclass{minimal}
\usepackage{lipsum}
\usepackage{color}
\usepackage{graphicx}
\usepackage{hyperref}
\setlength{\parindent}{0pt}

\newcommand{\raisetarget}[2]% #1 = label, #2 = contents
{\bgroup
  \sbox0{#2}%
  \raisebox{\ht0}{\hypertarget{#1}{}}\usebox0%
\egroup}

\begin{document}
\lipsum[1] 
The target is \raisetarget{target}{\textcolor{red}{here}}. 
\lipsum[2]

This is a \hyperlink{target}{\textcolor{blue}{link}}.

\lipsum[3] 
\raisetarget{newtarget}{\textcolor{red}{Here}} is a new target. 
\lipsum[4]

This is a new \hyperlink{newtarget}{\textcolor{blue}{link}}.\strut% no descenders

\raisetarget{anothertarget}{\includegraphics[scale=0.5]{example-image}} is a another target. 

This is another \hyperlink{anothertarget}{\textcolor{blue}{link}}.
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • minimal? Oh, no! ;-) – egreg Mar 04 '18 at 20:59
  • @egreg - I tried changing it back to article, but it went to two pages. – John Kormylo Mar 05 '18 at 00:27
  • @John Kormylo I used a solution from here https://tex.stackexchange.com/questions/17057/hypertarget-seems-to-aim-a-line-too-low but your solution seems to work fine as well. – Rasmus Mar 06 '18 at 19:22
  • 1
    I looked up the definition of \Hy@raisedlink. It seems to use \raise\baselineskip instead of measuring the height, so would not work on graphics or other large boxes. – John Kormylo Mar 06 '18 at 20:35