[Tex/LaTex] `missing $ inserted` Error when not putting $ before and after array environment

amsmatharrayserrorsmath-mode

Considering this answer I added $ before and after the array, i. e. before \begin{array} and after \end{array} in the following code. Then the former missing $ inserted Error was gone.

\documentclass[a4paper,10pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{center}
    $\begin{array}{cc}
    z_{ij} = 1      &       \quad \text{if } i, j \in C    \\
    z_{ij} = 0      &       \quad \text{otherwise}
    \end{array}$
\end{center}
\end{document}

However, why is that necessary? I thought the array environment switches LaTex to math mode itself.

Best Answer

the array environment does not automatically switch into math mode; math needs to be applied explicitly.

a better approach than embedding it in a center environment is to put it into a math display environment; either equation* or \[ ... \] will suffice.

also, the \text{...} string in the first line would be better if it includes the in-line math expression at the end.

finally, the right-hand column of the array is better left-aligned.

making all these changes, here's a recommended alternative.

\[
 \begin{array}{cl}
  z_{ij} = 1      &       \quad \text{if $ i, j \in C $}  \\
  z_{ij} = 0      &       \quad \text{otherwise}
 \end{array}
\]

For completeness, here's @DavidCarlisle's suggestion:

\[
z_{ij} = 
\begin{cases}
1 & \text{if } i, j \in C   \\
0 & \text{otherwise}
\end{cases}
\]