Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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}
share|improve this answer
    
I updated my question with a failing example with the ruled option, which is what I currently use. –  OlivierBlanvillain Nov 27 '13 at 9:38
    
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}. –  Andrew Swann Nov 27 '13 at 10:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.