[Tex/LaTex] cases environment inside an array

arrayscaseserrorslyx

LyX is giving a lot of error messages for my Latex code which runs in other places.

\[
\begin{array}{rl}
p_{Y \mid H_1}(y \mid H_1) & = f_X(g^{-1}(y))\left| \dfrac{dg^{-1}(y)}{dy} \right| \\ \\
& = \begin{cases} y>0 & : & f_X(\ln(y))\left| \dfrac{d \ln(y)}{dy} \right| = \dfrac{f_X(\ln(y))}{y} \\
\text{otherwise} & : & 0
\end{cases}
\end{array}
\]

LyX says:

Extra alignment tabs changed to \cr.
 & = \begin{cases} y>0 & : &
                                  f_X(\ln(y))\left| \dfrac{d \ln(y)}{dy} \ri...
You have given more \span or & marks than there were
in the preamble to the \halign or \valign now in progress.
So I'll assume that you meant to type \cr instead.

However the exact code works perfectly in the SE Mathematics domain to give this output:

should have been like this

What does LyX not like in my code?

Best Answer

Here's a solution which (a) uses the align* environment instead of the array environment and (b) inverts the order of the arguments inside the cases environment (as I believe that this may be closer to general usage of this environment). It also defines a macro called \abs to simplify typesetting in the body of the example.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\newcommand{\abs}[1]{\left|#1\right|}
\begin{document}
\begin{align*}
p_{Y \mid H_1}(y \mid H_1) 
&= f_X(g^{-1}(y)) \abs{ \dfrac{dg^{-1}(y)}{dy} } \\ 
&= \begin{cases}
      f_X(\ln(y)) \abs{ \dfrac{d \ln(y)}{dy} } = \dfrac{f_X(\ln(y))}{y} & \text{if $y>0$}\\
      0 & \text{otherwise}
   \end{cases}
\end{align*}
\end{document}

If you want to state the condition part before the result part in the cases environment, you could write it as:

   \begin{cases}
      \text{if $y>0$} &:\  
        f_X(\ln(y)) \abs{ \dfrac{d \ln(y)}{dy} } = \dfrac{f_X(\ln(y))}{y}\\
      \text{otherwise} &:\ 0  
   \end{cases}

leading to the following result:

enter image description here