# Equation autolabelling

I am just wondering if there is a way to refer to autolabelled equations in the text.

I can use:

$$x^2$$


The equation will autonumber, but I can't refer to it in the text without providing a label, and I don't want to do that because I might be added other equations before it in the future (plus I'd still have to update the text).

Apologies if this has been asked. I have tried to search for it with no luck. Maybe I am using the wrong search terms.

• Welcome to TeX.SE! See the posting Cross-reference packages: which to use, which conflict? for an overview and discussion of LaTeX packages that simplify and/or extend cross-referencing to various objects in a document, including equations. – Mico Mar 8 '18 at 18:12
• Thanks for the edit Phelype Oleinik. I made the code bits code but it didn't look right for some reason. – cohara Mar 9 '18 at 14:10

You don't have to number the equations by yourself. Let LaTeX do the heavy lifting. You can just give names to the equations and refer to them by their names:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Equation~\ref{xsquared} shows that the universe is
infinite, but equation~\ref{ysquared} says otherwise.
$$\label{xsquared} a = x^2$$
$$\label{ysquared} b = y^2$$
\end{document}


If you insert an equation before it, LaTeX will do the numbering by itself:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Equation~\ref{answer} is the Answer to Life, the Universe and Everything

Equation~\ref{xsquared} shows that the universe is
infinite, but equation~\ref{ysquared} says otherwise.
$$\label{answer} x = y = 42$$
$$\label{xsquared} a = x^2$$
$$\label{ysquared} b = y^2$$
\end{document}


# Getting fancy :)

Barbara Beeton pointed a few problems with my previous answer.

An empty line between an and the next will create an extra empty space between the equations, so I removed them.

Notice also the ugly spacing between equations (1) and (2) in the previous figure. The gather environment from amsmath allows multiple equations inside the same environment and makes the spacing between them correct. At the same time, it allows one label per equation the same way that the three equation environments allowed:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
Equation~\ref{answer} is the Answer to Life, the Universe and Everything

Equation~\ref{xsquared} shows that the universe is
infinite, but equation~\ref{ysquared} says otherwise.
\begin{gather}
x = y = 42\label{answer}\\
a = x^2\label{xsquared}\\
b = y^2\label{ysquared}
\end{gather}
\end{document}


Much better now :)

• there's an awful lot of space above and between these equations. it's not recommended to leave a blank line above any display, and if multiple equations are grouped with no (or very little) text between them, using a multi-line structure from amsmath is also recommended. (gather would be the most appropriate here.) – barbara beeton Mar 8 '18 at 18:32
• @barbarabeeton I didn't notice that a blank line above a math display would create those spaces. Thank you for the advice :) – Phelype Oleinik Mar 8 '18 at 22:02
• Thanks Phelype Oleinik, I guess I was just putting the label in the wrong place – cohara Mar 9 '18 at 14:02