15

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}
3
  • 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).
    – LSpice
    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.
    – pluton
    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.
    – LSpice
    Jun 7 '15 at 15:45
16

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

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}
2
  • 1
    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 7:43
  • @egreg Thank you! Jun 10 '20 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.