算法子编号

算法子编号

我有 MWE:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}

\noindent
\begin{minipage}[tb]{\textwidth}    \centering \setlength{\intextsep}{0pt}
    \begin{minipage}[t]{.49\textwidth}
        \null
        \begin{algorithm}[H]
            \caption{New algorithm 1a}\label{a:1a}
            \begin{algorithmic}[1]

                \State\Return
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
    \hfill\vline\hfill
    \begin{minipage}[t]{.49\textwidth}
        \null
        \begin{algorithm}[H]
            \caption{New algorithm 1b}\label{a:1b}
            \begin{algorithmic}[1]

                \State\Return
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
\end{minipage}

\end{document}

我需要将左侧算法编号重命名为 1(a),将右侧算法编号重命名为 1(b)。之后,我有另一个全页宽度的算法,为此我需要算法编号 2。

我怎样才能做到这一点?

答案1

subequations这个解决方案类似于amsmath:里面的subalgorithms算法将被编号<main><letter>,与最终的格式无关。

我添加了垂直调整以便在两个对象之间获得更好的规则(但我看不出需要该规则)。

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newcounter{savealgorithm}
\newenvironment{subalgorithms}
 {%
  \stepcounter{algorithm}%
  \edef\currentthealgorithm{\thealgorithm}%
  \setcounter{savealgorithm}{\value{algorithm}}%
  \setcounter{algorithm}{0}%
  \renewcommand{\thealgorithm}{\currentthealgorithm\alph{algorithm}}%
 }
 {%
  \setcounter{algorithm}{\value{savealgorithm}}%
 }

\begin{document}

\noindent
\begin{minipage}{\textwidth}
\begin{subalgorithms}

\begin{minipage}[t]{.49\textwidth}
  \vspace*{-\baselineskip}
  \begin{algorithm}[H]
  \caption{New algorithm 1a}\label{a:1a}
  \begin{algorithmic}[1]
    \State\Return
  \end{algorithmic}
  \end{algorithm}
\end{minipage}%
\hfill\vline\hfill
\begin{minipage}[t]{.49\textwidth}
  \vspace*{-\baselineskip}
  \begin{algorithm}[H]
  \caption{New algorithm 1b}\label{a:1b}
  \begin{algorithmic}[1]
  \State\Return
  \end{algorithmic}
  \end{algorithm}
\end{minipage}

\end{subalgorithms}
\end{minipage}

\bigskip

\noindent
\begin{minipage}{\textwidth}

\begin{algorithm}[H]
\caption{New algorithm 2}\label{a:2}
\begin{algorithmic}[1]
  \State\Return
\end{algorithmic}
\end{algorithm}

\end{minipage}

\end{document}

在此处输入图片描述

答案2

由于您将它们放在组 a 中minipage,因此您可以本地重新定义计数器的显示algorithm以表示您的子编号。虽然后台的计数器仍将照常递增,但\labels 将使用\thealgorithm并正确引用算法。

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\noindent
\begin{minipage}{\textwidth}
  \centering \setlength{\intextsep}{0pt}%
  \stepcounter{algorithm}% Increment algorithm counter
  \edef\algbasenum{\thealgorithm}% Store current value
  % Local redefinition...
  \renewcommand{\thealgorithm}{\algbasenum (a)}% Update algorithm number display
  \begin{minipage}[t]{.49\textwidth}
    \null
    \begin{algorithm}[H]
      \caption{New algorithm 1a}
      \begin{algorithmic}[1]
        \State\Return
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \hfill\vline\hfill
  % Local redefinition...
  \renewcommand{\thealgorithm}{\algbasenum (b)}% Update algorithm number display
  \begin{minipage}[t]{.49\textwidth}
    \null
    \begin{algorithm}[H]
      \caption{New algorithm 1b}
      \begin{algorithmic}[1]
        \State\Return
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
  \addtocounter{algorithm}{-2}% Restore algorithm counter
\end{minipage}

\begin{algorithm}[H]
  \caption{New algorithm 2}
  \begin{algorithmic}[1]
    \State\Return
  \end{algorithmic}
