如何改变嵌入式伪算法中while循环的缩进?

如何改变嵌入式伪算法中while循环的缩进?

我正在编写一个算法,它有一些子算法,我想将它们都放在一个伪代码中。我该怎么做(管理缩进)?

只要我使用\State,我就可以使用\hspace{\algorithmicindent}并且它可以工作,但我无法更改 while 循环的缩进。我希望 while 与上一行的缩进完全相同。您可以在以下位置找到我的代码:

\begin{algorithm}
  \caption{my algorithm}
  \label{myalgorithm}
  \textbf{Input:} All paths\\
  \textbf{Output:} Best path.
  \begin{algorithmic}
    \State $CurrentSelectedPath \gets \{\}$
    \State\textbf{sub-algorithm}
    \Statex\hspace{\algorithmicindent} $h\gets \text{allPossible}$
    \While{condition}
      \State text
    \EndWhile
  \end{algorithmic}
\end{algorithm}

答案1

您需要为子算法定义适当的分组。方法如下:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algpseudocode,amsmath}

\algnewcommand{\algorithmicsubalgorithm}{\textbf{sub-algorithm}}
\algdef{SE}[SUBALG]{SubAlgorithm}{EndSubAlgorithm}{\algorithmicsubalgorithm}{\algorithmicend\ \algorithmicsubalgorithm}%
%\algtext*{EndSubAlgorithm}% If you want to avoid seeing "end sub-algorithm"

\algnewcommand{\algorithmicinput}{\textbf{Input:}}
\algnewcommand{\algorithmicoutput}{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

\begin{document}

\begin{algorithm}
  \caption{my algorithm}
  \textbf{Input:} All paths \\
  \textbf{Output:} Best path.
  \begin{algorithmic}
    \State $\text{CurrentSelectedPath} \gets \{\}$
    \State\textbf{sub-algorithm}
    \Statex \hspace{\algorithmicindent} $h \gets \text{allPossible}$
    \While{condition}
    \State text
    \EndWhile
  \end{algorithmic}
\end{algorithm}

\begin{algorithm}
  \caption{my algorithm}
  \begin{algorithmic}
    \Input All paths.
    \Output Best path.
    \State $\text{CurrentSelectedPath} \gets \{\}$
    \SubAlgorithm
      \State $h \gets \text{allPossible}$
      \While{condition}
        \State text
      \EndWhile
    \EndSubAlgorithm
  \end{algorithmic}
\end{algorithm}

\end{document}

新的块用\SubAlgorithm...表示\EndSubAlgorithm(可以通过取消注释相应的代码片段来避免/删除结束子句)。可以采用\SubAlgorithm一个参数,该参数可以用作子算法的描述符。

您会注意到,我还擅自清理了算法中的一些其他内容;这应该提供一些一致的外观,并且更容易更新。


下面是一个允许您命名\SubAlgorithm{<name>}...\EndSubAlgorithm部分的实现:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm,algpseudocode,amsmath}

\algnewcommand{\algorithmicsubalgorithm}{\textbf{sub-algorithm}}
\algdef{SE}[SUBALG]{SubAlgorithm}{EndSubAlgorithm}[1]{\algorithmicsubalgorithm\ \textsc{#1}}{\algorithmicend\ \algorithmicsubalgorithm}%
%\algtext*{EndSubAlgorithm}% If you want to avoid seeing "end sub-algorithm"

\algnewcommand{\algorithmicinput}{\textbf{Input:}}
\algnewcommand{\algorithmicoutput}{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

\begin{document}


\begin{algorithm}
  \caption{my algorithm}
  \begin{algorithmic}
    \Input All paths.
    \Output Best path.
    \State $\text{CurrentSelectedPath} \gets \{\}$
    \SubAlgorithm{First}
      \State $h \gets \text{allPossible}$
      \While{condition}
        \State text
      \EndWhile
    \EndSubAlgorithm
    \SubAlgorithm{Second}
      \State $h \gets \text{allPossible}$
      \While{condition}
        \State text
      \EndWhile
    \EndSubAlgorithm
  \end{algorithmic}
\end{algorithm}

\end{document}

相关内容