0

I need to build page that is sort-of like an index ... but it isn't exactly an index.

I pretty much accomplished what I want using \label{some unique ID} to capture the page number ... and \pageref{some unique ID} to print that page number.

However, in the following MWE, \pageref is indeed printing the correct page number ... but it's linking to an incorrect page (It's linking to the first page of the chapter that contains the page of some unique ID).

I'd like to either (a) turn it's linking off (since my hyperlink is using the correct hypertarget anyway, ... or (2) fix it to link to the correct page.

Thanks for your help with this one !!!!

%&pdf laTeX
% !TEX encoding = UT F-8 Unicode
\documentclass[letter, 12pt]{book}

\usepackage{hyperref}

\newcommand{\mylink}[2]{ \hypertarget{#1}{#2} \label{#1}}

\begin{document}

\chapter*{Chapter One}  
   \mylink{key for  link 1}{This} text on page one.

\newpage
   \mylink{key for  link 2}{This} text on page two. 

\newpage
   \mylink{key for  link 3}{This} text on page three.


\newpage            
    \noindent \hyperlink{key for  link 1} {Item 1} ............ pg \pageref{key for  link 1}\\  
    \noindent \hyperlink{key for  link 2} {Item 2} ............ pg \pageref{key for  link 2}\\  
    \noindent \hyperlink{key for  link 3} {Item 3} ............ pg \pageref{key for  link 3}\\


\end{document}

RichWalt
  • 171

1 Answers1

3

\hypertarget doesn't produce a target that can be referenced by \label.

\documentclass[letter, 12pt]{book}

\usepackage{hyperref}

\newcommand{\mylink}[2]{ \phantomsection#2\label{#1}}

\begin{document}

\chapter*{Chapter One}
   \mylink{key for  link 1}{This} text on page one.

\newpage
   \mylink{key for  link 2}{This} text on page two.

\newpage
   \mylink{key for  link 3}{This} text on page three.


\newpage
    \noindent \hyperref[key for  link 1] {Item 1} ............ pg \pageref{key for  link 1}\\
    \noindent \hyperref[key for  link 2] {Item 2} ............ pg \pageref{key for  link 2}\\
    \noindent \hyperref[key for  link 3] {Item 3} ............ pg \pageref{key for  link 3}\\


\end{document}

Ulrike Fischer
  • 327,261
  • Thanks for your answer ... works GREAT !!!! But I do not really understand your comment at the top. my \hypertarget was being followed correctly by \hyperlink. But obviously it was interfering with \pageref. Apparently, the uniqueID can not be shared by both \hypertarget and \label ... in order for \pageref to work. BTW ... I thought I was only using \label and \pageref to get the page number ... I was surprised to see that it was itself a link. – RichWalt May 02 '22 at 16:07
  • if you don't want a link use \pageref*{key}. – Ulrike Fischer May 02 '22 at 16:21
  • No need for that ... I'll keep both links with your solution !!!! (I just didn't know I was going to get linking ability along the way ) With your solution, both the description part, as well as the page number, link correctly ! BTW ... thanks for such a FAST reply. This community is great ! – RichWalt May 02 '22 at 16:31
  • Ulrike is one of those who make this community great. Over the years I learned a lot about TeX/LaTeX from her. – Ulrich Diez May 03 '22 at 17:59