1

I'm using the algorithms package and the algorithm environment has its own numbering. I want to number the algorithms with the equations. That is, if I write

\begin{equation} 1 = 1 \end{equation}
\begin{algorithm} 
     \caption{Some Algorithm}
     \label{alg}
\end{algorithm}

I want the first equation to be numbered 1 and the algorithm to be numbered 2.
The documentation teaches a way to number the algorithms within chapters, sections, etc. But I didn't find anything about this.

Thanks.

EDIT: My MWE

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath,amsthm,amsfonts}
\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\begin{document}

\begin{equation} \label{eq}
1 = 0
\end{equation}

\begin{algorithm}
\caption{My Algorithm}
\label{al}
\begin{algorithmic}
\Function{Alg}{n}
\State \Return $n$
\EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}

1 Answers1

1

You make the algorithm counter be exactly the same as the equation counter using

\makeatletter
\let\c@algorithm\c@equation % algorithm counter is exactly the same as equation
\makeatother

Note though that the document elements you're trying to equate are completely different. The one is set in-place (as you're coding it) while the other typically floats around to find a suitable spot.

The following minimal example does what you ask and also highlights the out-of-sync enumeration if algorithm floats:

enter image description here

\documentclass{article}

\usepackage{algorithm}

\makeatletter
\let\c@algorithm\c@equation % algorithm counter is exactly the same as equation
\makeatother

\begin{document}

Some text before the equation.
\begin{equation}
  f(x) = ax^2 + bx + c
\end{equation}
Some text after the equation.

\begin{algorithm}[t]
  \caption{Some Algorithm}
\end{algorithm}

Some text after the \texttt{algorithm} \emph{float}. Another equation
\begin{equation}
  g(x) = ax^2 + bx + c
\end{equation}
at the end.

\end{document}

Relevant: How to influence the position of float environments like figure and table in LaTeX?

Werner
  • 603,163