5

We try to reference a line in a shy algorithm and the algorithm itself with zref-clever. Running pdflatex twice on

\documentclass{article}
\pagestyle{empty}
\usepackage{hyperref}
\usepackage{algorithm2e}
\usepackage{zref-clever}
\zcRefTypeSetup{algocf}{
  name-sg=Alg.\@,
  Name-sg=Alg.\@
}
\zcRefTypeSetup{AlgoLine}{
  Name-sg=line,
  name-sg=line
}
\zcRefTypeSetup{algocfline}{
  Name-sg=line,
  name-sg=line
}
\zcRefTypeSetup{line}{
  Name-sg=line,
  name-sg=line
}
\begin{document}
  \begin{algorithm}\LinesNumbered
    \caption{My great algorithm}\label{algLabel}
    \KwOut{The oh so great output of my great algorithm}
    command1\;
    command2\nllabel{lineLabel}\;
    command3
  \end{algorithm}
  \zcref[S]{algLabel} contains \zcref{lineLabel}.
\end{document}

yields

output of pdflatex

As we see, the second type name is “Alg.”, whereas we'd expect “line”. If we comment out hyperref, we get “Alg. 1 contains line 2.” as expected. So far we failed to get different type names for the two references in the presence of hyperref, so you can blame it. At the same time, given that algorithm2e has been unmaintained for about 8½ years and zref-clever for slightly over 1 year, you can blame them, too. Anyway, what is the technical root cause of the failure? Any bugfix, patch, or workaround?

9
  • Would (a customized version of) \autoref{algLabel} contains \autoref{lineLabel} be an option? Commented Jan 22 at 19:31
  • 1
    @Marijn Are you suggesting that we switch from \zref from zref-clever to \autoref from thmtools? I'm afraid switching to yet another package for my non-minimal document (not shown here) is out of option as of now, as it'd take me at least a full day, given the current state of the bug tracker of thmtools and given that its last commits happened already 2 years ago. I'm in the process of ditching an even older cleveref … Commented Jan 22 at 19:35
  • \autoref is from hyperref, not from thmtools (see page 26 of the hyperref manual). It just takes the value of the string from thmtools or babel or whichever other package (re)defines \algorithmname. You can still use zref-clever for everything else. Not ideal but a quick solution - I'll post an answer. Commented Jan 22 at 19:42
  • @Marjin Oh. I search for autoref on CTAN and it directed me to thmtools. Sure, please feel free to post an answer. Maybe I was wrong about the source of the provided command. Commented Jan 22 at 19:44
  • 1
    You're right, I didn't look closely at the other question and assumed it was about zref-clever as well. I retracted the close vote. Commented Jan 23 at 6:47

2 Answers 2

6

from https://tex.stackexchange.com/a/730188/2388

\documentclass{article}
\pagestyle{empty}
\usepackage{hyperref}
\usepackage{algorithm2e}
\usepackage{zref-clever}
\zcRefTypeSetup{algocf}{
  name-sg=Alg.\@,
  Name-sg=Alg.\@
}
\zcRefTypeSetup{AlgoLine}{
  Name-sg=line,
  name-sg=line
}
\makeatletter
\renewcommand{\nllabel}[1]
 {{\let\@currentlabel\algocf@currentlabel
  \let\@currentcounter\algocf@currentcounter
  \label{#1}}}%
\renewcommand{\algocf@nl@sethref}[1]{%
  \renewcommand{\theHAlgoLine}{\thealgocfproc.#1}%
  \hyper@refstepcounter{AlgoLine}%
  \gdef\algocf@currentlabel{#1}%
  \gdef\algocf@currentcounter{AlgoLine}%
 }%
\makeatother
\begin{document}
  \begin{algorithm}\LinesNumbered
    \caption{My great algorithm}\label{algLabel}
    \KwOut{The oh so great output of my great algorithm}
    command1\;
    command2\nllabel{lineLabel}\;
    command3
  \end{algorithm}
  \zcref[S]{algLabel} contains \zcref{lineLabel}.
\end{document}
1
  • 1
    Thx! I removed the now useless parts from your answer. Commented Jan 22 at 20:35
3

Quick workaround is to use a customized version of \autoref from hyperref in cases where \zcref et al. doesn't work (and use the zref-clever commands everywhere else if you are happy with them).

\documentclass{article}
\pagestyle{empty}
\usepackage{hyperref}
\usepackage{algorithm2e}
\usepackage{zref-clever}
\zcRefTypeSetup{algocf}{
  name-sg=Alg.\@,
  Name-sg=Alg.\@
}
\zcRefTypeSetup{AlgoLine}{
  Name-sg=line,
  name-sg=line
}
\zcRefTypeSetup{algocfline}{
  Name-sg=line,
  name-sg=line
}
\zcRefTypeSetup{line}{
  Name-sg=line,
  name-sg=line
}

\def\algorithmautorefname{Alg.}% the default is "algorithm" 

\begin{document}
  \begin{algorithm}\LinesNumbered
    \caption{My great algorithm}\label{algLabel}
    \KwOut{The oh so great output of my great algorithm}
    command1\;
    command2\nllabel{lineLabel}\;
    command3
  \end{algorithm}
  \autoref{algLabel} contains \autoref{lineLabel}.
\end{document}

Or even simpler without needing the \algorithmautorefname definition:

\zcref[S]{algLabel} contains \autoref{lineLabel}.

correct automatic entity names in references

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.