LaTeX中算法环境设置

http://tex.stackexchange.com/questions/82888/algorithmic-arbitrary-names-for-algorithms

The question is advanced version of my previous one. I need to name algorithms (package algorithmic) with arbitrary names so it appears like:

Algorithm MyAlgo

and \ref{...} will appear like MyAlgo.

Next code is (by cmhughes) puts A in front of a number:

\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}

But how to do it for arbitrary algorithm name?

EDIT: Maybe I was unclear in my question. What I need is assigning to algorithms arbitrary names without numbering, so that \ref{...} will appear as name of the algorithm.

share edit flag
 
 
Not sure I undestand: \renewcommand{\thealgorithm}{MyAlgo\arabic{algorithm}}? –   Peter Grill Nov 15 '12 at 20:24
 
I don't understand it too but it works to make names like Algorithm A1 instead Algorithm 1 A1. Now I want to give arbitrary names to algorithms. –   msh  Nov 15 '12 at 20:28 
 
add comment
 

1 Answer

up vote 6 down vote accepted

Is this what you want to achieve?

\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}

enter image description here

share edit flag
  add comment

Your Answer


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值