# newenvironment: error with \begin{align} and \end{align}

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}

• Although @Boris's answer tex.stackexchange.com/a/236666/1169 works, note that this is not a general problem with LaTeX environments, but with align in particular (see tex.stackexchange.com/questions/112558/…). For example, if you replaced align by equation, then your code would work (or, rather, complain about misplaced alignment characters). Jun 6 '15 at 22:14
• @LSpice yes, I noticed what you describe but I do not know why the align environment behaves this way. Jun 7 '15 at 5:44
• @MatthewLeingang's answer tex.stackexchange.com/a/112565/1169 (referring to comments of David Carlisle and Ulrike Fischer) quotes the AMS documentation to explain why. Jun 7 '15 at 15:45

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}

• Thank you: and would you know how to add an optional label at the subequations level? Something like (but it does not work): \newenvironment{subalign}[1]{\subequations\label{#1}\align}{\endalign\endsubequations} Apr 3 '15 at 16:08
• Ok: \newenvironment{subalign}[1]{ should be replaced by \newenvironment{subalign}[1][]{ and then it works. Apr 3 '15 at 16:16
• How can this be adapted to environments with *s in their names? For example, if I wanted to wrap align* (obviously not with this example, because it makes no sense with subequations)? Jun 6 '15 at 22:20
• @LSpice I suppose \csname align*\endcsname and \csname endalign*\endcsname would do the trick Aug 6 '18 at 17:55
• @AndreasT, I think that doesn't work because * is an argument, not part of the name. Aug 6 '18 at 17:57

Define the environment using environ:

\documentclass{article}

\usepackage{amsmath,environ}

\NewEnviron{subalign}{%
\begin{subequations}
\begin{align}
\BODY
\end{align}
\end{subequations}
}

\begin{document}

\begin{subalign}
a &= b \\
&= b
\end{subalign}

\end{document}


To provide different approaches under same question, this is a xparse solution, using the b-type argument specifier.

\documentclass{article}

\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentEnvironment{subalign}{b}{%
\begin{subequations}
\begin{align}
#1
\end{align}
\end{subequations}
}{}

\begin{document}

\begin{subalign}
a &= b \\
&= b
\end{subalign}

\end{document}

• You were missing the end part in the definition (a mistake that I make myself, because of old habits with environ). The error went unnoticed because you had an empty line after the definition, but this is taken as \par in the end part. Jun 10 '20 at 7:43
• @egreg Thank you! Jun 10 '20 at 7:47