Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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} {
                \begin{equation}    \label{eq:sclrec1}
                    a = b
                \end{equation}
            }
            \Else {
                \If{t} {
                    \begin{equation} \label{eq:sclrec2}
                    x = y
                    \end{equation}
                }

            }

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

\end{document}

The result is as follows :

Result of the code

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

writeLatex Code

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

2 Answers 2

up vote 3 down vote accepted

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:

Sample output

\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
          \begin{equation} \label{eq:sclrec2}
          x = y
          \end{equation}
      }
      \Else {
      \begin{equation}
        \label{eq:sclrec3}
        p = q
      \end{equation}
      }
  }

  \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}
share|improve this answer

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

enter image description here

\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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.