0

I tried to use the tipps in this post cleveref and algorithm2e. But it does not lead to the correct referencing, what is done wrong here and how to i get cref running with algorithm?

\documentclass[]{article}
\usepackage{subcaption}

\usepackage{algorithm}
\usepackage{algpseudocode}


%\input{Packages}
\usepackage{cleveref}


\crefname{algocf}{alg.}{algs.}
\Crefname{algocf}{Algorithm}{Algorithms}

\begin{document}
    \section{asdja}
\cref{alg:a} is a algorithm.
\ref{alg:a} is a glrotihm
\begin{algorithm}
    \label{alg:a}
    \begin{algorithmic}[1]
        \State x
    \end{algorithmic}
    \caption{This is an algorithm}
\end{algorithm}
\end{document}
  • The problem is that you didn't place the \label after the \caption command. If you use \caption{This is an algorithm}\label{alg:a}, if works fine. See Why does an environment's label have to appear after the caption? – frougon Apr 15 '20 at 17:15
  • YES YES YES! It is working. – user3680510 Apr 15 '20 at 17:20
  • Glad to hear that. :-) – frougon Apr 15 '20 at 17:22
  • I forgot to say: you also need to use algorithm instead of algocf in the \crefname and \Crefname calls if you want them to be effective. The algocf counter is used by the algorithm2e package, which your sample code doesn't use. So, this was not completely a duplicate: half of the problem is the duplicate, the other half is what I said in this comment. – frougon Apr 15 '20 at 21:44