演算法 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}

結果如下:

程式碼的結果

我不明白為什麼我的參考資料不正確。誰能解釋一下嗎?

編寫Latex程式碼

答案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提供了所需的方程式編號+括號的重複項。

相關內容