Incorrect reference to the equation number in algorithm2e

I want equation number in an algorithm with chapter number included. Here is my code.

\documentclass{book}
\usepackage{amsmath,amssymb,amsthm, latexsym,float,epsfig,subfig}
\usepackage[mathscr]{euscript}
\usepackage{mathrsfs, breqn}
\usepackage[ruled,vlined,linesnumbered,algochapter]{algorithm2e}
\usepackage{multirow}
\newtheorem{exmp}{Example}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{corollary}{Corollary}

\numberwithin{figure}{chapter}
\numberwithin{theorem}{chapter}
\numberwithin{exmp}{chapter}
\numberwithin{lemma}{chapter}
\numberwithin{proposition}{chapter}
\numberwithin{corollary}{chapter}
\numberwithin{equation}{chapter}
\begin{document}
\chapter{First}
\label{chap_ch1}

\begin{algorithm}[!h]
\DontPrintSemicolon
\SetAlgoLined

\If{p} {
$$\label{eq:sclrec1} a = b$$
}
\Else {
\If{t} {
$$\label{eq:sclrec2} x = y$$
}

}

\caption{\label{alg:algo} algorithm}
\end{algorithm}
Equation~\ref{eq:sclrec1} and Equation~\ref{eq:sclrec2}

\end{document}


The result is as follows :

I don't understand why my references are incorrect. Can anyone please explain ?

writeLatex Code

-
I guess it is referencing the line numbers. –  cfr May 11 at 2:08
an extremely ugly hack: \addtocounter{equation}{-1}\refstepcounter{equation}\label{eq:s‌​clrec1} –  cmhughes May 11 at 3:56

This is a conflict between amsmath and algorithm2e (your example includes a number of packages not relevant to the problem) and only applies to the equation environment. The simplest workaround is to just use gather instead.

For equation placing text before the equation or in the absence of such just \leavevmode will also work:

\documentclass{book}

\usepackage{amsmath}
\usepackage[ruled,vlined,linesnumbered,algochapter]{algorithm2e}

\begin{document}

\chapter{First}
\label{chap_ch1}

\begin{algorithm}[!h]
\DontPrintSemicolon
\SetAlgoLined

\If{p} { Correct reference
\begin{gather}    \label{eq:sclrec1}
a = b
\end{gather}
}
\Else {
\If{t} { \leavevmode
$$\label{eq:sclrec2} x = y$$
}
\Else {
$$\label{eq:sclrec3} p = q$$
}
}

\caption{\label{alg:algo} algorithm}
\end{algorithm}
Equation~\ref{eq:sclrec1} and Equation~\ref{eq:sclrec2} are correctly
referenced, equation~\ref{eq:sclrec3} (really 1.3) is not.

\end{document}

-

The following provides a work-around, as well as a better vertical alignment of the equations:

\documentclass{book}

\usepackage{amsmath}
\usepackage[ruled,vlined,linesnumbered,algochapter]{algorithm2e}

\begin{document}
\chapter{First}\label{chap_ch1}

\begin{algorithm}[!h]
\DontPrintSemicolon
\SetAlgoLined
\If{p} {
\hfill\llap{%
\makebox[\linewidth]{\hfill $a = b$\hfill\refstepcounter{equation}\llap{(\theequation)}\label{eq:sclrec1}}}
}
\Else {
\If{t} {
\hfill\llap{%
\makebox[\linewidth]{\hfill $x = y$\hfill\refstepcounter{equation}\llap{(\theequation)}\label{eq:sclrec2}}}
}
}
\caption{\label{alg:algo} algorithm}
\end{algorithm}

Equation~\eqref{eq:sclrec1} and Equation~\eqref{eq:sclrec2}

\end{document}


Each equation is set in a box of width \linewidth (thereby disregarding the well-maintain algorithm indentation - perhaps something to improve upon) with the equation exactly in the middle. The equation counter is manually stepped (and made possible to reference) and set.

Since you're loading amsmath, using \eqref provides the required duplicate of the equation number + parentheses.

-