
\usepackage{algorithm,algpseudocode}
\usepackage{hyperref}
所以當我
\begin{algorithm}
\caption{Baseline}\label{ALG_baseline}
\begin{algorithmic}[1]
...
\end{algorithmic}
\end{algorithm}
輸出看起來有點像這樣:
--------------------------------------
Algorithm 1: Baseline
--------------------------------------
...
--------------------------------------
很明顯,已經有一個計數器了。
但
\autoref{ALG_baseline} is simply linked to with a number.
將產生 int
[1] is simply linked to with a number.
如何進行 autoref 輸出
[Algorithm 1] is ...
反而?
答案1
\autoref
僅適用於對應的\....autorefname
宏,即\sectionautorefname
等。
hyperref
從其特定標籤資訊中提取參考訊息,其中還儲存了相關計數器名稱。
這意味著,如果計數器名為foo
,則\fooautorefname
必須存在 - 否則它將被忽略(並向控制台發送警告)。
環境algorithm
有一個同名的計數器,所以
\newcommand{\algorithmautorefname}{Algorithm}
將提供hyperref
正確的資訊。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{hyperref}
\newcommand{\algorithmautorefname}{Algorithm}
\begin{document}
\begin{algorithm}
\caption{Baseline}\label{ALG_baseline}
%\begin{algorithmic}[1]
%
%\end{algorithmic}
\end{algorithm}
\autoref{ALG_baseline} is simply linked to with a number.
\end{document}
請注意,該cleveref
套件提供了類似的功能,但是,名稱也必須設置,\crefname{algorithm}{algorithm}{algorithms}
等等。
更新
這解決了OP關於檢測計數器名稱的評論。
文件摘錄.log
:
.log
計數器定義以 的形式寫入文件c@foo=\countY
,其中Y
是空閒計數器暫存器的編號,並不重要(除了少數例外)。
\@float@every@algorithm=\toks16
\c@algorithm=\count88
後來我們發現
\c@ALG@line=\count89
\c@ALG@rem=\count90
\c@ALG@nested=\count91
\ALG@tlm=\skip43
\ALG@thistlm=\skip44
\c@ALG@Lnr=\count92
\c@ALG@blocknr=\count93
\c@ALG@storecount=\count94
\c@ALG@tmpcounter=\count95
\ALG@tmplength=\skip45
這意味著ALG@line
(很可能)是行號計數器。
然而,由於@
名稱中的使用,它是一個“隱藏”計數器!
答案2
我花了半天時間才發現我必須使用 \renewcommand{} 而不是僅僅使用 \newcommand 來覆蓋 hyperref 預設演算法名稱。