3

I am trying a way to number algorithms 1a, 1b, 1c, 2a, 2b, 2c, etc. As I understand from the documentation of the algorithms package here, I can number them after the part, chapter, section and so on. In this related topic they discuss how to customize the number. But I don't want the number, I would like to also have letters: for example, in the following MWE, I would like to have the two algorithms numbered (and therefore referenced to as) 1a and 1b.

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
Algorithm~\ref{MyAlg} does things in a way. Algorithm~\ref{MyOtherAlg} does things in some other way.
\begin{algorithm}\caption{My algorithm}\label{MyAlg}
\begin{algorithmic}[1]
\State Do stuff.
\end{algorithmic}
\end{algorithm}
\begin{algorithm}\caption{My other algorithm}\label{MyOtherAlg}
\begin{algorithmic}[1]
\State Do stuff differently.
\end{algorithmic}
\end{algorithm}
\end{document}

Any idea on how to achieve this?

7
  • And what represents the "1"? Is it associated to a section? Commented Jun 18, 2015 at 17:12
  • no, it can be a custom number Commented Jun 18, 2015 at 17:24
  • What is a sequential counter? A user-defined one? Commented Jun 18, 2015 at 17:25
  • See my edit: a custom number. It might as well not be there. For example I may want to number them simply "A" and "B", let's say. Hand curated numbering. Commented Jun 18, 2015 at 17:28
  • Please see my recently updated answer. Something like that (see the las to examples after "Update")? Commented Jun 18, 2015 at 17:30

1 Answer 1

2

All you have to do is to redefine the command \thealgorithm which controls the representation for the algorithm counter. For example, to number them using alphabetic lower-case characters and to prepend the section number you would do

\renewcommand\thealgorithm{\thesection\alph{algorithm}}

Assuming that you want the counter to reset at every section, you will also need

\usepackage{chngcntr}
\counterwithin{algorithm}{section}

A complete example.

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

\counterwithin{algorithm}{section}
\renewcommand\thealgorithm{\thesection\alph{algorithm}}

\begin{document}
\section{Test section}

Algorithm~\ref{MyAlg} does things in a way. Algorithm~\ref{MyOtherAlg} does things in some other way.
\begin{algorithm}\caption{My algorithm}\label{MyAlg}
\begin{algorithmic}[1]
\State Do stuff.
\end{algorithmic}
\end{algorithm}
\begin{algorithm}\caption{My other algorithm}\label{MyOtherAlg}
\begin{algorithmic}[1]
\State Do stuff differently.
\end{algorithmic}
\end{algorithm}

\end{document}

The result:

enter image description here

Update:

Same idea if a user-defined counter is to be used:

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

\newcounter{mycount}
\counterwithin{algorithm}{mycount}
\refstepcounter{mycount}
\renewcommand\thealgorithm{\arabic{mycount}\alph{algorithm}}

\begin{document}
\section{Test section}

Algorithm~\ref{MyAlg} does things in a way. Algorithm~\ref{MyOtherAlg} does things in some other way.
\begin{algorithm}\caption{My algorithm}\label{MyAlg}
\begin{algorithmic}[1]
\State Do stuff.
\end{algorithmic}
\end{algorithm}
\begin{algorithm}\caption{My other algorithm}\label{MyOtherAlg}
\begin{algorithmic}[1]
\State Do stuff differently.
\end{algorithmic}
\end{algorithm}

\end{document}

or to have the numbering with upper-case alphabetical characters:

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

\renewcommand\thealgorithm{\Alph{algorithm}}

\begin{document}
\section{Test section}

Algorithm~\ref{MyAlg} does things in a way. Algorithm~\ref{MyOtherAlg} does things in some other way.
\begin{algorithm}\caption{My algorithm}\label{MyAlg}
\begin{algorithmic}[1]
\State Do stuff.
\end{algorithmic}
\end{algorithm}
\begin{algorithm}\caption{My other algorithm}\label{MyOtherAlg}
\begin{algorithmic}[1]
\State Do stuff differently.
\end{algorithmic}
\end{algorithm}

\end{document}

The result:

enter image description here

1
  • Can we do something like this: For 2 algorithms Algorithm 2(a) and Algorithm 2(b) and the rest of the Algorithms like Algorithm 1, Algorithm 3, etc follow the normal naming and numbering styles? Commented Aug 16, 2021 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .