# Multiline statements and conditions in algorithm environment

I have an algorithm that I want to write in mathematical pseudo-code. However, some conditions and statements are very long, e.g. "if (a series of relations and constraints hold) do" or "X gets (a series with several elements)". These statements does not fit nicely into a single line. I don't want to break them into temporary variables just to meet the width limit.

What I would have done in a real program code, is something like:

if (
long condition 1 and
long condition 2 ...
) {...}


Or

X <- {
long element 1,
long element 2...
}


How can I achieve this effect in some LaTeX algorithmic environment? I am currently using algorithm2e but it's not a must.

Regular line-breaks and associated indentation is discussed in Line-break in package algorithm2e. For conditional adjustments, perhaps the following might be of interest:

\documentclass{article}
\usepackage{algorithm2e}
\SetKwIF{If}{ElseIf}{Else}{if~(\endgraf}{\endgraf)~then}{else if}{else}{end if}%
\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
\eIf{\begin{tabular}{@{\hspace*{1.5em}}l@{}}
understand {\normalfont and} \\
understand some more
\end{tabular}}{
go to next section\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}
\end{document}​


I have adjusted the formatting of \eIf - only for the conditional part - to include (..). You'll have to do the same for other structures/conditionals.

This is all much easier if you use the algoritmicx package, it has a lot more functionality and works better with other packages:

\documentclass[12pt]{report}
\usepackage[a4paper,margin=1.25in]{geometry}

\usepackage{algorithmicx}
%Defines the \If command, ending in \EndIf
\algblockdefx[If]{If}{EndIf}[0]{\textbf{if} $($}{$)$}
%Defines the alternative ending \Then to the \If command,
%which in turn ends in \EndThen
\algcblockdefx[Then]{If}{Then}{EndThen}{$)$ $\{$}{$\}$}
%Provides the alternative ending \Else to the \Then command,
%which in turn ends in \EndElse
\algcblockdefx[Else]{Then}{Else}{EndElse}{$\}$ \textbf{else} $\{$}{$\}$}

\begin{document}

Here is an example:

\begin{algorithmic}[1]
\State Here We Begin
\If
\State this is true
\State Or this is true
\Then
\State Then we do that
\EndThen
\Statex
\If
\State this is true
\State Or this is true
\Then
\State Then we do that
\Else
\State Or do this
\EndElse

\end{algorithmic}

\end{document}


This gives the output:

The package also provides algpseudocode, which has a lot of useful blocks predefined.