
장 번호가 포함된 알고리즘에서 방정식 번호를 원합니다. 여기 내 코드가 있습니다.
\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}
결과는 다음과 같습니다.
내 참조가 왜 잘못된지 이해가 되지 않습니다. 누구든지 설명해 주시겠습니까?
답변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
방정식 번호 + 괄호의 필수 복제본을 제공합니다.