アルゴリズムを参照できません

アルゴリズムを参照できません

何らかの理由で、アルゴリズムを参照すると、アルゴリズムの実際の番号ではなく、常に 0 が表示されます。参照は機能しており、クリックするとアルゴリズムに移動できます。

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

答え1

\labelの直後に続きます\caption:

ここに画像の説明を入力してください

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

答え2

\labelの直後にを配置します\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}

例では、\autorefアルゴリズム参照の使用方法も示しました。

ここに画像の説明を入力してください

関連情報