Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

        An aditional step.
    }
}
\Return the result
\end{algorithm}

I'm using algorithm2e and amsmath.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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

        An aditional step.
    }
}
\end{algorithm}

\end{document}

enter image description here

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) ).

share|improve this answer
    
Lots learned here! Thank you very much for the prompt answer. –  Ramiro Feb 7 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}

                An additional step.
            }
        }
        \Return the result
        \end{algorithm}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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