[Tex/LaTex] Algorithmic label cross references not working

algorithmicalgorithmscross-referencing

I'm using the Latex style files of ICML 2020, that can be found here. In particular, they supply algorithm.sty and algorithmic.sty. When I try to reference a line in an algorithm, I get a cross-reference to the algorithm itself. To make the problem even weirder, when I remove these two style files (and force my compiler to use the updated packages) the cross-reference becomes correct.

Any Ideas?

Here's a minimal working example (I kept the lines I felt are necessary):

\documentclass{article}
\newcommand{\theHalgorithm}{\arabic{algorithm}}
\usepackage{icml2020}
\begin{document}
\section{Introduction}
\label{submission}
\begin{algorithm}
  \caption{This is an algorithm}
  \label{alg:the_alg}
  \begin{algorithmic}[1]
    \STATE line number one \label{op0}
    \STATE line number two \label{op1}
  \end{algorithmic}
\end{algorithm}
This is Line \ref{op0}, and this is Line \ref{op1}.
\end{document}

This code produces the following:

enter image description here

Best Answer

The main problem here is on line 106 of algorithmic.sty:

106:  \newcommand{\ALC@it}{\addtocounter{ALC@line}{1}\addtocounter{ALC@rem}{1}\ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}\item}

The line counter number is incremented using

\addtocounter{ALC@line}{1}

instead of

\refstepcounter{ALC@line}

So, it should resemble

106:  \newcommand{\ALC@it}{\refstepcounter{ALC@line}\addtocounter{ALC@rem}{1}\ifthenelse{\equal{\arabic{ALC@rem}}{#1}}{\setcounter{ALC@rem}{0}}{}\item}

My suggestion would be to contact the ICML and request that they change their version of algorithmic.sty they release with their conference style to allow for proper referencing.

In the interim, you can define your own \alglinelabel macro that properly sets a \label that can be referenced:

enter image description here

\documentclass{article}

\newcommand{\theHalgorithm}{\arabic{algorithm}}

\newcommand{\alglinelabel}{%
  \addtocounter{ALC@line}{-1}% Reduce line counter by 1
  \refstepcounter{ALC@line}% Increment line counter with reference capability
  \label% Regular \label
}

\usepackage{icml2020}

\begin{document}

\section{Introduction}
\begin{algorithm}
  \caption{This is an algorithm}
  \label{alg:the_alg}
  \begin{algorithmic}[1]
    \STATE line number one \alglinelabel{op0}
    \STATE line number two \alglinelabel{op1}
  \end{algorithmic}
\end{algorithm}
This is Line \ref{op0}, and this is Line \ref{op1}.

\end{document}
Related Question