4

I've noticed an apparent regression when using cleveref to reference lines of algorithms with the combination of algorithm and algcompatible. Below is an example:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}
\usepackage{cleveref}
\begin{document}
\begin{algorithm}[h]
    \caption{Some algorithm}
    \label{myalg}
    \begin{algorithmic}[1]
    \STATE An instruction \label{myline}
    \end{algorithmic}
\end{algorithm}
Reference to the algorithm: \cref{myalg} (should be "algorithm 1")\par
Reference to the line: \cref{myline} (should be "line 1")
\end{document}
With texlive 2024

Correct reference with texlive 2024

With texlive 2025

Incorrect reference with texlive 2025

Do you have any clue? In particular, I'm surprised by one thing: None of the packages loaded in my example have changed between texlive 2024 and 2025!

Note. The question is indeed very similar to this question, but the package for typesetting algorithms is different, hence the workaround too. I keep this question due to the adapted answer of cabohah.

4
  • 1
    See tex.stackexchange.com/a/730188/2388 Commented Mar 12, 2025 at 21:24
  • 2
    This question is similar to: algorithm2e + hyperref + cleveref: algorithms' lines are not numbered/referenced properly. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Mar 12, 2025 at 21:58
  • not the packages; LaTeX has changed. ;) Commented Mar 12, 2025 at 21:58
  • 2
    The difference between the two questions is the use of distinct packages, algcompatible vs algorithm2e. The source of error is probably the same but the fix doesn't work directly in my case. I'll look more closely and maybe post an adapted fix as an answer to my own question. Commented Mar 12, 2025 at 22:11

1 Answer 1

7

Package algorithmicx does not correctly setup up \@currentcounter. And cleveref does not know the counter type ALG@line. But you can add the definition of \@currentcounter to the algorithmic environment and also set an suitable alias (or alternatively define names for counter ALG@line):

\documentclass{article}
\usepackage{algorithm}
\usepackage{algcompatible}
\makeatletter
\AddToHook{env/algorithmic/begin}{\def\@currentcounter{ALG@line}}
\makeatother
\usepackage{cleveref}
\crefalias{ALG@line}{line}
\begin{document}
\begin{algorithm}[h]
    \caption{Some algorithm}
    \label{myalg}
    \begin{algorithmic}[1]
    \STATE An instruction \label{myline}
    \end{algorithmic}
\end{algorithm}
Reference to the algorithm: \cref{myalg} (should be "algorithm 1")\par
Reference to the line: \cref{myline} (should be "line 1")
\end{document}

This is similar to @UlrikeFischer's answer to algorithm2e + hyperref + cleveref: algorithms' lines are not numbered/referenced properly.

algorithm 1 and line 1

2
  • 1
    I did need to add \crefalias{ALG@line}{line} to my document, because otherwise the reference line 1 would show up as ?? 1. Commented Jul 8, 2025 at 10:08
  • I'm using algpseudocodex by the way, not algcompatible Commented Jul 8, 2025 at 10:19

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.