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


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.

    z_{ij} = 1      &       \quad \text{if } i, j \in C    \\
    z_{ij} = 0      &       \quad \text{otherwise}

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.

  z_{ij} = 1      &       \quad \text{if $ i, j \in C $}  \\
  z_{ij} = 0      &       \quad \text{otherwise}

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

z_{ij} = 
1 & \text{if } i, j \in C   \\
0 & \text{otherwise}