5

To show the page number of a Figure or a Table, \pageref is useful. However, it does not work for a \hypertarget.

What if I need to show the page number of a hyper-target?

For example:

The problem is explained in page ???? at \hyperlink{my-explanation}{here}

.
.
.

\hypertarget{my-explanation}
Explanation ....

Attempt:

\getpagerefnumber{my-explanation} does not work and it returns zero.

1
  • 3
    You've asked several questions by now. Where is the MWE?
    – Troy
    Jun 26, 2017 at 6:46

2 Answers 2

6

Similar to touhami's answer, but using the phantomsection and \label to get correct anchors, all combining into a \pagetarget macro:

\documentclass{article}
\usepackage{blindtext}
\usepackage{hyperref}

\newcommand{\pagetarget}[2]{%
  \phantomsection%
  \label{#1}%
  \hypertarget{#1}{#2}%
}


\begin{document}

The problem is explained in page \pageref{my-explanation} at \hyperlink{my-explanation}{here}

\blinddocument
\blindtext[2]
\pagetarget{my-explanation}{Explanation}

\blindtext[5]

\end{document}
2
  • Not even close. First, i used mwe ..., ... ;-)
    – touhami
    Jun 26, 2017 at 11:30
  • @touhami: Which I never use ;-)
    – user31729
    Jun 26, 2017 at 11:30
2

You can use \label-\pageref.

Example

\documentclass{article}
\usepackage{hyperref}

\usepackage{mwe}

\begin{document}

The problem is explained in page \pageref{my-explanation} at \hyperlink{my-explanation}{here}

\blinddocument

...
\hypertarget{my-explanation}{\label{my-explanation}Explanation} ....

\end{document}
5
  • Do hypertarget and label IDs collide?
    – ar2015
    Jun 26, 2017 at 7:21
  • If i understand, no, there is nothing between them.
    – touhami
    Jun 26, 2017 at 7:39
  • I'm not sure that's safe. \label points in that case to the last counter increased by \refstepcounter, which is subsection 2.3.1 in your document. Try adding \lipsum between \blinddocument and \hypertarget: the \hyperkink and the page number will work, but clicking onto the page number will take you to the section, not to the point where you called \label. \phantomsection should be the way to go, I think.
    – campa
    Jun 26, 2017 at 8:03
  • @campa you're right. For me the question was to show the page number \hyperkink is there if you want go to the page. And again, your right use of \phantomsection is safer.
    – touhami
    Jun 26, 2017 at 8:34
  • @touhami: You could use another counter that is increased with \refstepcounter and then \label, but effectively, this is the same as \phantomsection
    – user31729
    Jun 26, 2017 at 11:31

You must log in to answer this question.

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