18

It is possible to make a hyperref to go to page x, but how we can define a custom anchor to go a destination line?

For example:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
  \lipsum[1]
  \lipsum[2]
  THE DESTINATION LINE (anchor x)
  \lipsum[3]
  \lipsum[4]
  CLICK HERE (go to anchor x)
  \lipsum[5]
\end{document}

How to click on CLICK HERE and goes to the DESTINATION LINE? I mean displaying THE DESTINATION LINE as the first line in the pdf reader.

Googlebot
  • 4,035

2 Answers2

25

Use either \hypertarget + \hyperlink or \phantomsection with a \label and \hyperref:

\documentclass{article}
\usepackage{lipsum,hyperref}

\begin{document}
  \lipsum[1]
  \lipsum[2]
  \phantomsection\label{here}THE DESTINATION LINE (anchor x)
  \lipsum[3]
  \lipsum[4]
  \hyperref[here]{CLICK HERE} (go to anchor x)
  \lipsum[5]

  \lipsum[1]
  \lipsum[2]
  \hypertarget{here2}{}THE DESTINATION LINE (anchor x)
  \lipsum[3]
  \lipsum[4]
  \hyperlink{here2}{CLICK HERE} (go to anchor x)
  \lipsum[5]
\end{document}
Ulrike Fischer
  • 327,261
  • Note that the hyperlink and hypertarget approach goes one line below the actual target position, as you can see by adding some text below with \lipsum[1-5]. This problem is also mentioned in these two questions: 1, 2. – dexteritas Mar 27 '23 at 15:54
12

You can use \hyperlink and hypertarget from hyperref.

\documentclass{article}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}
  \lipsum[1]
  \lipsum[2]
  \hypertarget{mylink}{THE DESTINATION LINE (anchor x)}
  \lipsum[3]
  \lipsum[4]
  \hyperlink{mylink}{CLICK HERE (go to anchor x)}
  \lipsum[5]
\end{document}