I need to create hyperlink
that points to the top of the page on which given hypertarget
is located. By default, hyperlink
referencing hypertarget
goes to the line of text where referenced hypertarget
is located. However, instead I want it to go to the top of the page where this hypertarget is located, in similar way page \hyperlink{page.3}{page 3}
does.
Here is MWE:
\documentclass{book}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}
Following link will go to top of \hyperlink{page.3}{page 3}.
I want \hyperlink{mytarget1}{this link} to also go to top of page 3, because ``mytarget1'' is located on this page.
And \hyperlink{mytarget2}{this one} to top of page 4, because ``mytarget2'' is located on this page.
\lipsum[1-15]
\phantomsection\hypertarget{mytarget1}{mytarget1 is on this page}
\lipsum[5]
\phantomsection\hypertarget{mytarget2}{mytarget2 is on this page}
\end{document}
I found an answer to similar question, but question and answer deal with using \label
as "target" destination.
This question is not a duplicate, because I want to achieve this with hypertargets, without need to use labels at all.
Is it possible?
Best Answer
I combined and
\hypertarget
and\label
into one macro, using the same label name for the PDF link and the aux file\newlabel
. One can use\hyperlink
,\ref
(which returns the\hypertarget
text),\pageref
and a new macro\pagelink
(which links to the page anchor).