Multiple Counters on algorithm

I'm using the algorithm package and want to number two types of algorithms with separate counters.

I would like to know, what you think about my following code

Are there any mistakes? Could something be done better?

\documentclass[a4paper, 12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{caption}

\newcounter{typeACounter}
\newcounter{typeBCounter}

\newenvironment{typeA}[1]
{
\renewcommand\labelenumi{\bfseries\theenumi.}
\setcounter{algorithm}{\value{typeACounter}}
\floatname{algorithm}{TypeA}
\begin{algorithm}[H]
\caption{#1}
\begin{algorithmic}
\STATE
\begin{enumerate}
}
{
\end{enumerate}
\end{algorithmic}
\end{algorithm}
}

\newenvironment{typeB}[1]
{
\setcounter{algorithm}{\value{typeBCounter}}
\floatname{algorithm}{TypeB}
\begin{algorithm}[H]
\caption{#1}
\begin{algorithmic}
}
{
\end{algorithmic}
\end{algorithm}
}

\begin{document}

\begin{typeB}{test}
\IF{a}
\STATE b
\ENDIF
\end{typeB}

\begin{typeB}{test}
\IF{a}
\STATE b
\ENDIF
\end{typeB}

\begin{typeA}{test}
\item t
\item tt
\end{typeA}

\begin{typeB}{test}
\IF{a}
\STATE b
\ENDIF
\end{typeB}

\begin{typeA}{test}
\item t
\item tt
\end{typeA}

\begin{typeA}{test}
\item t
\item tt
\end{typeA}

\begin{typeB}{test}
\IF{a}
\STATE b
\ENDIF
\end{typeB}

\end{document}


Which produces:

What you have done is fine as it works. I don't like code duplication so I would do this by defining only one environment that takes the type, A or B, as an argument. The latex counters are quite flexible so that something like \value{type#1Counter} will work.

Secondly, rather than writing \begin{algorithm}...\end{algorithm} inside your environment I would just use \algorithm...\endalgorithm. The two are almost the same except that that the first version puts everything inside its' own group, which isn't really necessary here because your environment starts its own group.

Finally, I'd add some %'s at the ends of the lines to avoid the environment accidentally adding extra space. They are probably not necessary but I'm lazy and don't want to keep track of when spaces matter and when they don't.

Here's an updated version of your code with these changes. The output is the same.

\documentclass[a4paper, 12pt]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage{caption}

\newcounter{typeACounter}
\newcounter{typeBCounter}

\renewcommand\labelenumi{\bfseries\theenumi.}
\newenvironment{Algorithm}[2]%
{ \setcounter{algorithm}{\value{type#1Counter}}%
\stepcounter{type#1Counter}%
\floatname{algorithm}{Type #1}%
\algorithm[H]%
\caption{#2}%
\algorithmic%
\ifx#1A\relax% add \STATE and start enumerate if A
\STATE\enumerate\let\myending\endenumerate%
\else\let\myending\relax%
\fi%%
}{\myending\endalgorithmic\endalgorithm}

\begin{document}

\begin{Algorithm}{B}{test}
\IF{a}
\STATE b
\ENDIF
\end{Algorithm}

\begin{Algorithm}{B}{test}
\IF{a}
\STATE b
\ENDIF
\end{Algorithm}

\begin{Algorithm}{A}{test}
\item t
\item tt
\end{Algorithm}

\begin{Algorithm}{B}{test}
\IF{a}
\STATE b
\ENDIF
\end{Algorithm}

\begin{Algorithm}{A}{test}
\item t
\item tt
\end{Algorithm}

\begin{Algorithm}{A}{test}
\item t
\item tt
\end{Algorithm}

\begin{Algorithm}{B}{test}
\IF{a}
\STATE b
\ENDIF
\end{Algorithm}

\end{document}


The \ifxA#1\relax test is there to insert the \STATE and \enumerate environment if we are in type A. It also sets \myending to be \endenumerate in this case and \relax otherwise, which is TeX's way of doing nothing.

Oh, and I moved the \renewcommand\labelenumi out of the environment definition.

• Since I don't want to set the items bold in the hole document, I guess I just leave it where it has been? – Ben Aug 26 '16 at 11:20
• My floatnames actually aren't Typ A / Typ B, so \ifx#1A\relax ABC% \else XYZ% \fi}% would be a good idea, wouldn't it? – Ben Aug 26 '16 at 11:32
• @Ben Yes, on both counts! – Andrew Aug 26 '16 at 21:19