# Numbering many equations automatically plus allowing to add new equations

I'm facing a problem with my document that has many equations. The problem with the labelling issue if I decide to modify or add new equations. Basically so far, I'm using \label{eq4.1} where 4 for the chapter four and 1 for the first equation. With this approach, if I decide later to add new equations before eq4.1, then I have to change all subsequent equations to reflect this change. I'm looking for a better and flexible approach to cope with this problem.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
x &= 2^{100}, \label{eq4.1} \\
y &= 2^{100}, \label{eq4.2} \\
z &= 2^{100}. \label{eq4.3}
\end{align}

These are \eqref{eq4.1} and \eqref{eq4.2} and \eqref{eq4.3}.

\end{document}

• Well your labelling scheme defeats the object of automatic numbering somewhat. I try to use labels that describe the equation, so in your case I would use \label{def:x} (meaning definition of x), etc. Nov 17, 2015 at 17:50
• @ChristianHupfer, not all of them but there are many equations need to be labelled. I'm in a phase that my supervisor requires me to add new equations. I could overcome this problem by explicitly labeling the equations but I'm asking if there is a better approach. Nov 17, 2015 at 18:04
• the problem is not with the actual numbering, but with how you are assigning the labels. it's probably not too likely that an equation will move to a different chapter, so including the chapter number as part of the label is probably safe. but something more descriptive is desirable. how about something like \label{eq:4-def-x} to borrow an idea from another comment? Nov 17, 2015 at 18:11
• You're trying to do manually what LaTeX does automatically. The purpose of \label is to assign a name completely independent from the number which will be eventually assigned to the equation. So you need to change neither the label nor the reference at any time, for as many equations you add (or remove). Nov 17, 2015 at 20:50

The purpose of the \label\ref mechanism is to free the user from the burden of renaming cross references as soon as some of the numbers change.

Your projected scheme is completely equivalent to not adding any \label command and doing cross references just by the assigned number. Yes, you have the benefit of the \ref command, but your code would be completely equivalent with

\DeclareRobustCommand{\eqref}[1]{\eqrefaux#1\eqrefaux}
\def\eqrefaux eq#1\eqrefaux{\textup{(#1)}}


in the preamble and typing

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
x &= 2^{100}, \\
y &= 2^{100}, \\
z &= 2^{100}.
\end{align}

These are \eqref{eq4.1} and \eqref{eq4.2} and \eqref{eq4.3}.

\end{document}


The macros just remove the eq string. Note that this wouldn't need two passes, because it doesn't exploit \label. But it's wrong, deadly wrong.

If you add an equation somewhere, you need to keep track of where it was and to find all cross references with a higher number, start from the one with the largest number, adding one to it and go down step by step. A nightmare! And a sure source of errors: suppose your chapters are stored in different files: you need to check each one of them and fix the cross references.

And, maybe, your supervisor will email you, a week later, “Dear CroCo, that equation you added last week should be removed”. Did you think to this? By Murphy's law, this will happen with utmost certainty.

There is already a flexible mechanism: using \label and \ref properly!

\documentclass{article}
\usepackage{amsmath}
\usepackage{showkeys} % remove for final versions

\begin{document}

\begin{align}
x &= 2^{100}, \label{eq-x100}\\
y &= 2^{100}, \label{eq-y100}\\
z &= 2^{100}. \label{eq-z100}
\end{align}

These are \eqref{eq-x100} and \eqref{eq-y100} and \eqref{eq-z100}.

\end{document}


You won't have to worry about adding or removing equations or other numbered objects, because LaTeX will take care of this. Even warning you if you have cross-referenced an equation (or chapter, section, whatever) you have meanwhile removed.

With showkeys you have the labels shown in the PDF, so you can easily skim through it to find the exact name when you want to do \ref. If the names are good enough, you'll probably remember them.

• Where were you @egreg when I did start writing my document? :)). The showkeys is the cure for the labeling cancer. I have more than a hundred of equations, I always worry about remembering the labels of the equations, this is why I've chosen eq4.1. With showkeys , I will not to worry about memorizing the labels. THANK YOU SO MUCH. Nov 17, 2015 at 22:31
• @CroCo what do you thank him for:-) I wrote the package:-) Nov 17, 2015 at 22:46
• @DavidCarlisle, for guiding me to this package. I wasn't aware of this package. You deserve the BIG THANKS. Nov 17, 2015 at 22:47
• @CroCo You can also try showlabels that has some more options. Nov 17, 2015 at 22:48
• @CroCo, you can search for the labels in the LaTeX source, using your editor... Nov 18, 2015 at 1:32

In general the labelling of the equations is based on their description rather than the order in which they appear. In a technical writing it is very common to add/delete the equations, figures, and tables. A general scheme followed in my lab is:

1. Use fg for figure, tb for table, and eq for equations. For example, in your case you might want to use \label{eq:defx}.

2. If there are several equations and you wish to separate them out as per their sections, for convenient recall of the label names, you might want to use something like \label{eq:bbt:defx} for an equation which defines x under a section say Big Bang Theory.

3. When there is a set of equations which can be referred as a single equation (similar to your example), you might want to use \nonumber to suppress the equation number.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
x &= 2^{100}, \nonumber \\
y &= 2^{100}, \nonumber \\
z &= 2^{100}. \label{eq:defxyz}
\end{align}

These are defined in Eqs.~\ref{eq:defxyz}.
\end{document}


Edit: This does not answer the question but is a suggestion. It might take time for you to re-label all the equations, but in the long-run you would probably be saving lot of time---considering that you are already into adding/deleting equations.

• Shouldn't it be \nonumber? Nov 17, 2015 at 20:47
• Oh Yes! My bad! Nov 17, 2015 at 20:50

I show a way to store the equations first in a expl3 seq - variable and write the equations with labels to a separate file. Adding new equations before or after writing or interchanging the order is easy using \AddEquation.

However, the file is rewritten each time, so design has to be revised ;-)

\documentclass{article}
\usepackage{amsmath}

\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l__croco_equation_seq
\iow_new:N \l__croco_equation_file

\seq_put_right:Nn \l__croco_equation_seq {#1}
}

\NewDocumentCommand{\WriteEquations}{O{eq4}}{%
\int_zero:N \l_tmpa_int
\iow_open:Nn \l__croco_equation_file {\jobname.equ}
\iow_now:Nn \l__croco_equation_file {\begin{align}}
\seq_map_inline:Nn \l__croco_equation_seq {%
\int_incr:N \l_tmpa_int
\iow_now:Nx \l__croco_equation_file {##1\space \string\label{#1.\int_use:N \l_tmpa_int} \string\\}
}
\iow_now:Nn \l__croco_equation_file {\end{align}}
\iow_close:N \l__croco_equation_file
\seq_gclear:N \l__croco_equation_seq
}

\ExplSyntaxOff

\begin{document}

\WriteEquations

\InputIfFileExists{\jobname.equ}{}{}

\WriteEquations[eqother]

\InputIfFileExists{\jobname.equ}{}{}

These are \eqref{eq4.1} and \eqref{eq4.2} and \eqref{eq4.3}.

and those are are \eqref{eqother.1} and \eqref{eqother.2} and \eqref{eqother.3}.

\end{document}