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}
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, 2015 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, 2015 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, 2015 at 15:45

3 Answers 3

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .