[Tex/LaTex] Using “cases” inside “equation” gives error[with “amsmath” loaded]

casesequations

I'm trying to use the cases environment inside an equation environment.
The sample code is quite simple:

\begin{equation*}
        X(\omega) = \begin{cases}
                        1 \text{se $\omega \in A$} \\
                        0 \text{se $\omega \in A^c$}
                    \end{cases}

\end{equation*}

Compiling it with Kile results in this error:

Missing $ inserted

and some other messages about ending delimiters not present.

I have \usepackage{amsmath} at the beginning of my document.

Also I must tell you that I have an other piece of code, which is identical, and which works fine:

 \begin{equation*}
  B_i = \begin{cases}
        A_i^c \text{se $i \in I$,}
        \\
        A_i \text{se $i \in I \smallsetminus I'$}.
        \end{cases}
 \end{equation*}

Also this piece gave me some errors the other day, then I changed the \begin{equation*} ... \end{equation*} to $$ and $$ and it worked.
Replace $$ with the equation environment and the error was magically gone.

I've already tried doing this with that piece of code, but nothing changes.

Best Answer

In the displaymath mode, you can not use any paragraph triggering commands such as blank lines or \par commands. (Not a good idea, but you can use it inside the cases environment). Also you don't need to switch to text mode and then again to math mode. You can just use text on the text and leave the rest as it is.

Another point is the use of & characters which is the column delimiter that should be used inside the cases environment. This would be apparent if one of the cases starts with 1250 and the other is 1 which would lead to a bad alignment (try without the & characters!).

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
X(\omega) = \begin{cases}
1 &\text{se $\omega\in A$}\\
1250 &\text{se $\omega \in A^c$}
\end{cases}
\end{equation*}

\end{document}

enter image description here

Also mathtoolspackage provides some nice extensions and bugfixes of amsmath, so here is an example of dcases* environment which automatically switches to text mode in the second entry of the case declaration:

\documentclass{article}
\usepackage{mathtools}
\begin{document}
\[
X(\omega) = \begin{dcases*}
1 & se $\omega\in A$\\
0 & se $\omega \in A^c$
\end{dcases*}
\]
\end{document}

enter image description here

If you are using the standalone class (which gives errors) then you have to use it as

\documentclass[preview]{standalone}
Related Question