
\autoref
アルゴリズムのコマンド名を設定するには、次を使用します。
\newcommand{\algorithmautorefname}{Algorithm}
コメントしたいいねここalgorithmic
. また、を使って環境内の行を参照できるようにしたいと思いますline x
。
\begin{algorithm}
\label{alg:myalg}
\begin{algorithmic}[1]
\State Do X
\State Do Y \label{algl:y}
\State $x = y + z$
\end{algorithmic}
\end{algorithm}
\autoref{alg:myalg}
正しく教えてくれるアルゴリズム1\autoref{algl:y}
さらに、生成も行いたい。2行目. これまで使用していたのは: line~\ref{algl:y}
.
見て同じ答え自動参照名を次のように定義できます: \....autorefname
。ただし、行番号など、一部のカウンターは非表示になっていると述べています: \c@ALG@line
。それらの自動参照名を作成することは可能ですか?
私が試してみました:
\newcommand{\ALGlineautorefname}{line}
\newcommand{\ALG_lineautorefname}{line}
\newcommand{\ALG@lineautorefname}{line}
しかし、どれも動作せず、最初のものだけがコンパイルされます。
可能であれば、環境\autorefname
内の行に対してを作成するにはどうすればよいですかalgorithmic
。
答え1
ラインカウンターは非表示になっていますhyperref
は、\autoref
カウンタが\addtocounter{ALG@line}{1}
の代わりにを使用してステップされるからです\refstepcounter{ALG@line}
。次のパッチはこれを修正し、 を定義できるようにします\ALG@lineautorefname
。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{hyperref,etoolbox}
\makeatletter
\patchcmd{\ALG@step}{\addtocounter{ALG@line}{1}}{\refstepcounter{ALG@line}}{}{}
\newcommand{\ALG@lineautorefname}{Line}
\makeatother
\newcommand{\algorithmautorefname}{Algorithm}
\begin{document}
See \autoref{alg:myalg}, specifically \autoref{algl:y}.
\begin{algorithm}
\caption{An algorithm}\label{alg:myalg}
\begin{algorithmic}[1]
\State Do X
\State Do Y \label{algl:y}
\State $x = y + z$
\end{algorithmic}
\end{algorithm}
\end{document}
答え2
以下はcleveref
ベースのソリューションです。注: \cref
(パッケージのメイン ユーザー マクロcleveref
) は と非常によく似ています\autoref
が、実際には よりもはるかに強力で柔軟です\autoref
。たとえば、\cref
は複数の引数を取ることができます。例については、以下のコードを参照してください。さらに良いニュース: は、cleveref
環境内の行を「行」と呼ぶ必要があることを認識するように「すぐに使える」ようにプログラムされているためalgorithmic
、この設定を自分で行う必要はありません。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage[colorlinks]{hyperref}
\usepackage[noabbrev,capitalize,nameinlink]{cleveref}
\begin{document}
\begin{algorithm}
\caption{Baseline}\label{ALG_baseline}
\begin{algorithmic}[1]
\State Do X
\State Do Y \label{algl:y}
\State $x = y + z$ \label{algl:sum}
\end{algorithmic}
\end{algorithm}
\cref{algl:y} in \cref{ALG_baseline} is \dots
\cref{algl:y,algl:sum} of the algorithm are based on \dots
\end{document}