16

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

3 Answers3

17

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}
Boris
  • 38,129
  • 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} – pluton Apr 03 '15 at 16:08
  • Ok: \newenvironment{subalign}[1]{ should be replaced by \newenvironment{subalign}[1][]{ and then it works. – pluton Apr 03 '15 at 16:16
  • 5
    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)? – LSpice Jun 06 '15 at 22:20
  • 1
    @LSpice I suppose \csname align*\endcsname and \csname endalign*\endcsname would do the trick – AndreasT Aug 06 '18 at 17:55
  • @AndreasT, I think that doesn't work because * is an argument, not part of the name. – LSpice Aug 06 '18 at 17:57
7

Define the environment using environ:

enter image description here

\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}
Werner
  • 603,163
4

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}
muzimuzhi Z
  • 26,474
  • 2
    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. – egreg Jun 10 '20 at 07:43