算法环境中的单独方程编号

算法环境中的单独方程编号

考虑以下代码:

\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}

有没有办法可以自动化在算法环境内对方程式执行不同的编号系统的过程?

具体来说,我希望算法内的所有方程式都编号为 AlgX.Y,其中 X 是算法编号,Y 是算法 X 内的方程式编号。

答案1

您可以使用类似如下的方法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}

在此处输入图片描述

没有ht选项minipage

相关内容