Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What I have: 3 algorithms, like so:

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

What I want: I want algorithm 1 to be numbered in the same manner as the rest of the algorithms in the document, say, as Algorithm 1. Then I want Algorithms 2 and 3 to be numbered Algorithm 1a (or 1.1, or 1-a, or whatever) and Algorithm 1b.

Then I want numbering to continue as previous, so that the next algorithm is Algorithm 2. Can this be done, and if so, how?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You could borrow the subequations definition from amsmath

\documentclass{article}
\usepackage{algorithm}

\makeatletter
\newcounter{parentalgorithm}
\newenvironment{subalgorithms}{%
  \refstepcounter{algorithm}%
  \protected@edef\theparentalgorithm{\thealgorithm}%
  \setcounter{parentalgorithm}{\value{algorithm}}%
  \setcounter{algorithm}{0}%
  \def\thealgorithm{\theparentalgorithm\alph{algorithm}}%
  \ignorespaces
}{%
  \setcounter{algorithm}{\value{parentalgorithm}}%
  \ignorespacesafterend
}
\makeatother

\begin{document}

\begin{subalgorithms}

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\end{subalgorithms}

\end{document}
share|improve this answer
2  
That's more elegant than my subalgorithms environment. But the \refstepcounter{algorithm} line needs to be taken out otherwise the numbers go 1–2a–2b–3 instead of 1–1a–1b–2 as the OP wanted. –  Matthew Leingang Nov 6 '10 at 13:47
    
This works perfectly, with two caveats: as Matthew points out, the \refstecounter{algorithm} line has to be taken out, and the parent algorithm has to be outside the subalgorithms environment. Two simple fixes for an excellent solution! –  philosodad Nov 6 '10 at 14:07

Here is the "easy" way to do it:

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{algorithm}
\renewcommand{\thealgorithm}{1a}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\renewcommand{\thealgorithm}{1b}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\begin{algorithm}
\setcounter{algorithm}{1}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

The "right" way to do it might be to create macros that turn on and off subnumbering.

\makeatletter
\newcounter{subalgorithm}
\newenvironment{subalgorithms}{
  \setcounter{subalgorithm}{0}
  % redefine the macro that sets the algorithm number to include the subalgorithm number
  \renewcommand{\thealgorithm}{\arabic{algorithm}\alph{subalgorithm}}%
  % patch the macro that sets the caption head to unincrement to
  % - unincrement the algorithm counter
  % - increment the subalgorithm counter
  % - then do whatever it was supposed to do before
  \let\old@fnum@algorithm=\fnum@algorithm%
  \def\fnum@algorithm{%
    \addtocounter{algorithm}{-1}%
    \stepcounter{subalgorithm}%
    \old@fnum@algorithm}%
}{}
\makeatother

\begin{document}

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm} 

\begin{subalgorithms}

\begin{algorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{algorithm}

\begin{algorithm}
\caption{Caption Three}
...
\label{alg:three}
\end{algorithm}

\end{subalgorithms}

\begin{algorithm}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

A third method would be to create a new float subalgorithm with a counter that reset with algorithm.

\makeatletter
\floatstyle{\ALG@floatstyle}
\newfloat{subalgorithm}{htbp}{loa}[algorithm]
\floatname{subalgorithm}{Subalgorithm}
\renewcommand{\thesubalgorithm}{\arabic{algorithm}\alph{subalgorithm}}
\@namedef{l@subalgorithm}{\@dottedtocline{1}{1.5em}{2.3em}}%
\makeatother

\begin{document}
\listofalgorithms

\begin{algorithm}
\caption{Caption One}
...
\label{alg:one}
\end{algorithm}

\begin{subalgorithm}
\caption{Caption Two}
...
\label{alg:two}
\end{subalgorithm}

\lipsum[2-4]

\begin{subalgorithm}
\caption{Caption Three}
...
\label{subalg:three}
\end{subalgorithm}

\begin{algorithm}
\caption{Caption Four}
...
\label{alg:four}
\end{algorithm}

References:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.