[Tex/LaTex] Align equals inside case statement

aligncases

I have the following code:

\begin{equation}
\frac {\partial C}{\partial {z_k}}= \begin{cases}
  - \frac {1} {y_i} {y_i} (1-{y_i}) = y_{i} - 1 & k = i \\
   \frac {1} {y_i} {y_{i}^{2}} = y_{i}  &  k \ne i  \\
\end{cases}
\end{equation}

It works fine, but what I want to achieve is to align the = from the first case and the second case. I did not manage to do that.

Any advice?

Thank you!

Best Answer

I can be done, using aligned instead, but I am not sure which exact alignment is desirable. Probably the best is to use a second cases for the simplification.

Sample output

\documentclass{article}

\usepackage{mathtools}

\begin{document}

\begin{equation}
  \frac {\partial C}{\partial {z_k}}
  =
  \left\{
  \begin{aligned}
    - \frac {1} {y_i} {y_i} (1-{y_i})&= y_{i} - 1, &\qquad& k = i, \\
    \frac {1} {y_i} {y_{i}^{2}}&= y_{i},  &&  k \ne i.
  \end{aligned}
  \right.
\end{equation}

\begin{equation}
  \begin{split}
    \frac {\partial C}{\partial {z_k}} &=
    \begin{dcases}
      - \frac {1} {y_i} {y_i} (1-{y_i}),& k = i, \\
      \frac {1} {y_i} {y_{i}^{2}}, & k \ne i,
    \end{dcases}\\
    &=
    \begin{cases}
      y_{i} - 1, &k = i, \\
      y_{i}, & k \ne i.
    \end{cases}
  \end{split}
\end{equation}

\end{document}