[Tex/LaTex] newenvironment: error with \begin{align} and \end{align}

alignenvironments

In the example below, an error is caused by \begin{align} and \end{align} but I do not see why:

\documentclass{book}
\usepackage{amsmath}
\newenvironment{subalign}{\begin{subequations}\begin{align}}{\end{align}\end{subequations}}
\begin{document}
\begin{subalign} 
    a&=b\\
    &=b
\end{subalign}
\end{document}

Best Answer

This is caused by the way LaTeX environments are defined. The simplest way around is to use \env...\endenv construction instead of \begin{env}...\end{env}:

\documentclass{book}
\usepackage{amsmath}
\newenvironment{subalign}{\subequations\align}{\endalign\endsubequations}
\begin{document}
\begin{subalign} 
    a&=b\\
    &=b
\end{subalign}
\end{document}