# Problem with cross referencing using gather environment

I am using gather environment to write bunch of equations. When I try to cross-reference those equations, the equation number doesn't show up. Here is my code

\documentclass[12 pt]{article}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{float}
\usepackage{setspace}
\usepackage{longtable}
\usepackage{lscape}
\usepackage{color}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{caption}
\usepackage{array}
\makeatletter
\newcommand*{\compress}{\@minipagetrue}
\makeatother
\doublespacing

\begin{document}

\begin{gather}\label{eq:1}
a=1  \nonumber \\
b=1
\end{gather}
Equation \ref{eq:1}
\end{document}


The equation number is supposed to show up after "Equation" but its missing. What could be the problem? If I remove \nonumber then thing seems to work fine but I need \nonumber.

-
So what do you want numbered? a=1, b=1, the whole thing, or none of it? Since you're referencing something, it can't be one of the former three... –  Werner Mar 11 '13 at 5:46
I want to reference all the equations within gather by one number which is same as the number for b=1 –  John Smith Mar 11 '13 at 5:54
Please always try and post minimal working examples - you include a lot of packages that are unrelated to your problem. –  Hendrik Vogt Mar 11 '13 at 10:53

For this you need to use the gathered environment from amsmath and place the \label outside that environment. Another quick-and-dirty way to achieve this, without using amsmath, would be to use an array:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
$$\begin{gathered} a = 1 \\ b = 2 \end{gathered}\label{eq:1}$$
Equation~\eqref{eq:1} and~\eqref{eq:2}.
$$\begin{array}{c} a = 1 \\[\jot] b = 2 \end{array} \label{eq:2}$$
\end{document}


Also consider using \eqref instead of \ref for equations, since this is fully supported by amsmath to duplicate the behaviour of the equation number, even in other fonts.

-
Thanks this works perfectly. But the weird thing is the code that I provided above, I used it sometime before and it worked well. In any case, your solution is exactly what I wanted. –  John Smith Mar 11 '13 at 6:18

If you want to reference several equations by one number, then you can use gathered as Werner suggests, but I'd rather recommend the aligned environment in this case:

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\label{eq:1} \begin{aligned} a &= 1 \\ b &= 1 \end{aligned}
Equation~\eqref{eq:1}
\end{document}


The advantage of aligned is that you can use alignment tabs & to align the equals signs (which by coincidence is not necessary in your case). Note that you should use \eqref and not \ref for referencing the equation. Moreover, it's best to always use a tie ~ between the word "equation" and the \eqref so that you won't have a line break inbetween. (Obviously this can't happen here since we're at the beginning of a line, but just make it a habit.)

If you're wondering why you're code worked before, then my best guess is that you had the \nonumber \\ switched: with \\ \nonumber it works, more or less, since then the first equation has the label and the second one has no number:

\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{gather}\label{eq:1}
a=1 \\ \nonumber
b=1
\end{gather}
Equation~\eqref{eq:1}
\end{document}

-
I'd prefer the aligned environment to split, in this case; no substantial difference, but it makes intentions clearer: it's not a single equation split across lines, but two aligned equations (that we want to consider as a block nonetheless). –  egreg Mar 11 '13 at 10:54
I would also prefer aligned. Logically split is for dividing up one equation over several lines and aligning the result, aligned is for a group of equations. –  Andrew Swann Mar 11 '13 at 12:46
@Andrew: Thanks for insisting, egreg and you are so right! Edited accordingly. –  Hendrik Vogt Mar 11 '13 at 14:24