3

I was testing some links within my produced PDF file and recognized that all of the links to an algorithm (from the algorithm2e package) will jump to the beginning of the algorithm with skipping half of the algorithm's header and the caption of the algorithm. I use the ruled option of the algorithm2e package such that the caption will be above the actual algorithm.

Here is a minimal example:

\documentclass{scrbook}

\usepackage[colorlinks=true,linkcolor=blue]{hyperref}

\usepackage[ruled]{algorithm2e}

\begin{document}
   Der Algorithmus \ref{alg:roundoff} approximiert das ''Closest Vector Problem``. 
   \newpage
       \begin{algorithm}
        \caption{\label{alg:roundoff}Babais \textsf{ROUNDING OFF-PROCEDURE}}

        \emph{Berechne $r_1, r_2, \ldots, r_n \in R$, sodass $\vec{q} = r_1\vec{b}_1+r_2\vec{b}_2+\cdots+r_n\vec{b}_n$ gilt.}\\
        $\vec{v} := \vec{0}$\\
        \For{$i = 1,2,\ldots,n$}
        {
            $\vec{v} := \vec{v} + \lfloor r_i\rceil \cdot \vec{b}_i$
        }
     \end{algorithm}
\end{document}

This code will produce a (blue) link on the first page to the algorithm on the second page. But when zoomed and after clicking the link, it will jump to the following part of the algorithm:

Wrong jump target on ruled algorithm

Question: Is it possible to adjust the jump position for algorithms (from package algorithm2e) such that a click will jump to the position before the algorithm header/caption even if the ruled option of the algorithm2e package is used?

2

1 Answer 1

2

Since you're using the ruled style, the caption is always placed on the top. As such, we can patch the start of the algorithm environment and insert the aid provided by Equivalent to hypcap for [ruled]{algorithm2e}?.

The following patch is provided by xpatch, since the definition of algorithm takes an optional argument:

\documentclass{scrbook}% http://ctan.org/pkg/koma-script

\usepackage[ruled]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}% http://ctan.org/pkg/hyperref
\usepackage{hypcap,xpatch}% http://ctan.org/pkg/{hypcap,xpatch}
\makeatletter
% \xpatch{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
\xpatchcmd{\algorithm}{\ignorespaces}{\ignorespaces%
  \smash{\raisebox{1.5\baselineskip}{\capstart}}}{}{}
% Insert \capstart immediately after algorithm environment starts
\makeatother
\begin{document}
Der Algorithmus \ref{alg:roundoff} approximiert das ''Closest Vector Problem``. 
\newpage
\begin{algorithm}
  \caption{\label{alg:roundoff}Babais \textsf{ROUNDING OFF-PROCEDURE}}
  \emph{Berechne $r_1, r_2, \ldots, r_n \in R$, sodass $\vec{q} = r_1\vec{b}_1+r_2\vec{b}_2+\cdots+r_n\vec{b}_n$ gilt.}\\
  $\vec{v} := \vec{0}$\\
  \For{$i = 1,2,\ldots,n$}
  {
    $\vec{v} := \vec{v} + \lfloor r_i\rceil \cdot \vec{b}_i$
  }
\end{algorithm}
\end{document}

All cases should be covered where the algorithm name does not span multiple lines. One drawback is the placement of dual hypertargets, leading to "destination with same identifier" hyperref/pdfTeX warning.

2
  • This is only working for pdfLatex. Is it normal that this won't work with latex + latex + dvips + ps2pdf? Commented Dec 8, 2013 at 9:35
  • I now just changed to pdflatex which works with this fix. Commented Dec 8, 2013 at 15:02

You must log in to answer this question.

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