14

hyperref provides the command \hyperlink, with syntax:

\hyperlink{name}{text}

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,

Click \hyperlink{page.23}{here} to jump to page 23.

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
\hyperlink{page.\getpageref{lkd}}{this page}
for further clarification.

\newpage

This is the linked page.\label{lkd}

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

3
  • Regarding your question: Take a look at cleveref, which, when used, should BTW loaded after hyperref.
    – Speravir
    Commented May 13, 2012 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
    Commented May 13, 2012 at 2:31
  • The option pageanchor in hyperref is true by default, so I left it out in my answer.
    – Speravir
    Commented May 13, 2012 at 15:10

2 Answers 2

17

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
\hyperlink{page.\getpagerefnumber{mypage}}{this page}
for further clarification.
\clearpage

\dots
\clearpage

This is the linked page.\label{mypage}

\end{document}
4
  • But \hyperlink needs a \hypertarget, or am I wrong? See my answer.
    – Speravir
    Commented May 13, 2012 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
    Commented May 13, 2012 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
    Commented May 13, 2012 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
    Commented May 13, 2012 at 20:03
3

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[%
  colorlinks=true,%
  linkcolor=green,%
%  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

%%% \mypageref{<label>}{some linked text}
\newcommand{\mypageref}[2]{\hyperref{./\jobname.pdf}{page}{\getpagerefnumber{#1}}{#2}}

\begin{document}

\section{Package \texttt{blindtext}}

\subsection{English blindtext}\label{btext-en}
See also package \hyperref[klipsum]{\ttfamily kantlipsum} on
\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}
See also package \hyperref[btext-en]{\texttt{blindtext} with English text} on
\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.

1
  • The cyan-colored "page 2", "page 1", "next page", and "previous page" do not act as links! (And why do they get colored if not?)
    – murray
    Commented Jul 3, 2020 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .