Hyperlink to top of the page with given hypertarget

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}

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]

\lipsum[5]

\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?

• Maybe, to improve the question for other users looking for the same solution, you could explain why you want to use hypertarget instead of labels? – Runar Dec 30 '15 at 11:50
• @runartrollet I think that many people might actually be looking for how to solve this with hypertargets, because "default" way to point to another point in document is by using hyperlink-hypertarget pair. For me, personally, one reason to get solution with hypertarget is because I have an existing document which uses hypertargets already, so I wouldn't want to switch them to labels now. Besides, I think it is good idea to have 2 alternatives to choose from. (And I think that solution with labels might have some side-effects, but I haven't looked into analyzing those side-effects yet.) – Rafal Dec 30 '15 at 12:02
• The hypertarget is a name used by the PDF and refers to a specific location. Presumably the page number is encoded somewhere, but it is not obvious. The top of the page is assigned its own location under the names page.1 page.2 etc. – John Kormylo Dec 30 '15 at 17:21
• @JohnKormylo Since both label and hypertarget refer to specific location in PDF in similar way, as far as I know, I was hoping there was some way to retrieve number of the page on which hypertarget appears, in similar fashion to how it is done with labels in linked answer (which was by use of \getpagerefnumber{labelname}). – Rafal Dec 31 '15 at 4:20
• No, \label saves the text shown by \ref plus {\thepage} {\@currentlabelname} and {\@currentHref} where \@currentHref is a \hypertarget name created by \refstepcounter. – John Kormylo Dec 31 '15 at 16:58

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

\documentclass{book}
\usepackage{lipsum}
\usepackage{hyperref}

\makeatletter
\newcommand{\pagetarget}[2]% #1=label (both hypertarget and label), #2=text
{\hypertarget{#1}{#2}\protected@write\@auxout{}{%
\string\newlabel{#1}{{#2}{\thepage}{page.\thepage}{#1}{}}}}
\makeatother
% \ref{#1} will return #2 and link to #2.
% \pageref will return the page and link to #2.
% \getrefbykeydefault{#1}{name}{Doc-Start} returns the page anchor.

\begin{document}

I want \pagelink{mytarget1}{this link} to also go to top of page 3, because mytarget1'' is located on this page.

And \pagelink{mytarget2}{this one} to top of page 4, because mytarget2'' is located on this page.

\lipsum[1-15]