1

Consider the following code:

\documentclass{article}
\usepackage[utf8]{inputenc}
%Algorithms:
\usepackage[boxruled]{algorithm2e} %Fancy algorithms
\usepackage{algcompatible}
\usepackage{amsmath}


\begin{document}

Blah blah blah

\begin{equation}
    E = mc^2
\end{equation}

\begin{algorithm}[ht!] 
  \SetAlgoNoLine
  \SetAlgoNoEnd
  \DontPrintSemicolon
  \caption{My method}
  \begin{minipage}[ht]{\dimexpr\textwidth-2\algomargin\relax}
  \begin{enumerate}
    \item Solve this equation:
        \begin{equation}
            \nabla f = u \tag{Alg1.1}
        \end{equation}
    \item Then, solve this equation:
        \begin{equation}
            \nabla f + cf = u' \tag{Alg1.2}
        \end{equation}
  \end{enumerate}
  \end{minipage}
%
\end{algorithm}

blah blah blah

\begin{equation}
    E \ne mc^3
\end{equation}

\begin{algorithm}[ht!] 
  \SetAlgoNoLine
  \SetAlgoNoEnd
  \DontPrintSemicolon
  \caption{My inverse method}
  \begin{minipage}[ht]{\dimexpr\textwidth-2\algomargin\relax}
  \begin{enumerate}
    \item Update $u$:
        \begin{equation}
            u = \nabla f \tag{Alg2.1}
        \end{equation}
    \item Update $u'$:
        \begin{equation}
            u' = \nabla f + cf \tag{Alg2.2}
        \end{equation}
  \end{enumerate}
  \end{minipage}
\end{algorithm}

blah blah blah

\begin{equation}
    E \ne mc^4
\end{equation}

\end{document}

Is there a way to automate the process of doing a different numbering system for equations inside the algorithm environment?

In particular, I would like all equations inside the Algorithms to be numbered as AlgX.Y where X is the algorithm number and Y is the equation number within Algorithm X.

2

You can use a method similar to subequations:

\documentclass{article}
\usepackage[utf8]{inputenc}
%Algorithms:
\usepackage[boxruled]{algorithm2e} %Fancy algorithms
\usepackage{algcompatible}
\usepackage{amsmath}
\usepackage{etoolbox}

\AtBeginEnvironment{algorithm}{\algoequations}
\AtEndEnvironment{algorithm}{\restoreequations}
\newcounter{algosavedequation}
\newcommand{\algoequations}{%
  \setcounter{algosavedequation}{\value{equation}}%
  \setcounter{equation}{0}%
  \renewcommand{\theequation}{Alg\thealgocf.\arabic{equation}}%
}
\newcommand{\restoreequations}{%
  \setcounter{equation}{\value{algosavedequation}}%
}


\begin{document}

Blah blah blah

\begin{equation}
    E = mc^2
\end{equation}

\begin{algorithm}[ht!] 
  \SetAlgoNoLine
  \SetAlgoNoEnd
  \DontPrintSemicolon
  \caption{My method}
  \begin{minipage}{\dimexpr\textwidth-2\algomargin\relax}
  \begin{enumerate}
    \item Solve this equation:
        \begin{equation}
            \nabla f = u
        \end{equation}
    \item Then, solve this equation:
        \begin{equation}
            \nabla f + cf = u'
        \end{equation}
  \end{enumerate}
  \end{minipage}
%
\end{algorithm}

blah blah blah

\begin{equation}
    E \ne mc^3
\end{equation}

\begin{algorithm}[ht!] 
  \SetAlgoNoLine
  \SetAlgoNoEnd
  \DontPrintSemicolon
  \caption{My inverse method}
  \begin{minipage}{\dimexpr\textwidth-2\algomargin\relax}
  \begin{enumerate}
    \item Update $u$:
        \begin{equation}
            u = \nabla f
        \end{equation}
    \item Update $u'$:
        \begin{equation}
            u' = \nabla f + cf
        \end{equation}
  \end{enumerate}
  \end{minipage}
\end{algorithm}

blah blah blah

\begin{equation}
    E \ne mc^4
\end{equation}

\end{document}

enter image description here

There is no ht option for minipage.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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