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
  • @frougon your advice really helped me, but the interesting thing is that I'm using algorithm2e and what helped me was the opposite of what you mentioned:

    `\usepackage[ruled,vlined,portuguese]{algorithm2e} % ... \usepackage{cleveref} % ... %\Crefname{algocf}{Algoritmo}{Algoritmos} % <----- DOESN'T WORK! \Crefname{algorithm}{Algoritmo}{Algoritmos} % <----- This works! % ... \begin{algorithm}[H] % ... \caption{How to write algorithms\label{algo:xxx}} \end{algorithm}

    Teste \Cref{algo:xxx}.`

    – igormcoelho Aug 02 '21 at 19:06
  • @igormcoelho It's quite possible that I mixed something up but AFAIK, you should put your \label{algo:xxx} after the \caption{...}, not inside its argument. ;) – frougon Aug 02 '21 at 22:08
  • @frougon yep, that was another good advice I took from you here... in fact, some years ago, the brazilian text guidelines (abnt) on latex, called ABNTex, required us to put \label inside \caption, otherwise count was incorrect, very strange thing. Then, I continued to do that for all these years. But I agree it's non-standard. Maybe even abntex has fixed this issue already, I don't know. Regarding the algorithm2e, my setup is quite crazy here, I'm using Pandoc and other stuff, so it could be another strange issue here as well. But it's solved. Thanks a lot! – igormcoelho Aug 03 '21 at 18:03
  • @igormcoelho In fact, \label is something legal in the mandatory arg of \part, \chapter, \section, etc. and, after a quick look at the code for \caption in the LaTeX kernel, it seems to me that it can work there too (a quick test agrees). One learns every day. Good night! – frougon Aug 03 '21 at 20:51

0 Answers0