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.

As the title suggests, I'm trying to reference to an algorithm created by algorithm2e package. Here is a MWE:

\documentclass[12pt,a4paper]{report}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm \nameref{bla}.
%The pseudo-code is taken from the v4.01 manual    
\begin{algorithm}[H]
    \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{bla}
\end{algorithm}

\end{document}

Which results in the following:

enter image description here

As you can see, hyperlink-ref is generated correctly but obviously there is not text (However I'm able to click on the empty link box which brings me the correct place).

The question is, how to assign a name to an algorithm to be used by nameref?

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Package algorithm2e redefines the internals of \caption inside its environments, therefore \nameref's patching misses this case. The following patches \algocf@latexcaption to get the caption title for \nameref:

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\makeatletter
\let\original@algocf@latexcaption\algocf@latexcaption
\long\def\algocf@latexcaption#1[#2]{%
  \@ifundefined{NR@gettitle}{%
    \def\@currentlabelname{#2}%
  }{%
    \NR@gettitle{#2}%
  }%
  \original@algocf@latexcaption{#1}[{#2}]%
}
\makeatother

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm
\nameref{bla}.

%The pseudo-code is taken from the v4.01 manual
\begin{algorithm}[H]
    \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{bla}
\end{algorithm}
\end{document}

Result

Example with independent title

In the following example macro \algotitle sets an anchor and expects two arguments, the title for \nameref and the name for \label:

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\makeatletter
\newcommand*{\algotitle}[2]{%
  \stepcounter{algocf}%
  \hypertarget{algocf.title.\theHalgocf}{}%
  \NR@gettitle{#1}%
  \label{#2}%
  \addtocounter{algocf}{-1}%
}
\makeatother

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm
\nameref{bla.title}.

%The pseudo-code is taken from the v4.01 manual
\begin{algorithm}[H]
    \algotitle{Bla bla}{bla.title}
    \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{bla}
\end{algorithm}
\end{document} 

Result

share|improve this answer
    
I asked the same question on the other answer. [copy/paste!]: Thank you very much. Although your answer is great I have a little problem with that (no attack! I just need to modify slightly for my purpose). I may use a very long caption for an algorithm and in nameref I just need the title or a single world as representative of the algorithm. Lets say, the name of algorithm is hello_world but the captions is This is how you write a simple program in a language. Obviously in this case I rather to use the name/title of the algorithm instead of the caption. Is there anyway to achieve this? –  Pouya May 10 '13 at 8:07
    
@Pouya: The solution of Heiko can handle the optional argument of caption as well. –  Marco Daniel May 10 '13 at 8:25
add comment

algorithm2e uses its own definition of caption. So you have to redefine it. You have to provide a string for \nameref. This can be achieved by \Nr@gettitle.

\documentclass[12pt,a4paper]{report}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{etoolbox}
\makeatletter
\long\def\algocf@caption@algo#1[#2]#3{%
  \ifthenelse{\equal{\algocf@algocfref}{\relax}}{}{\algocf@captionref}%
  \@ifundefined{hyper@refstepcounter}{\relax}{% if hyper@refstepcounter undefind, no hyperref, else...
    \ifthenelse{\equal{\algocf@algocfref}{\relax}}{\renewcommand{\theHalgocf}{\thealgocf}}{% take algocf as Href
      \renewcommand{\theHalgocf}{\algocf@algocfref}}%else if SetAlgoRefName done, take this name as ref.
    \hyper@refstepcounter{algocf}%set algocf as category of ref
  }%
     \NR@gettitle{#2}% 
  \algocf@latexcaption{#1}[{#2}]{{#3}}% call original caption
}%
\makeatother

\begin{document}
\begin{verbatim}
\ref{fig} + \nameref{fig} + \autoref{fig}

\ref{algo} + \nameref{algo} + \autoref{algo}
\end{verbatim}

\ref{fig} + \nameref{fig} + \autoref{fig}

\ref{algo} + \nameref{algo} + \autoref{algo}

\begin{algorithm}[H]
    \caption{How to write algorithms}
    \label{algo}
\end{algorithm}

\begin{figure}[!ht]
\caption{figure fdoo}\label{fig}
\end{figure}
\end{document}

enter image description here

share|improve this answer
    
Thank you very much. Although your answer is great I have a little problem with that (no attack! I just need to modify slightly for my purpose). I may use a very long caption for an algorithm and in nameref I just need the title or a single world as representative of the algorithm. Lets say, the name of algorithm is hello_world but the captions is This is how you write a simple program in a language. Obviously in this case I rather to use the name/title of the algorithm instead of the caption. Is there anyway to achieve this? –  Pouya May 10 '13 at 8:05
    
@Pouya: Fixed. Now you can work with the optional argument of \caption. That means \caption[foo]{bar} will provide the result "foo" for nameref and \caption{bar} will provide "bar" for nameref. –  Marco Daniel May 10 '13 at 8:23
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.