1

Considering this MWE, I found that the hyperlink of cross-reference \Cref{algo1} always navigates to the algorithm bottom where caption is printed when not loading ruled option.

My question is how to force cross-reference hyperlink navigate to the top of algorithm?

\RequirePackage{luatex85}
\documentclass{article}

\usepackage{varioref}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{lipsum}
\usepackage[ruled]{algorithm2e}
\Crefname{algocf}{Algorithm}{Algorithms}

\begin{document}
    \Cref{algo1}
    \lipsum[1-5]
    \begin{algorithm}
        Some\;
        Algorithm\;
        \caption{some caption}\label{algo1}
    \end{algorithm}
\end{document}
1

Barring a persistent warning, the following solution works by inserting a duplicate hyperlink destination at the top of the ruled caption's predisplay:

\documentclass{article}

\usepackage{lipsum}

\usepackage{hyperref,cleveref}
\usepackage[ruled]{algorithm2e}

\Crefname{algocf}{Algorithm}{Algorithms}

\makeatletter
\let\old@algocf@pre@ruled\@algocf@pre@ruled
\renewcommand{\@algocf@pre@ruled}{%
  \Hy@raisedlink{\hyper@anchorstart{algocf.\thealgocf}\hyper@anchorend}%
  \old@algocf@pre@ruled}
\makeatother

\begin{document}

\Cref{algo1}
\lipsum[1-5]

\begin{algorithm}
  Some\;
  Algorithm\;
  \caption{some caption\label{algo1}}
\end{algorithm}

\end{document}
5
  • Many thanks for your answer, but I found that the counter of cross-reference has a number of 1, while the algorithm environment prints a counter of value 2. So, how can I reset the environment counter to be 1? I tried your previous solution, but it didn't help. – Diaa Dec 6 '16 at 18:31
  • @DiaaAbidou: I see. I'll work on this later today. – Werner Dec 6 '16 at 18:32
  • I appreciate your help. – Diaa Dec 6 '16 at 18:33
  • I found another issue; loading hypcap spoiled the navigation of figures cross-references to their tops as previously done by loading caption package. So, how can I fix this also for figure environment? – Diaa Dec 7 '16 at 1:59
  • @DiaaAbidou: The best I can come up with yields a persistent warning about a duplicate destination identifier. It ensures a correct hyperlink jump to the top of the algorithm, and you can ignore the warning. It also doesn't need hypcap anymore. – Werner Dec 7 '16 at 5:17

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.