No se puede hacer referencia al algoritmo

No se puede hacer referencia al algoritmo

Por alguna razón, al hacer referencia a un algoritmo se muestra siempre 0 en lugar del número real del algoritmo. Las referencias funcionan, puedo hacer clic en ellas y me llevará al algoritmo.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{hyperref}

\begin{document}

\section{Introduction}
It doesn't work \ref{alg1} \ref{alg2}.

\begin{algorithm}
    \caption{Algorithm 1}
    \begin{algorithmic}[ht]
    \label{alg1}
        \State it's a test
    \end{algorithmic}
\end{algorithm}

\newpage

\begin{algorithm}
    \caption{Algorithm 2}
    \begin{algorithmic}[ht]
    \label{alg2}
        \State it's a test
    \end{algorithmic}
\end{algorithm}


\end{document}

Respuesta1

\labelva directamente después \caption:

ingrese la descripción de la imagen aquí

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{hyperref}

\begin{document}

\section{Introduction}
It doesn't work \ref{alg1} \ref{alg2}.

\begin{algorithm}
    \caption{Algorithm 1}
    \label{alg1}
    \begin{algorithmic}[ht]
        \State it's a test
    \end{algorithmic}
\end{algorithm}

\newpage

\begin{algorithm}
    \caption{Algorithm 2}
    \label{alg2}
    \begin{algorithmic}[ht]
        \State it's a test
    \end{algorithmic}
\end{algorithm}


\end{document}

Respuesta2

Coloque el \labelinmediatamente después \caption{…}:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{hyperref}

\newcommand*{\algorithmautorefname}{Algorithm}

\begin{document}

\section{Introduction}
It does work Algorithm~\ref{alg1} and \autoref{alg2}.

\begin{algorithm}
    \caption{Algorithm 1}
    \label{alg1}
    \begin{algorithmic}[ht]
        \State it's a test
    \end{algorithmic}
\end{algorithm}

\newpage

\begin{algorithm}
    \caption{Algorithm 2}
    \label{alg2}
    \begin{algorithmic}[ht]
        \State it's a test
    \end{algorithmic}
\end{algorithm}


\end{document}

En el ejemplo que también he mostrado, cómo usarlo \autorefcon referencia de algoritmo.

ingrese la descripción de la imagen aquí

información relacionada