[Tex/LaTex] How to put a custom tag to the algorithm environment

algorithmsnumbering

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?

Best Answer

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

Related Question