这个问题是我之前一。我需要用任意名称来命名算法(包算法),因此它看起来像:
Algorithm MyAlgo
并且\ref{...}
将显示为MyAlgo
。
下一个代码是(通过休斯) 将 A 放在数字前面:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\renewcommand{\thealgorithm}{A\arabic{algorithm}}
\begin{document}
\begin{algorithm}
\caption{Euclid’s algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
Test reference: \ref{alg:euclid}
\end{document}
但是对于任意算法名称该怎么做呢?
编辑:我的问题可能不太清楚。我需要的是为算法指定任意名称(不带编号),这样它\ref{...}
就会显示为算法的名称。
答案1
这是你想要达到的目标吗?
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\newenvironment{varalgorithm}[1]
{\algorithm\renewcommand{\thealgorithm}{#1}}
{\endalgorithm}
\begin{document}
\begin{varalgorithm}{Euclid}
\caption{Euclid's algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if r is 0}
\State $a\gets b$
\State $b\gets r$
\State $r\gets a\bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{varalgorithm}
\begin{varalgorithm}{Ten}
\caption{Count to ten}\label{alg:ten}
\begin{algorithmic}
\State $x \gets 1$
\While{$x < 10$}
\State $x \gets x + 1$
\EndWhile
\end{algorithmic}
\end{varalgorithm}
Test reference: \ref{alg:euclid}
Test reference: \ref{alg:ten}
\end{document}