4

I want to use algorithm2e to generate two procedure and two algorithm. The procedure and algorithm may be interleaved with one another, and both procedure and algorithm have their independent counter number.

I used the option procnumbered to algorithm2e so that the procedure environment can be numbered, but I find another problem: the procedure and algorithm environment only share one counter number. How can I number these two environments separately, each with its own counter?

As an example, how can I number the procedure as 1, and the algorithm as 1 too (not 2). Thanks.

enter image description here

\documentclass{article}
\usepackage[procnumbered,ruled]{algorithm2e}
\begin{document}

\begin{procedure}
  \caption{myproc()}
  This is a procedure
\end{procedure}

\begin{algorithm}
  \caption{My algorithm}
  This is a procedure
\end{algorithm}

\end{document}

1 Answer 1

4

Both environments use the algocf counter; you can teach LaTeX to use another one for procedures.

\documentclass{article}
\usepackage[procnumbered,ruled]{algorithm2e}
\usepackage{etoolbox}
\newcounter{procedure}
\makeatletter
\AtBeginEnvironment{procedure}{\let\c@algocf\c@procedure}
\makeatother
\begin{document}

\begin{procedure}[!htp]
  \caption{myproc()}
  This is a procedure
\end{procedure}

\begin{algorithm}[!htp]
  \caption{My algorithm}
  This is a procedure
\end{algorithm}

\begin{procedure}[!htp]
  \caption{myproc()}\label{aproc}
  This is a procedure
\end{procedure}

\begin{procedure}[!htp]
  \caption{myproc()}
  This is a procedure
\end{procedure}

\begin{algorithm}[!htp]
  \caption{My algorithm}
  This is a procedure
\end{algorithm}

Procedure~\ref{aproc}

\end{document}

enter image description here

You must log in to answer this question.

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