7

I've been trying to use this answer Changing label name for algorithm to no avail. I use the packages:

\usepackage{algorithm}
\usepackage{algpseudocode}

And this code for defining new environment:

\newenvironment{megaalgorithm}[1][htb]{
    \renewcommand{\algorithmcfname}{MegaAlgorithm}% Update algorithm name
   \begin{algorithm}[#1]%
  }{\end{algorithm}}

But then it tells me that there is an error:

Latex Error: \algorithmcfname undefined

I can't use algorithm2e as in previous example as it breaks my current code.

2
  • Untested, due to the lack of an MWE: \makeatletter\renewcommand{\ALG@name}{MEGAAlgorithm}\makeatother Commented Feb 27, 2015 at 18:49
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Commented Feb 27, 2015 at 18:52

1 Answer 1

15

The right one when using the algorithm package is \ALG@name and not \algorithmcfname.

So you have to define your new environment as

\makeatletter
\newenvironment{megaalgorithm}[1][htb]{%
    \renewcommand{\ALG@name}{MegaAlgorithm}% Update algorithm name
   \begin{algorithm}[#1]%
  }{\end{algorithm}}
\makeatother

MWE

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\makeatletter
\newenvironment{megaalgorithm}[1][htb]{%
    \renewcommand{\ALG@name}{MegaAlgorithm}% Update algorithm name
   \begin{algorithm}[#1]%
  }{\end{algorithm}}
\makeatother

\begin{document} 

\begin{megaalgorithm}
\begin{algorithmic}
\State Hello
\end{algorithmic}
\caption{A mega algorithm}
\end{megaalgorithm}

\end{document} 

enter image description here


In algorithm.sty (part of the algorithms bundle) you can find:

\newcommand{\ALG@name}{Algorithm}

and

\floatname{algorithm}{\ALG@name}

So, another option is to define your new environment as

\newenvironment{megaalgorithm}[1][htb]{%
    \floatname{algorithm}{MegaAlgorithm}% Update algorithm name
   \begin{algorithm}[#1]%
  }{\end{algorithm}}

The first method is taken directly from the style file, while the latter can be found in subsection 4.4, "Customization", of the algorithms documentation

4
  • Thank you! For the future, where should I look for things like \ALG@name, I haven't been able to find anything like this in the algorithmicx documentation. Commented Feb 28, 2015 at 13:48
  • @paewel You're welcome. I've added some explanations in the edit. Commented Feb 28, 2015 at 16:45
  • How can I use separate numbering for this new environment. My document has both the basic algorithm environment as well as the newly defined one. Commented Oct 13, 2018 at 16:42
  • 1
    My first instinct would be to use \floatname{algorithm}{MegaAlgorithm}, since that's what the package manual says to use. Why might you want to use \renewcommand{\ALG@name}{MegaAlgorithm} (surrounded by \makeatletter and \makeatother)? Commented Jan 11, 2019 at 15:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.