
Quero o número da equação em um algoritmo com o número do capítulo incluído. Aqui está meu código.
\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}
O resultado é o seguinte :
Não entendo por que minhas referências estão incorretas. Alguém pode explicar?
Responder1
Este é um conflito entre amsmath
e algorithm2e
(seu exemplo inclui vários pacotes não relevantes para o problema) e se aplica apenas ao equation
ambiente. A solução mais simples é apenas usar gather
.
Para equation
colocar texto antes da equação ou na ausência dela \leavevmode
também funcionará:
\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}
Responder2
O seguinte fornece uma solução alternativa, bem como um melhor alinhamento vertical das equações:
\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}
Cada equação é definida em uma caixa de largura \linewidth
(desconsiderando assim o recuo bem mantido do algoritmo - talvez algo para melhorar) com a equação exatamente no meio. O contador de equações é escalonado manualmente (e possibilitado a referência) e definido.
Já que você está carregandoamsmath
, usando \eqref
fornece a duplicata necessária do número da equação + parênteses.