1

Following this resource, I tried creating an environment like the following:

\newenvironment{splitEq}{
    \begin{equation}
    \begin{split}
}
{
    \end{split}
    \end{equation}
}

And using it like this:

\begin{enumerate}[label=(\alph*)]
    \item 
    \begin{splitEq} % Line 209
        a + b & = c \\ %example
        & = abc
    \end{splitEq}
\end{enumerate}

I get this error:

\begin{split} on input line 209 ended by \end{splitEq}.

It's my first time using LaTeX so I don't know if I'm misunderstanding something basic.

Thank you

2
  • 3
    you can not use ams alignments in newenvironment in that way, they need to grab the whole body so need to "see" the end enviroment not have it hidden in a user defined environment. (It is possible to work round that, but best advice is not to do it) Apr 10, 2022 at 10:02
  • I don't think that there is a real gain in doing so. Adding split when necessary is quite simple, more than remembering the new environment name.
    – egreg
    Apr 10, 2022 at 19:59

1 Answer 1

2

As @DavidCarlisle said, you can't use the amsmath environments straightforwardly inside \newenvironment. But you can grab the entire environment body of your environment and use that to call the amsmath environments in a way they see each other. Using \NewDocumentEnvironment this is pretty easy:

\documentclass{article}

\usepackage{enumitem}
\usepackage{amsmath}

\NewDocumentEnvironment{splitEq}{b}
  {%
    \begin{equation}%
      \begin{split}%
        #1%
      \end{split}%
    \end{equation}%
  \ignorespacesafterend}
  {}

\begin{document}
\begin{enumerate}[label=(\alph*)]
    \item 
    \begin{splitEq} % Line 209
        a + b & = c \\ %example
        & = abc
    \end{splitEq}
\end{enumerate}
\end{document}
2
  • This works perfectly, however @DavidCharlisle mentioned that it was possible to work a round it but didn't recommended it, could this cause any issues in the future? Should I just be using a snippet in my editor instead? My goal was just to avoid writing both over and over again. Apr 10, 2022 at 10:39
  • 5
    @lovelysarah actually it needed an extra \ignorespacesafterend (see update) otherwise you'd tend to get a spurious space in following text, but otherwise it's safe enough. But you said you were a beginner. In general I would aim to write the first few documents using very few preamble definitions so that the document is using documented standard latex syntax, that makes it much easier to find help when things go wrong. Apr 10, 2022 at 11:20

You must log in to answer this question.

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