[Tex/LaTex] Missing $ error in math mode

amsmathcaseserrorsmath-mode

I have the following LaTeX code (beginning from line 80):

\begin{definition}
$\hat{n}_{v}$ = \begin{cases}
$n_{v}$ & \text{ if } $v \in$ \text{connected component of size} $< \frac{2}{\epsilon}$
 \\
$\frac{2}{\epsilon}$ & \text{ otherwise }
\end{cases}

\end{definition}

However, when I want to compile this code, I get the following error:

./lect42.tex:80: Missing $ inserted.
<inserted text> 
                $
l.80 $\hat{n}_{v}$ = \begin{cases}

I looked at the code carefully, but there are no missing $'s. Why I get this error?

Best Answer

You have lots of unnecessary dollar symbols, and one missing from the end.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\hat{n}_{v} = \begin{cases}
n_{v} & \text{ if } v \in \text{connected component of size} < \frac{2}{\epsilon}
 \\
\frac{2}{\epsilon} & \text{ otherwise }
\end{cases}
$
\end{document}

As this is quite a large expression, I would be inclined to use \[ and \] instead of the dollar signs, to create a displayed equation, rather than an in-text equation. You could also use \displaystyle to enlarge the fractions.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\hat{n}_{v} = \begin{cases}
n_{v} & \text{ if } v \in \text{connected component of size} < \displaystyle\frac{2}{\epsilon} \\
\displaystyle\frac{2}{\epsilon} & \text{ otherwise }
\end{cases}
\]
\end{document}