\end{algorithm}

\end{document}

由于 的重新定义\thealgorithm仅对组进行本地定义,因此它将在 恢复为其默认值\arabic{algorithm}-- \end{minipage}

答案3

可以使用\ContinuedFloatcaption包的功能:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption} % - This line added
\begin{document}

\noindent
\begin{minipage}[tb]{\textwidth}    \centering \setlength{\intextsep}{0pt}
    \renewcommand\theContinuedFloat{\alph{ContinuedFloat}} % - This line added
    \begin{minipage}[t]{.49\textwidth}
        \null
        \begin{algorithm}[H]
            \ContinuedFloat* % - This line added
            \caption{New algorithm 1a}\label{a:1a}
            \begin{algorithmic}[1]

                \State\Return
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
    \hfill\vline\hfill
    \begin{minipage}[t]{.49\textwidth}
        \null
        \begin{algorithm}[H]
            \ContinuedFloat % - This line added
            \caption{New algorithm 1b}\label{a:1b}
            \begin{algorithmic}[1]

                \State\Return
            \end{algorithmic}
        \end{algorithm}
    \end{minipage}
\end{minipage}

\begin{algorithm}[H]
    \caption{New algorithm 2}\label{a:2}
    \begin{algorithmic}[1]

        \State\Return
    \end{algorithmic}
\end{algorithm}

\end{document}

结果截图

答案4

这是我的方法,最初基于沃纳埃格尔的答案。

这是一个可嵌套的环境,它将为其中定义的任何算法向当前图形名称附加一个计数器。

\newenvironment{subalgorithms}{%
    \edef\lastAlgorithm{\value{algorithm}}% Store the current algorithm counter (parent algorithm counter)
    \edef\parentAlgorithm{\thealgorithm}% Store the value of \thealgorithm which is the entire algorithm number
    \setcounter{algorithm}{0}% Reset algorithm counter (to count subalgorithms)
    % Redefine \thealgorithm to include the parent algorithm number
    \renewcommand{\thealgorithm}{\parentAlgorithm.\arabic{algorithm}}%
}{%
    \edef\previousAlgorithm{\value{algorithm}}% Store the 'last breadcrumb' algorithm counter
    \setcounter{algorithm}{\lastAlgorithm}% Restore algorithm counter (to parent algorithm counter)
    \addtocounter{algorithm}{-\previousAlgorithm}% Subtract subalgorithm count from parent algorithm count
    \addtocounter{algorithm}{1}% But add 1 to account for the parent algorithm
}

示例用法

代码的 LaTeX 渲染示例

\begin{algorithm}
    \caption{The SAT Function}
\end{algorithm}

\subsubsection{The Auxiliary Functions}
\begin{subalgorithms}
    \begin{algorithm}[H]
        \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
        \label{alg:sat_ex}\label{alg:the-sat-ex-algorithm-for-ctl}
        \index{SAT!Formal Definition for CTL!$SAT_{EX}$ Auxiliary Function}
    \end{algorithm}

    \begin{algorithm}[H]
        \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
    \end{algorithm}

    \begin{algorithm}[H]
        \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
    \end{algorithm}

    \begin{subalgorithms}
        \begin{algorithm}[H]
            \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
        \end{algorithm}

        \begin{algorithm}[H]
            \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
        \end{algorithm}
    \end{subalgorithms}
\end{subalgorithms}

\begin{algorithm}
    \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
\end{algorithm}

\begin{algorithm}
    \caption{The $SAT_{EX}$ Auxiliary Function for SAT}
\end{algorithm}

注意图形将从子算法环境之前的任何算法继承“父”图形编号(即图形编号的前导部分)。因此,如果您将多个subalgorithms环境接连放置,则除第一个环境之外的所有环境都会重复编号。

这是一种设计选择,允许您简单地subalgorithms在算法之后放置一个环境以为subalgorithms其添加附加内容。

如果您希望将多个subalgorithms环境一个接一个地放置,只需\addtocounter{algorithm}{1}在环境之间使用来增加“父”算法计数器。

相关内容