[Tex/LaTex] How to change indentation of while loop in embedded pseudo algorithm

algorithmicxindentation

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}

Best Answer

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}