アルゴリズム2eの方程式番号の参照が間違っている

アルゴリズム2eの方程式番号の参照が間違っている

章番号が含まれたアルゴリズムの方程式番号が欲しいです。これが私のコードです。

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

結果は以下のようになります。

コードの結果

私の参照がなぜ間違っているのか分かりません。どなたか説明していただけますか?

writeLatexコード

答え1

amsmathこれはとの間の競合でありalgorithm2e(例には問題に関係のないパッケージがいくつか含まれています)、equation環境にのみ適用されます。最も簡単な回避策は、代わりに を使用することですgather

equation数式の前にテキストを配置する場合、または数式がない場合は次のように記述するだけ\leavevmodeでも機能します。

サンプル出力

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

答え2

以下に、回避策と、方程式の垂直方向の配置を改善する方法を示します。

ここに画像の説明を入力してください

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

各方程式は幅のあるボックス内に設定され\linewidth(そのため、よく維持されているアルゴリズムのインデントは無視されます - おそらく改善すべき点です)、方程式はちょうど真ん中に配置されます。方程式カウンターは手動でステップされ (参照可能になります)、設定されます。

読み込んでいるのでamsmathを使用すると、\eqref必要な数式番号 + 括弧の複製が提供されます。

関連情報