Não é possível referenciar o algoritmo

Não é possível referenciar o algoritmo

Por alguma razão, referenciar um algoritmo exibe sempre 0 em vez do número real do algoritmo. As referências funcionam, posso clicar nelas e isso me levará ao 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}

Responder1

\labelvai diretamente depois \caption:

insira a descrição da imagem aqui

\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}

Responder2

Coloque \labelimediatamente depois \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}

No exemplo que também mostrei como usar \autorefcom referência de algoritmo.

insira a descrição da imagem aqui

informação relacionada