7

I use the great package zref to create my own referencing system. Can I make hyperlinks for pdftex (and tex4ht)?

See the following code. I'd like to make the \showref command clickable, so that I jump to the place where I use the \setdest command. Is this (easily) possible?

\documentclass{article}
\usepackage{zref}
\makeatletter
\zref@newlist{zoo}
\zref@newprop{animal}{\currentanimal}
\zref@addprop{zoo}{animal}
\newcommand\setdest[2]{\def\currentanimal{#2}\zref@labelbylist{#1}{zoo}}
\newcommand\showref[1]{\zref@extract{#1}{animal}}
\makeatother

\begin{document}
\section{My favorite pets}
\setdest{label}{Cat}

See the animal \showref{label}.
\end{document}

2 Answers 2

8

According to the zref manual, hyperref is left as a future work.

However, you could at least use the normal \phantomsection trick, and another label:

\documentclass{article}
\usepackage{zref}
\makeatletter
\zref@newlist{zoo}
\zref@newprop{animal}{\currentanimal}
\zref@addprop{zoo}{animal}
\newcommand\setdest[2]{\def\currentanimal{#2}\zref@labelbylist{#1}{zoo}\phantomsection\label{zref:#1}}
\newcommand\showref[1]{\hyperref[zref:#1]{\zref@extract{#1}{animal}}}
\makeatother

\usepackage{lipsum}

\usepackage{hyperref}

\begin{document}
\section{My favorite pets}
\lipsum[1]

\setdest{label}{Cat}
Cat goes here.

\lipsum[2]

\section{Other}
See the animal \showref{label}.
\end{document}
1

I know this is a very old question, but it seems to be the most explicit one on the "status" of zref's support for hyperref (even if not the only one), and the accepted answer makes the claim that "it is left as future work", which is not quite correct. It is true that there is no user documentation for zref-hyperref and that it's implementation leaves "UNFINISHED :-(" as a caveat. But the module is functional, and loading it does provide the anchor property (and includes it to the main property list) which can be used to construct an hyperlink to the reference with standard hyperref user macros. So, even if things are not quite polished and documented, they work:

\documentclass{article}

\usepackage{zref}
\usepackage{zref-hyperref}
\usepackage{hyperref}

\makeatletter
\zref@newlist{zoo}
\zref@newprop{animal}{\currentanimal}
\zref@addprop{zoo}{animal}
\zref@addprop{zoo}{anchor}
\newcommand\setdest[2]{\def\currentanimal{#2}\zref@labelbylist{#1}{zoo}}
\newcommand\showref[1]{%
  \hyperlink{\zref@extractdefault{#1}{anchor}{}}{\zref@extract{#1}{animal}}}
\makeatother

\begin{document}
\section{My favorite pets}
\setdest{label}{Cat}

See the animal \showref{label}.
\end{document}

Produces:

enter image description here

I haven't tested tex4ht, but I'd presume it should work as long as \hyperlink does (hence, pending on hyperref, not quite on zref).

8
  • I've tested it in TeX4ht and it doesn't fail, it produces the Cat text, but link is wrong. It seems that zref cannot set correct link destination to the section in this case.
    – michal.h21
    Commented Sep 17, 2021 at 18:02
  • Hi @michal.h21! Long time! I hope you are doing well. I think this may be due to the non-standard way the label is being set (in \setdest). Let me try with a more standard section link.
    – gusbrs
    Commented Sep 17, 2021 at 18:18
  • @michal.h21 I've tested here, and the anchor is correct even for \setdest (it points to the section counter though, as expected here). However, make4ht points to #Doc-Start (probably also what you saw there). Does \hyperlink work in general for tex4ht, I mean, regardless of zref?
    – gusbrs
    Commented Sep 17, 2021 at 18:30
  • Hi, yes long time :) I am well, I hope you too. \hyperlink works with \hypertarget with TeX4ht. It seems that \currentHref that hyperref uses is not set correctly with TeX4ht for sections.
    – michal.h21
    Commented Sep 17, 2021 at 18:41
  • Mhm, I don't know why that would make a difference, but I'm never surprised by tex4ht needing special handling for things. The anchor is stored by hyperref for standard labels as well, it is just usually not the most common (user facing) way to refer to it. Would you like a sample document to test?
    – gusbrs
    Commented Sep 17, 2021 at 18:47

You must log in to answer this question.

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