# Align an equation box found inside an algorithm

I'm writing an algorithm that has an \begin{equation} inside it. I want the whole equation to be aligned to the left, as any typical text block would be aligned if written inside the algorithm environment.

The LaTeX code I'm currently working on is this:

\begin{algorithm}
\caption{Algorithm}
\label{alabel}

\KwData{
Lots of stuff written here.
}

\For{$i = 1, \ldots, n$} {
\If{something} {
do this
}
\Else {
do that
}

$\epsilon =$ simple equation

\If{$\epsilon < 0$} {
\begin{equation} %align me to the left
\begin{matrix}
\text{Choose } \theta \in
\left\{
\begin{array}{l l}
\left[f_k(x_i), f_k(x_{i+1}) \right) \text{ if } i < m\\
\left[f_k(x_m), +\infty \right) \text{ if } i = m\\
\end{array}
\right.\\
\end{matrix}
\end{equation}

}
}
\Return the result
\end{algorithm}


I'm using algorithm2e and amsmath.

Don't use equation, but the standard in-line math mode $...$ (or $$...$$):

\documentclass{article}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}

\begin{algorithm}
\caption{Algorithm}
\label{alabel}

\KwData{Lots of stuff written here.}
\For{$i = 1, \ldots, n$} {
\If{something} {
do this
}
\Else {
do that
}
$\epsilon =$ simple equation
\If{$\epsilon < 0$} {
$\text{Choose } \theta \in \begin{cases} [f_k(x_i), f_k(x_{i+1}) ), & \text{if } i < m.\\ [f_k(x_m), +\infty ), & \text{if } i = m.\\ \end{cases}$

}
}
\end{algorithm}

\end{document} I made some other changes to the code:

1. You can use the cases environment from amsmath; this will give better spacing and simplifies the code.

2. I suppressed the \left, \right constructs; they were not doing anything in your code, since there was nothing that would force the delimiters to stretch. If you want to increase the size of the outer delimiters a little, you can use \bigl, \bigr instead (as in \bigl( ... \bigr) ).

• Lots learned here! Thank you very much for the prompt answer. Feb 7, 2014 at 16:50

You also can use flalign if you want the equation number. Btw, I simplified your code, using the casesenvironment:

        \begin{algorithm}
\caption{Algorithm}
\label{alabel}

\KwData{
Lots of stuff written here.
}

\For{$i = 1, \ldots, n$} {
\If{something} {
do this
}
\Else {
do that
}

$\epsilon =$ simple equation

\If{$\epsilon < 0$} {
\begin{flalign} %align me to the left
& \text{Choose } \theta \in
\smash[t]{\begin{cases}
[f_k(x_i), f_k(x_{i+1}) ) &  \text{ if } i < m\\[3pt]
[f_k(x_m), +\infty )  & \text{ if } i = m
\end{cases}} &  &
\end{flalign} 