[Tex/LaTex] algorithm2e: How to customize caption of the “procedure” environment

algorithm2enamingnumbering

The procedure environment requires the caption to be in a very restricted form: "Name()". However, I would like to add more details like: "my awesome procedure $A = (para1, para2)$". How would I do that?

Actually, this caption is possible in the algorithm environment, alternatively, how to change the label, e.g. Algorithm 1, of the environment into "procedure".

Best Answer

If you just want to change Algorithm to Procedure, you can do the following:

enter image description here

\documentclass{article}

\usepackage{algorithm2e}
\renewcommand{\algorithmcfname}{Procedure}

\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }

  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

The name of the algorithm is stored in \algorithmcfname, which you can redefine to suit your needs. You can also use \SetAlgorithmName{#1}{#2}{#3} where

  • #1 denotes the name associated with the List of Algorithms
  • #2 denotes the name associated with the algorithm environment
  • #3 denotes the name associated with hyperref's \autoref name

Assuming that you want to remove the numbering of the Procedure, you also want it removed from the List of Algorithms, and therefore not have any counter associated with it. The following redefinition of \@caption within a new algoproc environment does that:

enter image description here

\documentclass{article}

\usepackage[ruled]{algorithm2e}

\makeatletter
\newenvironment{algoproc}[1][]
  {\renewcommand{\algorithmcfname}{Procedure}%
   \begin{algorithm}[#1]
   \long\def\@caption##1[##2]##3{%
     \par
     \begingroup\@parboxrestore
     \if@minipage\@setminipage\fi
     \normalsize \@makecaption{\AlCapSty{\AlCapFnt\algorithmcfname}}{\ignorespaces ##3}%
     \par\endgroup
   }}
  {\end{algorithm}}
\makeatother

\begin{document}

\begin{algoproc}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }

  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algoproc}

\end{document}