1

I am having issues figuring out how to reference an algorithm created with algorithm2e. For my figures, I use:

\begin{figure}
  \includegraphics{...}
  \caption{Test Caption.}
  \label{fig:test_label}
\end{figure}

and the reference with \Cref{fig:test_label} (using package cleveref). All works fine.

However, this does not seem to work with algortihm2e:

\begin{algorithm}
  \For{$b\gets 0$ \KwTo $B- 1$}{
      ...
  }
  \caption{Test Caption} 
  \label{alg:test_label}
\end{algorithm}

and then doing \Cref{alg:test_label} results in ??. I would like to have something similar to Figure 1, i.e. Algorithm 1 or Alg. 1.

How do I properly reference an algorithm created with algorithm2e?

Thanks!

EDIT: Upon further inspection, I noticed that the numbers are actually showing, but the word Algorithm is missing: ?? 1, ?? 2,...

4
  • 1
    Did you compile your test document twice? Commented Mar 7, 2023 at 16:23
  • 1
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Commented Mar 7, 2023 at 16:27
  • @Mico Yes, I compiled it twice. Commented Mar 8, 2023 at 6:29
  • @John Right, I ripped that out for the question here. Sry. Its back in there now. Commented Mar 8, 2023 at 6:30

1 Answer 1

2

I'm puzzled by your claim that

However, this does not seem to work with algortihm2e

For sure, the following minimalist test program

\documentclass{article} 
\usepackage{algorithm2e,cleveref}
\begin{document}

\begin{algorithm}
  \centering\dots \caption{Test a}\label{alg:a}
\end{algorithm}
\begin{algorithm}
  \centering\dots \caption{Test b}\label{alg:b}
\end{algorithm}

\Cref{alg:b}, \cref{alg:a,alg:b}.
\end{document}

produces this result after compiling twice:

enter image description here

However, be sure to include the package algorithm2e before cleveref, otherwise you will end up with:

enter image description here

2
  • Thanks. Indeed, it works. My issue was the order of including the two packages. I edited your answer to include this. Commented Mar 8, 2023 at 6:45
  • @Fabian - The general piece of advice is to load cleveref last. Commented Mar 8, 2023 at 11:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.