[Tex/LaTex] Loop conditions in multiple lines using algorithmic package

algorithmsindentationline-breaking

I am working on a paper in double-column format, and I am using the algorithmic package to typeset my algorithms. One of my while loops' condition is a bit long hence doesn't fit into the column.

When I try to split the condition into several lines, it is not properly indented, as in \WHILE{this is the first line which barely fits into the column \\ this is the second line}.

I have also tried the following:

\WHILE{this is the first line which barely fits into the column \\
  \hskip\algorithmicindent this is the second line}

However, it indents the second line of the condition only as much as the body of the WHILE, it needs to be aligned with the first line of the loop condition. How can I achieve this?

MWE is as follows:

\documentclass[twocolumn]{article}

\usepackage{algorithmic}

\begin{document}

\begin{algorithmic}
  \WHILE{There is an element which satisfies \\
         \hskip\algorithmicindent properties which are long}
    \STATE{while-body}
  \ENDWHILE
\end{algorithmic}

\end{document}

Best Answer

The following MWE provides \pushcode[<num>] which indents your code <num> indents (\algorithmicindent) from the left margin. The default (since <num> is optional) is 1 indent. You could also "undent" your code by specifying a negative number (be careful that this might push code into the line numbers).

enter image description here

\documentclass{article}
\usepackage{algorithmic}% http://ctan.org/pkg/algorithmic
\newcommand{\pushcode}[1][1]{\hskip\dimexpr#1\algorithmicindent\relax}
\begin{document}
\begin{algorithmic}[1]
  \WHILE{this is the first line which barely fits into the column \\
    \pushcode[2] this is the second line}
    \STATE here is some content
  \ENDWHILE
  \STATE here is some more content
\end{algorithmic}
\end{document}​
Related Question