# Using \hyperlink to link text to page with an implicit page anchor not known in advance

hyperref provides the command \hyperlink, with syntax:

where name is the name of a hypertext object, and text is used as the link to jump to the defined mark. Page anchors, e.g., page.12, can also be used as hypertext objects. For example,

This was proposed by Martin Scharrer as a solution to this problem.

I would like to link a string of text to a page (by page number) using \hyperlink, but being able to refer to an implicit page anchor page.<number> whose ''number'' is not known in advance. That is, use a value of the counter page, which is only set later in the document, in the argument of \hyperlink.

Here's a hypothetical solution:

\documentclass{article}

\usepackage[pageanchor]{hyperref} % option is the default anyway

%% Martin Scharrer's patch for hyperref
%% available at https://tex.stackexchange.com/a/19416/9237
\makeatletter
\let\orig@Hy@EveryPageAnchor\Hy@EveryPageAnchor
\def\Hy@EveryPageAnchor{%
\begingroup
\hypersetup{pdfview=Fit}%
\orig@Hy@EveryPageAnchor
\endgroup
}
\makeatother

\def\getpageref{???}   % \getpageref extracts the numeric value of \pageref{key}
% for use in the first argument of \hyperlink

\begin{document}

See
for further clarification.

\newpage

\end{document}

I know how to achieve a similar solution by simply using \label and then \pageref (or \autopageref), but I am interested in making use of a hyperref patch proposed here by Martin to make all page anchors be displayed in full page mode. (This patch only seems to work with \hyperlink.)

• Regarding your question: Take a look at cleveref, which, when used, should BTW loaded after hyperref. – Speravir May 13 '12 at 0:50
• Steffen, thanks for the suggestion, I'll start using it from now on. However, it is my understanding that none of the commands provided by cleveref are part of the solution to my question. – nnunes May 13 '12 at 2:31
• The option pageanchor in hyperref is true by default, so I left it out in my answer. – Speravir May 13 '12 at 15:10

You can label the page you want to link to using the normal \label command, and extract its page number with \getpagerefnumber from the refcount package to use it in the \hyperlink (the "normal" LaTeX macro \pageref doesn't work here, as it is not expandable).

### A small example:

\documentclass{article}

\usepackage{refcount}
\usepackage[pageanchor]{hyperref}

%% Martin's patch for hyperref
\makeatletter
\let\orig@Hy@EveryPageAnchor\Hy@EveryPageAnchor
\def\Hy@EveryPageAnchor{%
\begingroup
\hypersetup{pdfview=Fit}%
\orig@Hy@EveryPageAnchor
\endgroup
}
\makeatother

\begin{document}

See
for further clarification.
\clearpage

\dots
\clearpage

\end{document}
• But \hyperlink needs a \hypertarget, or am I wrong? See my answer. – Speravir May 13 '12 at 14:57
• @Speravir Generally it does, however the pageanchor option of hyperref (which is active by default) adds the necessary anchors automatically, so my solution works without explicitly creating these. See the hyperref manual for more information. – diabonas May 13 '12 at 15:14
• @Speravir Look for the description of the pageanchor option. Even more information can be found in the commented hyperref sources, especially p. 179f, where you can see that if pageanchor is activated, \hyper@@anchor{page.\@the@H@page}\relax is executed, which is the same as \hypertarget{page.<page number>}{}. – diabonas May 13 '12 at 19:33
• I still don't get it, it's far from what I'm able to understand, now. Nonetheless a suggestion: Add some explanation into your answer and we delete our comments. – Speravir May 13 '12 at 20:03

I tried last night some things, but got only errors, as I played with \pageref*. But diabonas’ answer gave me the decisive hint. So here’s my solution without \hyperlink. It uses the alternative definition \hyperref{URL}{category}{label}{text} instead with category = “page”. It’s especially useful for external resources (PDF in external URL needs of course page anchors, and it works, as far as I know, only with the browser plugin version of Adobe Reader). In my MWE the URL is set to ./\jobname.pdf, what will be converted into the local PDF file. It works exactly as wanted by the OP.

\documentclass{article}
\usepackage[latin,english]{babel} % needed for "blindtext",
% "english" is the active language
\usepackage{blindtext,lipsum,kantlipsum} % just for the examples

\usepackage{refcount}% from "oberdiek" bundle

\usepackage[%
%  filecolor=cyan,% it's the default
]{hyperref}

%%% Patch from Martin Scharrer
%%% cf. https://tex.stackexchange.com/a/19416/9237
\makeatletter
\let\orig@Hy@EveryPageAnchor\Hy@EveryPageAnchor
\def\Hy@EveryPageAnchor{%
\begingroup
\hypersetup{pdfview=Fit}%
\orig@Hy@EveryPageAnchor
\endgroup
}
\makeatother

\newcommand{\mypageref}[2]{\hyperref{./\jobname.pdf}{page}{\getpagerefnumber{#1}}{#2}}

\begin{document}

\section{Package \texttt{blindtext}}

\subsection{English blindtext}\label{btext-en}
\mypageref{klipsum}{next page}.

\medskip

\blindtext[1]

\subsection{Latin blindtext}\label{btext-la}
Compare package \hyperref[lipsum]{\ttfamily lipsum} on
\mypageref{lipsum}{page \pageref*{lipsum}}.

\medskip

\begin{otherlanguage}{latin}
\blindtext[1]
\end{otherlanguage}

\newpage

\section{Package \texttt{kantlipsum}}\label{klipsum}
\mypageref{btext-en}{previous page}.

\medskip

\kant[1]

\section{Package \texttt{lipsum}}\label{lipsum}
Compare package \hyperref[btext-la]{\texttt{blindtext} with pseudo-Latin text} on
\mypageref{btext-la}{page \pageref*{btext-la}}.

\medskip

\begin{otherlanguage}{latin}
\lipsum[1]
\end{otherlanguage}

\end{document}

MWE happily stolen from myself: Representing hyperlinks differently in a single pdf file.