51

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.

3
  • 4
    Try delimiting the error; it's probably due to something that comes before that cases environment. You should be putting & in front of \text, but this isn't related to the error.
    – egreg
    Commented Aug 18, 2012 at 14:45
  • 3
    Right now I realized that removing the blankline between \end{cases} and \end{equation*} fixes this issue. But I'm still interested in why something like this happens. I'll do some tests when I've got time, maybe this error is caused by the defn environment I'm using(defined using \newtheorem).
    – Bakuriu
    Commented Aug 18, 2012 at 14:52
  • @Bakuriu, you are aright. I got the same error with the blank line. Maybe this line is interpreted as a new line for equation and should be non empty.
    – Sigur
    Commented Aug 18, 2012 at 15:19

2 Answers 2

65

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}
4
  • @GonzaloMedina, I guess that the author edited the code in his question.
    – Sigur
    Commented Aug 18, 2012 at 15:28
  • Thank you for your reply. I did not think that leaving blank lines in math mode would cause such troubles. I thought that they'd simply be ignored.
    – Bakuriu
    Commented Aug 19, 2012 at 11:49
  • Missing a \ in 1 &\text{se $\omega\in A$}\ at the very end.
    – khatchad
    Commented Mar 17, 2017 at 22:07
  • Thank you for introducing me to the mathtools package, its dcases environment has helped me properly format some big equations. Commented Aug 15, 2018 at 13:36
2

I've found that \begin{cases} wants to be wrapped in $ to not generate an error. I am just learning LaTeX, but when I make sure to being and \end{cases} within a math environment, my error goes away.

4
  • 6
    In the original posting, the cases environment was contained in an equation* environment, which is provided by the amsmath package and automatically initiates (and terminates) displaymath. displaymath material is typeset centered on a separate line (or lines) by itself. The issue with the OP's code was not a missing switch to a math environment but, rather, the fact that the amsmath package doesn't permit blank lines in a displaymath setting. In contrast, you seem to be proposing a switch to an inline math environment, which will not display its contents centered.
    – Mico
    Commented Sep 26, 2013 at 17:12
  • \begin{equation} \label{c5:eqn5} fit_{new} = $\begin{cases} \frac{1}{1 + f_{new}} & \text{if f_{new} \geq 0}\\ 1 + |f_{new}| & \text{if f_{new} \leq 0}\\ \end{cases}$ \end{equation} Commented Nov 28, 2020 at 18:43
  • my \text{if f_{new} \leq 0} should be written as text{if} f_{new} \leq 0 Commented Nov 28, 2020 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .