[Tex/LaTex] Writing conditional math equation in latex

equationsmath-modetext;

Hi, I want to write this equation in latex. I've write the following lines but there is no space between "j" and "and". Please tell me how to fix it. Thanks.

  \begin{align}
    Y_{ij} = \left\{\begin{array}{lr}
        \sum_{k \sim i}y_{ij}, & \text{if } i = j,\\
        {-y_{ij}}, & \text{if } i \ne j   \text{and } i \sim j,\\
        0, & \text{otherwise.}
        \end{array}
  \end{align}

Best Answer

You can have a simpler typing with the dcases* environment from mathtools: The d in the name of the environment means it will typed in \displaystyle and the star means the second column will be in text mode:

\usepackage{mathtools}
...........
 \begin{align}
    Y_{ij} = \begin{dcases*}
        \sum_{k \sim i}y_{ij}, & if $ i = j $,\\
        {-y_{ij}}, & if $ i \ne j $ and $ i \sim j $,\\
        0, & otherwise. 
        \end{dcases*}
  \end{align}

enter image description here