![无法引用算法](https://linux22.com/image/461828/%E6%97%A0%E6%B3%95%E5%BC%95%E7%94%A8%E7%AE%97%E6%B3%95.png)
由于某种原因,引用算法时总是显示 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
算法参考。