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:
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: