Discussion:
Hyperref link to label
(too old to reply)
Peter Flynn
2020-06-19 15:53:03 UTC
Permalink
I'm clearly doing something wrong here.
The link on p.1 points to p.1 and not to p.2 where the label is.

\documentclass{article}
\usepackage{hyperref}
\begin{document}
This is about the \hyperref[foo]{Foo}
\clearpage
This is where the Foo lives\label{foo}
\end{document}

Peter
Ulrike Fischer
2020-06-19 16:14:45 UTC
Permalink
Post by Peter Flynn
I'm clearly doing something wrong here.
The link on p.1 points to p.1 and not to p.2 where the label is.
\documentclass{article}
\usepackage{hyperref}
\begin{document}
This is about the \hyperref[foo]{Foo}
\clearpage
This is where the Foo lives\label{foo}
\end{document}
You need an anchor. If no counter sets it, you have to set it
manually:

\phantomsection\label{foo}
--
Ulrike Fischer
http://www.troubleshooting-tex.de/
Peter Flynn
2020-06-19 20:15:09 UTC
Permalink
Post by Ulrike Fischer
Post by Peter Flynn
I'm clearly doing something wrong here.
The link on p.1 points to p.1 and not to p.2 where the label is.
\documentclass{article}
\usepackage{hyperref}
\begin{document}
This is about the \hyperref[foo]{Foo}
\clearpage
This is where the Foo lives\label{foo}
\end{document}
You need an anchor. If no counter sets it, you have to set it
\phantomsection\label{foo}
Oooh. In all these years I never found that one. Thank you. And Ulrich.
(Yes, on reflection, all previous links have been in big documents with
lots of structural labels.)

P

u***@web.de
2020-06-19 16:33:41 UTC
Permalink
Post by Peter Flynn
I'm clearly doing something wrong here.
The link on p.1 points to p.1 and not to p.2 where the label is.
\documentclass{article}
\usepackage{hyperref}
\begin{document}
This is about the \hyperref[foo]{Foo}
\clearpage
This is where the Foo lives\label{foo}
\end{document}
Hyperlinks need targets/named destinations/anchors.

Usually a sectioning-command like \section or \subsection
beneath other things expands to a call to
\refstepcounter{<counter>}.

\refstepcounter{<counter>} in turn increments the <counter>
in question and redefines the macro \@currentlabel to deliver
the expansion of the sequence \p@<counter>\the<counter>.

The label-command in turn uses the expansion of \@currentlabel
when it comes to writing referencing-data (these \newlabel-
entries) to the .aux-files.

When the hyperref-package is loaded, \refstepcounter also
places a target/named destination/anchor and stores the name
of that named destination in the macro \@currentHref.

The label-command in turn also uses the expansion of \@currentHref
when it comes to writing referencing-data (these \newlabel-
entries) to the .aux-files. This way referencing-commands can
also retrieve information about the name of target/named destination/
anchor placed into the .pdf-file at the time when the counter
which is to be referenced was incremented.

In your example no sectioning-commands and thus no instances
of \refstepcounter were applied, thus there are no targets/
named destinations/anchors which could be used for hyperlinking.

In such situations you usually get a message in the .log-file and
as default the target will be the first page of the document.

You can apply \phantomsection for placing a target/named destination/
anchor and having \@currentHref redefined to yield the name of that
target/named destination/anchor:


\documentclass{article}
\usepackage{hyperref}
\begin{document}
This is about the \hyperref[foo]{Foo}
\clearpage
This is where the Foo lives\phantomsection\label{foo}
\end{document}


View the .pdf-file at a very large zoom-factor to see where the
target/named destination/anchor will actually be placed and thus
which "point" of the document/of the second page will be navigated
to when clicking the hyperlink.


In case you are interested:

I tried to explain the details of LaTeX 2e's cross-referencing-
mechanism at TeX-LaTeX StackExchange:
<https://tex.stackexchange.com/a/442118/118714>


Sincerely

Ulrich
Loading...