演算法的 autoref

演算法的 autoref
\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 預設演算法名稱。

相關內容