Indenting lines of code in algorithm
Define:
\algdef{SE}[SUBALG]{Indent}{EndIndent}{}{\algorithmicend\ }%
\algtext*{Indent}
\algtext*{EndIndent}
Then in the algorithmic block, write:
\begin{algorithmic}[1]
\State Outside indent block
\Indent
\State Inside indent block
\EndIndent
\end{algorithmic}
In the following example code I defined two new commands allowing you to change the indentation; simply enclose the desired fragment using \bindent
, \eindent
; the length \myindent
controls the indent amount:
\documentclass{article}
\usepackage{algorithm,algorithmic}
\usepackage{caption}
\newlength\myindent
\setlength\myindent{2em}
\newcommand\bindent{%
\begingroup
\setlength{\itemindent}{\myindent}
\addtolength{\algorithmicindent}{\myindent}
}
\newcommand\eindent{\endgroup}
\begin{document}
\begin{algorithm}[H]
\caption*{my algorithm}
\begin{algorithmic}
\STATE \textbf{Stage one:} this is stage one
\bindent
\FORALL{i}
\STATE do something
\ENDFOR
\eindent
\STATE \textbf{Stage two:} this is stage two
\bindent
\STATE Update the trie:
\FORALL{j}
\STATE do something
\ENDFOR
\eindent
\end{algorithmic}
\end{algorithm}
\end{document}
Some comments to the code:
\newlength\myindent % define a new length \myindent
\setlength\myindent{6em} % assign the length 2em to \myindet
\newcommand\bindent{%
\begingroup % starts a group (to keep changes local)
\setlength{\itemindent}{\myindent} % set itemindent (algorithmic internally uses a list) to the value of \mylength
\addtolength{\algorithmicindent}{\myindent} % adds \mylength to the default indentation used by algorithmic
}
\newcommand\eindent{\endgroup} % closes a group
Problem of Gonzalo's answer is with numbering, as numbers are also indented.
Found better and also simpler solution for \algorithmic
, the inbuild environment ALC@g
\begin{ALC@g}
% Indent what you need
\end{ALC@g}
To compare it in same document
\newlength\myindent
\setlength\myindent{2em}
\newcommand\bindent{%
\begingroup
\setlength{\itemindent}{\myindent}
\addtolength{\algorithmicindent}{\myindent}
}
\newcommand\eindent{\endgroup}
\begin{algorithmic}[1]
\STATE \textbf{Gonsalo's answer}
\bindent
\STATE First
\STATE Second
\eindent
\STATE \textbf{Proposed answer}
\begin{ALC@g}
\STATE First
\STATE Second
\end{ALC@g}
\STATE Something else
\end{algorithmic}
And result
Of course you can include solution for bigger indent with \addtolength
and \setlength
inside my proposal.