tags:

views:

345

answers:

2

How is it possible to indent lines in an algorithm (algorithmic) in latex?

I would like to be able to write the following:

\begin{algorithm}[H]
\caption{My Awesome Program} \label{awesome-algorithm}
\begin{algorithmic}[1]
\FOR { $i=0$ to $logn$ } 
    \STATE Step A:
        % would like the indent the next lines...
        \STATE do something
        \STATE do another thing
    \STATE Step B
\ENDFOR
\end{algorithmic}
\end{algorithm}

How is it possible to indent those lines? I've been trying to find the answer by googling without success. I hope you guys can help me. Thanks.


I'm currently using the following for indentation:

          \STATE  \ \ \ \ do something

which seems plain wrong. But works.

+2  A: 

Try this instead:

\STATE\hspace{\algorithmicindent} do something
\STATE\hspace{\algorithmicindent} do another thing

It should work better because it uses the current indent value to indent.

Edit: Using Charles's suggestion, you could define a new command, \INDSTATE:

\newcommand{\INDSTATE}[1][1]{\STATE\hspace{#1\algorithmicindent}}

and then use that when you want indentation. By default, \INDSTATE indents by one level, but you can change it:

\INDSTATE do something % What you want
\INDSTATE[2] do something % Indent by twice the amount
Alok
I was about to suggest this... Defining an INDSTATE macro seems worthwhile.
Charles Stewart
Thanks Charles, I added INDSTATE command in my post.
Alok
This is great. Thanks!
Anna
+1  A: 

I would suggest you don't indent that way. The package is designed to format pseudocode in a standard way to make it easier for your readers.

You are trying to break that standard. It would be much better for to try to rewrite the code to conform to what readers expect.

Why are you labeling step A and B, anyway? Each statement is supposed to be its own step. It seems like you are adding extra structure that actually makes the code less readable. Can you get the same effect with comments? How about combining "do something" and "do another thing" into one statement?

UncleO
I cannot combine the steps. I tried using the comment. It is ok, but less clear that what I'd like to.
Anna
This looks like Anna is grouping these steps so that she can talk about them in the body of the text.
Charles Stewart
Thanks for your comment anyway, I did learn something from it.
Anna