# cleveref fails to reference algorithms

In the following example document, both \autoref and plain ref will correctly link and number an algorithm reference, but cleveref fails that task. It will just insert question marks instead. In another, far more complex document I have, it does insert correct numbers but links them all to page 1. Since the documentation of cleveref sounds like it should support everything other referencing packages support and more, I'd have hoped this was supported, too.

\documentclass{article}
\usepackage{hyperref}
\usepackage{algorithm}
\usepackage{algpseudocode}
\providecommand\algorithmname{algorithm}
\begin{document}
We want the real content on page two to better detect errors.
\newpage

\begin{algorithm}
\caption{Incrementation step}\label{alg:inc}
\begin{algorithmic}
\State $x\gets x+1$
\end{algorithmic}
\end{algorithm}
Comparing auto like \autoref{alg:inc},
clever like \cref{alg:inc} % this gives the error message
and manual like algorithm~\ref{alg:inc}.
\end{document}


Compiling the above I get

LaTeX Warning: Reference alg:inc' on page 1 undefined on input line 18.


If I move the algorithm package before cleveref, I get an error instead of that warning:

(/usr/share/texmf-dist/tex/latex/cleveref/cleveref.sty
! Undefined control sequence.
<argument> \ALG@beginalgorithmic

l.2843   }{}


But if I move the algpseudocode before cleveref, I'm back to the beginning.

This is a Gentoo texlive 2012 installation, with the following packages involved:

• cleveref 2012/03/07 v0.18.5
• hyperref 2012/05/13 v6.82q
• algorithm 2009/08/24 v0.1
• algorithmicx 2005/04/27 v1.2
-
–  Marco Daniel May 11 '13 at 14:30
@MarcoDaniel: Looks more like the opposite of what I have in my more complex real world example: that question describes a situation where links work but text fails, whereas my real document has correct text but broken links. The example above is broken in both ways. I whish I knew why these differences. But the solution to that question appears to be specific to algorithm2e which I don't use. And probably won't use any time soon, since it doesn't seem to integrate well with my other plans. –  MvG May 11 '13 at 15:06
Please also note that your version of cleveref is quite old (dare I say obsolete?). The most recent version is 0.18.9; among its improvements are several updates to keep up with recent changes in some of the algo packages... You may want to update all of these packages. –  Mico May 11 '13 at 16:16
@Mico: I had hoped that using a texlive 2012 install just as my distro provides it would maximize interoperability between packages. Things might be old, but as long as they should be equally old… However, I noticed that my Gentoo Texlive 2012 comes with mdframed v1.6b, as opposed to the v1.5 on my OS X. And 1.6b requires a fix to work with amsthm. So my belief that unmodified texlive releases would be a good foundation for portability is not as strong as it used to be. –  MvG May 11 '13 at 16:23

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

% hyperref must go last
\usepackage{hyperref}

% but cleveref goes "laster" than hyperref

\providecommand\algorithmname{algorithm}

\begin{document}
We want the real content on page two to better detect errors.
\newpage

\begin{algorithm}
\caption{Incrementation step}\label{alg:inc}
\begin{algorithmic}
\State $x\gets x+1$
\end{algorithmic}
\end{algorithm}
Comparing auto like \autoref{alg:inc},
clever like \cref{alg:inc}
and manual like algorithm~\ref{alg:inc}.
\end{document}


-
I had hoped it would be something as easy to fix as this. Easy once you know what to do, that is. Is there some way to figure out correct package order, short of reading all the docs of all involved packages, or trial and error? I know I had read somewhere that hyperref should go last, but on the other hand I figured that a lot of other packages will probably depend on hyprref, so just because I list it last won't mean it gets included last. –  MvG May 11 '13 at 15:10
@MvG A rule of thumb is that hyperref goes last; however a few package should go after it: cleveref is the main one, but also bookmark (that provides easier management of PDF bookmarks). –  egreg May 11 '13 at 15:21
I just got a pointer to “Which packages should be loaded after hyperref instead of before?”. Might be a good reference in this context here, since it has some more packages that go after hyperref`. Seems that is one package where order is particularly important. Thanks for your help in this! –  MvG May 11 '13 at 16:28