# Why do these three changes fix this equation-numbering error?

When I use \equation...\endequation instead of ..., all equations have the same number (I know this isn't best practice for environments; I'm trying to understand the underlying behavior), as in the following example.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
The first equation is
\equation
\label{pyth} a^2 + b^2 = c^2.
\endequation
The second equation is
\equation
\label{quad} x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.
\endequation
Referenced in order: \eqref{pyth} and \eqref{quad}.
\end{document}


While debugging, I've found three ways to fix it, but I have no idea why they work.

1. If I append \usepackage[colorlinks]{hyperref} to the preamble, on my first compilation I get the error message ! Argument of \Hy@setref@link has an extra }, but when I compile again, there's no error, and the equations are numbered correctly:

2. If I comment out \usepackage{amsmath} (and replace \eqref with \ref), the equations are numbered correctly, even without hyperref.

3. Since \begin{...} and \end{...} build a group, as mentioned in this post, I tried providing my own group:

The first equation is
\begingroup
\equation
\label{pyth} a^2 + b^2 = c^2.
\endequation
\endgroup
The second equation is
% Only the first one needs to be in a group
\equation
\label{quad} x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.
\endequation


This also numbers the equations correctly, which was interesting, because I don't a priori know how equation numbering is related to groups.

Thus, my question is: what differences between \equation...\endequation and ... cause these to solve this problem? Why do these solutions work?

• The difference is that you should never use \equation and \endequation in the body of a document. Dec 30 '15 at 22:59
• @egreg: I realize that; this question initially arose when \equation and \endequation were part of macros that I had defined in the preamble. But I don't know why including hyperref or adding \begingroup...\endgroup changes anything, and would like to understand that. Dec 30 '15 at 23:03
• saying "not best practice" isn't really the right description, it is a syntax error. The ams environments are explictly written to collect environment bodies so if the \equation form produces anything at all it is by accident not design. Dec 30 '15 at 23:05

Simply said, you must never use \env and \endenv (where env stands for a defined environment) in the body of the document; see Command form of tabularx (compilation problems) for some examples.

In case amsmath is not loaded this appears to work, but you lose the important changes amsmath makes to the equation environment, fixing some weaknesses of the stock one.

Here are the kernel definitions:

> texdef -t latex equation endequation

\equation:
macro:->$$\refstepcounter {equation} \endequation: macro:->\eqno \hbox {\@eqnnum }$$\@ignoretrue


and here's what happens with amsmath:

> texdef -t latex -p amsmath equation endequation

\equation:
\long macro:->\incr@eqnum \mathdisplay@push \st@rredfalse \global \@eqnswtrue \mathdisplay {equation}

\endequation:
\long macro:->\endmathdisplay {equation}\mathdisplay@pop \ignorespacesafterend


Without the \begin and \end parts, you interfere with the \mathdisplay@push and \mathdisplay@pop functions that are vital for usage of labels, equation numbers and tags.

Another cause for failure is \incr@eqnum: its definition is

> texdef -t latex -p amsmath incr@eqnum

\incr@eqnum:
macro:->\refstepcounter {equation}\let \incr@eqnum \@empty


You see that, when the second \equation command appears, the meaning of \incr@eqnum is the same as \@empty: this is the reason why the equation number is not incremented. If, as expected, the macro is executed in a group, the definition of \incr@eqnum will be restored at group end. This should explain why in your example you get the same equation number.

When amsmath is loaded, hyperref does its changes under the assumption that local assignments are indeed local to the equation environment, which aren't if you don't use the format.

• Well, TIL about texdef; that's really useful; thanks so much! That, combined with the change in \incr@enum's behavior, is what I was confused about. Thanks! Dec 31 '15 at 5:57