无法引用算法

无法引用算法

由于某种原因,引用算法时总是显示 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算法参考。

在此处输入图片描述

相关内容