# Equivalent to hypcap for [ruled]{algorithm2e}?

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}

-

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}{
\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}

-
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