38

I'm currently working on a document where I was using algorithm+algorithmic along with cleveref. With this everything worked perfectly.

Now I had to replace algorithm+algorithmic with algorithm2e. Ever since, I cannot \cref to the label in the algorithm environment. More explicitly, I can refer but \cref doesn't generate a text in front of the number (which is correct) of the algorithm.

I couldn't find any reference to algorithm's packages in the cleveref documentation. Any ideas?

3
  • In addition to the fix in my answer, I suggest you contact the author of cleveref. He is very responsive to user requests, and will likely add support for algorithm2e if you explain to him what is needed.
    – Lev Bishop
    Commented Jan 28, 2011 at 16:07
  • 3
    For the record, Cleveref versions since 0.17.9 (released in 2011) support algorithm2e "out-of-the-box". Commented Jun 13, 2017 at 17:17
  • 1
    @Dror could you review the accepted answer? I think fixing the load order will be the superior solution in most cases.
    – dreua
    Commented Mar 11, 2021 at 15:29

3 Answers 3

33

You need to tell cleveref how you want to refer to the algorithms. Something like:

\crefname{algocf}{alg.}{algs.}
\Crefname{algocf}{Algorithm}{Algorithms}
2
  • This solved my problem. But I have another question. How did you know to choose for the type algocf? Where is it documented? Thanks a lot in any case!
    – Dror
    Commented Jan 28, 2011 at 7:48
  • 5
    From the log-file, without the above commands: LaTeX Warning: cref reference format for label type algocf' undefined on input line 27.`
    – Lev Bishop
    Commented Jan 28, 2011 at 14:36
40

You only need to load algorithm2e before cleveref.

7
  • 7
    I think this is the most correct answer Commented Jun 28, 2018 at 11:54
  • 2
    This should be the accepted solution, in my opinion. Commented Aug 4, 2020 at 11:48
  • 2
    still, functions don't work. and remaming algorithm into function doesn't work.
    – caveman
    Commented Jan 11, 2021 at 21:00
  • @caveman This seems to properly answer the question for most people. If you want something else I suggest you ask another question.
    – dreua
    Commented Mar 11, 2021 at 15:32
  • 2
    @dreua - cleveref should almost always be loaded last.
    – Mico
    Commented Mar 11, 2021 at 15:49
0

For me, another problem was that I declared the \label before the \caption, resulting in "As shown in line 1, Euclid's ..."

Declaring the \caption before the \label solved the problem for me as follows:

\documentclass{article}
\usepackage{algorithm2e}
\usepackage{cleveref}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}
\label{euclid}
\KwData{Two numbers $a$ and $b$}
\KwResult{$\gcd(a,b)$}
\While{$b\neq0$}{
  \tcp{this is a comment}
  $r\gets a \bmod b$\;
  $a\gets b$\;
  $b\gets r$\;
}
\Return $a$\;
\end{algorithm}

As shown in \cref{euclid}, Euclid's algorithm computes the greatest common divisor.

\end{document}

You must log in to answer this question.

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