如何对算法进行编号(将某些算法归为一组)?

如何对算法进行编号(将某些算法归为一组)?

我想写几组算法,比如有2个组,每组有2个相关算法。

我不知道在这种情况下如何对每个算法进行编号(如 1.1.1、1.1.2、3.1.1、3.1.2;格式为:

部分数。部分中的组数。组中的算法数。

\documentclass{article}

\usepackage{algorithm}
\usepackage{algorithmic}
\setlength{\intextsep}{2pt}

\begin{document}

\section{A}
There is the first group of algorithm
\begin{figure*}[htbp]
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$A_{1}\left( x_{1} \right)$}
        \STATE $y_{1} \leftarrow f\left( x_{1} \right)$
        \RETURN $y_{1}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
     \begin{algorithmic}[1]
        \caption{$A_{2}\left( x_{2} \right)$}
        \STATE $y_{2} \leftarrow f\left( x_{2} \right)$
        \RETURN $y_{2}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure*}

Blah, blah, blah...
\section{B}
Blah, blah, blah...
\section{C}
There is the second group of algorithm
\begin{figure*}[htbp]
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$C_{1}\left( x_{1} \right)$}
        \STATE $y_{1} \leftarrow f\left( x_{1} \right)$
        \RETURN $y_{1}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$C_{2}\left( x_{2} \right)$}
        \STATE $y_{2} \leftarrow f\left( x_{2} \right)$
        \RETURN $y_{2}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure*}
\end{document}

在此处输入图片描述

答案1

添加计数器algroup并将计数器设置为自动重置为零。

\documentclass{article}

\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\setlength{\intextsep}{2pt}

\newcounter{algroup}[section]
\renewcommand{\thealgroup}{\thesection.\arabic{algroup}}
\counterwithin{algorithm}{algroup}
\renewcommand{\thealgorithm}{\thealgroup.\arabic{algorithm}}

\begin{document}

\section{A}
There is the first group of algorithm
\stepcounter{algroup}
\begin{figure*}[htbp]
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$A_{1}\left( x_{1} \right)$}
        \State $y_{1} \leftarrow f\left( x_{1} \right)$
        \Return $y_{1}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$A_{2}\left( x_{2} \right)$}
        \State $y_{2} \leftarrow f\left( x_{2} \right)$
        \Return $y_{2}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure*}

Blah, blah, blah...
\section{B}
Blah, blah, blah...
\section{C}
There is the second group of algorithm
\stepcounter{algroup}
\begin{figure*}[htbp]
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$C_{1}\left( x_{1} \right)$}
        \State $y_{1} \leftarrow f\left( x_{1} \right)$
        \Return $y_{1}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \hfill
  \begin{minipage}[t]{.49\textwidth}
    \begin{algorithm}[H]
      \begin{algorithmic}[1]
        \caption{$C_{2}\left( x_{2} \right)$}
        \State $y_{2} \leftarrow f\left( x_{2} \right)$
        \Return $y_{2}$
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure*}
\end{document}

演示

相关内容