2

Why does this code give an error? I suspect it's some quirk of environments that there's a simple solution for, but I have not been able to find a solution online. Probably I just don't know what search terms to use.

Code

Note that my real definition of eq has more in it; this is a minimal reproduction. Also note that replacing align with equation makes the code compile, but, of course, it then does not allow for using &.

\documentclass{article}

\usepackage{amsmath}

\newenvironment{eq}
  {\begin{align}}
  {\end{align}}

\begin{document}

\begin{eq}
x + 1 = 2
\end{eq}

\end{document}

Error

! LaTeX Error: \begin{align} on input line 11 ended by \end{eq}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.13 \end{eq}

I've also tried using

\documentclass{article}

\usepackage{amsmath}

\newenvironment{eq}
  {\begin{equation}\begin{aligned}}
  {\end{aligned}\end{equation}\ignorespacesafterend}

\begin{document}

Begin
\begin{eq}
x + 1 = 2
\end{eq}
End1 \\
End2

\end{document}

but I'm wondering why plain align doesn't work.

1
2

Try like this:

\documentclass{article}

\usepackage{amsmath}

\newenvironment{eq}
  {\align}
  {\endalign}

\begin{document}

\begin{eq}
x + 1 = 2
\end{eq}

\end{document}

Environments in the definition of a command or of an environment in most of the cases has to be defined like on my above way.

3
  • 1
    Why is it that it has to be defined that way? Other environment commands (such as \begin{equation}) seem to work fine in the definition. – Camelid Mar 30 at 0:31
  • Also, I realized that one of the differences between \begin{equation}\begin{aligned} and \begin{align}/\align is that the equation version will only use one equation number, whereas the align version will use one per line. – Camelid Mar 30 at 0:32
  • 1
    @Camelid ... When an environment starts with the \begin commend... LaTeX expacts an \end of the same command... The inner one... So, if your defined environment puts it's end there... LaTeX will possibly throw that error before expanding to the content of your \end ... even if this is the same... Using the start of the environment without \begin but with an equivalent command... The only environment that latex sees is your new defined environment... Something like this... Not exactly accurate... But you can think this way (The true is really close to this). – koleygr Mar 30 at 0:37

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.