9

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

2
  • 2
    I guess it is referencing the line numbers.
    – cfr
    Commented May 11, 2014 at 2:08
  • an extremely ugly hack: \begin{equation}\addtocounter{equation}{-1}\refstepcounter{equation}\label{eq:sclrec1}
    – cmhughes
    Commented May 11, 2014 at 3:56

2 Answers 2

3

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}
7

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .