2

I just discovered the hypcap packages which improves the \ref link to point before the figures and tables instead of to captions. How can I do the same with my algorithms written with algorithm2e using the ruled option?

Here is a minimal example where we cannot read the caption after clicking on the ref:

\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{hyperref}
\usepackage{hypcap}
\begin{document}

\begin{algorithm}[H]
  \capstart
  \caption{How to write algorithms}
  \label{alg:sample}
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
\end{algorithm}

Text with a ref to algorithm \ref{alg:sample}.

\end{document}

1 Answer 1

3

This works in the same way as for other floats: put \capstart at the beginning of the float.

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{hyperref}
\usepackage{hypcap}

\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{algorithm}[H]
  \capstart
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }

  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
      }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
  \label{alg:sample}
\end{algorithm}

Text.

\lipsum[2-5]

Algorithm~\ref{alg:sample}.

\lipsum[3]
\end{document}
2
  • I updated my question with a failing example with the ruled option, which is what I currently use. Commented Nov 27, 2013 at 9:38
  • 1
    That does still work, the link points to the beginning of the algorithm (not its caption). You can adjust the positioning with something like \renewcommand{\hypcapspace}{\baselineskip}. Commented Nov 27, 2013 at 10:32

You must log in to answer this question.

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