3

I am writing an algorithm, which has some sub-algorithms, and I want to have all in one pesudocode. How can I do this (manage the indention)?

As long as I am using \State, I can use \hspace{\algorithmicindent} and it works, but I can not change the indention of while loop. I want while to be exactly at the same indent as the previous line. You can find my code in the following:

\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 Answer 1

3

You need to define an appropriate grouping for your sub-algorithm. Here's the way:

enter image description here

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

The new block is denoted by \SubAlgorithm...\EndSubAlgorithm (the end-clause can be avoided/removed by uncommenting the appropriate piece of code). It would be possible to make \SubAlgorithm take an argument that could be used as the descriptor for the sub-algorithm, say.

You'll notice that I also took the liberty of cleaning up some other content in your algorithm; this should provide some consistent look-and-feel, and also be easier to update.


Here is an implementation that allows you to name the \SubAlgorithm{<name>}...\EndSubAlgorithm section:

enter image description here

\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}
3
  • 1
    @izi: No. You can use as many \SubAlgorithm...\EndSubAlgorithm constructions in your algorithmic environment as you wish. It would be possible to adapt the definition of \SubAlgorithm{<arg>} to take an argument <arg> so you could perhaps do \SubAlgorithm{First}...\EndSubAlgorithm ... \SubAlgorithm{Second}...\EndSubAlgorithm ... in the same algorithmic environment. Is this what you're after?
    – Werner
    Jan 23, 2015 at 20:27
  • yes, I want to know how can I define an argument for \SubAlgorithm as its name, so that I can use it for different sub algorithms?
    – Aazzz
    Jan 23, 2015 at 20:34
  • 1
    @izi: I've added an argument to \SubAlgorithm{<name>} allowing you to name the sub-algorithm. The formatting is set to \textsc{<name>}, but you can change that.
    – Werner
    Jan 23, 2015 at 20:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .