5

I can't reproduce this in miniature outside of my document, so I'm asking more debugging help on how I can fix it within the document I have.

The problem is, I have a section with something like:

First is \hyperref[P1]{Item I}, go there.  \hyperref[P2]{Next} is item 2, and finally is \hyperref[P3]{Item III}.
\newpage

%% later...
\section*{Item I}
\label{P1}
stuff
\section*{Item II}
\label{P2}
stuff
\section*{Item III}
\label{P3}
stuff

Suppose

  • Item I, P1 is on page 1.
  • Item II, P2 is on page 6.
  • Item III, P3 is on page 20.

No matter what I change, \hyperref[P3]{Item III} points to page 5, instead of 20.

I have tried the following debugs:

  1. Did I actually mislabel P3 as P2? No, it's P3.
  2. Did I actually fail to update the original hyperref, so it still says P2? No, it says P3.
  3. Am I sure? How about I call it something totally different, like LOCATION. \hyperref[LOCATION]{Item III} \section*{Item III}\label{LOCATION} Still points to page 5, not page 20!
  4. Is there an error I'm missing, such as: LaTeX Warning: There were undefined references.? Nope, there are no missing references.
  5. How about completely remove \label{P2} and see if P3 is still pointing there? Removed, yes, it still points to page 5, which now has zero labels intentionally pointing to it.
  6. What about debugging the pages themselves? \pageref{P1} \pageref{P2} \pageref{P3} prints 2 7 21 Close enough, but this tells me in fact pageref{P2} is also incorrect. It's not pointing at P2, but at the end of P1, just as P3 is pointing at the end of P1.

Beyond this, I have no idea how to make any refs defined after the first one work, and by that I mean point elsewhere than at page 5. What could be going on?

7

You need to use

\phantomsection
\section*{<title>}
\label{<label>}

since there is no hyper target set with \section* like there is with \section. If you're only using \section* within your document, you could automate this process using

\let\oldsection\section % Copy \section into \oldsection
\renewcommand{\section}{%
  \phantomsection % Set hyper target
  \oldsection*
}

in your preamble

  • This is so reasonable I wish it were the default. The macro helps a lot, thank you! – Mittenchops Nov 6 '18 at 18:19
  • 3
    @Mittenchops: The default for \section is to issue \refstepcounter. This sets a reference point and also steps the counter (section in this case). However, with \section* there is no counter to step. Moreover, if you use \label-\ref, the result is the previous counter that was stepped. So, it's just that there's nothing appropriate to reference in general. The onus is therefore shifted to the end-user. – Werner Nov 6 '18 at 18:22
3

Another approachs with hyperref and nameref that do not need \phantomsections:

mwe

\documentclass{article}
\usepackage[colorlinks,linkcolor=blue]{hyperref}
\usepackage{lipsum}
\usepackage{nameref}
\begin{document}

First is \Nameref{P1}, go there. \par 
\hyperlink{X}{Here} is \nameref{P2} (\Nameref{P2}).\par
But there are a  \Nameref{P3}.\par 
And there are not three without \hyperlink{Y}{four} in page \pageref{P4}

\newpage

\section*{Item I}
\label{P1}
\lipsum[1-20]
\section*{Item II}
\label{P2}\hypertarget{X}
\lipsum[21-40]
\section*{Item III}
\label{P3}
\lipsum[41-50]
\section*{Item IV}
\label{P4}
\hypertarget{Y} 
\lipsum[1]

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.