14

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

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
  • this is perfect, just curious what does this line \newlength\myindent mean? – ashim Sep 1 '12 at 21:48
  • could you add some comment explaining what does each line do? thank you – ashim Sep 1 '12 at 21:50
  • @capoluca sure! I've updated my answer adding some comments. – Gonzalo Medina Sep 1 '12 at 21:55
  • 5
    I'm getting an error with your approach: ! You can't use 'the character 1' after \advance. \ALG@cmd@2@algorithmicindent ->1 .5em l.21 \bindent But @mahdiz solution worked – Ciprian Tomoiagă Apr 28 '16 at 2:23
  • Thank you, it indeed generates indentation, but the line number of code also indents. How can I fix it? This answer can help: tex.stackexchange.com/a/333962/83288 – GoingMyWay Jan 14 at 2:51
14

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}
  • 1
    I want this to work but the definition throws an error. – 6005 Jun 11 '18 at 5:42
7

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.

  • 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 at 2:56
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.