26

I am writting algorithm which has two stages. How can I indent code for stage one and for stage two?

\begin{algorithm}[H]
\caption*{my algorithm}
\begin{algorithmic}
    \STATE \textbf{Stage one:} this is stage one
    \FORALL{i}
        \STATE do something
    \ENDFOR
    \STATE \textbf{Stage two:} this is stage two
    \STATE Update the trie: 
    \FORALL{j}
    \STATE do something
    \ENDFOR
\end{algorithmic}
\end{algorithm}
Werner
  • 603,163
ashim
  • 1,783

5 Answers5

27

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}
mahdiz
  • 387
17

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}

enter image description here

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
Gonzalo Medina
  • 505,128
13

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

Test

Of course you can include solution for bigger indent with \addtolength and \setlength inside my proposal.

Stefan Pinnow
  • 29,535
logocar3
  • 131
  • 2
    Your answer is the best working one. Good, it avoids generating line number indentations. BTW, how can I set the indentation spaces? In your answer, it indents only one space. – GoingMyWay Jan 14 '19 at 02:56
11

If you want to be flexible with the indentation size you can also quickly use \hskip1.0em:

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{caption}

\begin{document}

\begin{algorithm}[H]
    \caption{Foos}
    \begin{algorithmic}[1]
        \State foo
        \State \hskip0.5em foo
        \State \hskip1.0em foo
        \State \hskip1.5em foo
    \end{algorithmic}
\end{algorithm}

\end{document}

Result

Finn
  • 233
3

If you use the algorithm2e package, the answer is \Indp and \Indm. The first creates indent and the second creates negative indent, thus removes the previously created one. See this answer for more details.

SiGa
  • 369