子方程和对齐:错误:包 amsmath 错误:\tag 此处不允许?

子方程和对齐:错误:包 amsmath 错误:\tag 此处不允许?

我的优化问题存在问题。如下所示。

\documentclass{article}
    \usepackage[english]{babel}
    \usepackage{amsmath}
    \usepackage{amssymb}
    \usepackage{newtxtext,newtxmath}

    \begin{document}

        \begin{subequations}
            \label{pb:1} %\tag{P1} --> HERE IS THE PROBLEM
            \begin{align}
                & {\underset{\mathbf{ x }}{\text{maximize}}}
                & & \sum_{i=1}^nv_ix_i\label{obj:1}\tag{P1a}\\
                & \text{subject to}
                & &  \sum_{i=1}^nw_ix_{i}\leqslant W,\label{c:1}\tag{P1b}\\
                & & & x_{ i }\in\{0, 1\}, \forall i=1\ldots,n.\label{c:1}\tag{P1c}
          \end{align}
       \end{subequations}

       Problem~\eqref{pb:1} is a binary programming problem.


    \end{document}

当我删除 时\tag{P1},没有出现任何错误。如何修复此问题?

当尝试将问题标记为 、 等时,就会出现此问题。(P1)因此(P1a),我发现我应该添加\tag但当我这样做时,我唯一可以引用的方法(P1)是每次都调用它(P1)。例如,如果我写,\eqref{pb:1}我将得到:

enter image description here

在上图中,每当我调用 时,我都想(1)用替换。这就是为什么我在 后面添加了。(P1)\eqref{pb:1}\tagsubequations

但是,如果我删除标签,我会得到:

enter image description here

但这不是我想要的。

答案1

在这里,您可以使用独立的计数器来解决方程式和问题:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
%\usepackage{amssymb} % not needed with newtxmath
\usepackage{newtxtext,newtxmath}

\newcommand{\maximize}{%
  \mathopen{}\operatorname*{maximize}%
}
\newcommand{\subjto}{\textup{subject to}}

\newcounter{problem}
\newcounter{save@equation}
\newcounter{save@problem}

\makeatletter
\newenvironment{problem}
 {\setcounter{problem}{\value{save@problem}}%
  \setcounter{save@equation}{\value{equation}}%
  \let\c@equation\c@problem
  \renewcommand{\theequation}{P\arabic{equation}}%
  \subequations
  }
 {\endsubequations
  \setcounter{save@problem}{\value{equation}}%
  \setcounter{equation}{\value{save@equation}}%
 }

\begin{document}

Some text and an equation
\begin{equation}
a=b
\end{equation}
Some other text before the problem
\begin{problem}\label{pb:1}
\begin{alignat}{2}
& \maximize_{\mathbf{x}}
  &\qquad & \sum_{i=1}^nv_ix_i\label{obj:1}\\
& \text{subject to}
  & &  \sum_{i=1}^nw_ix_{i}\leqslant W,\label{b:1}\\
  & & & x_{ i }\in\{0, 1\}, \forall i=1\ldots,n.\label{c:1}
\end{alignat}
\end{problem}
Problem~\eqref{pb:1} is a binary programming problem. And
another equation
\begin{equation}
a=b
\end{equation}
And another problem
\begin{problem}\label{pb:2}
\begin{alignat}{2}
& \maximize_{\mathbf{x}}
  &\qquad & \sum_{i=1}^nv_ix_i\label{obj:2}\\
& \text{subject to}
  & &  \sum_{i=1}^nw_ix_{i}\leqslant W,\label{b:2}\\
  & & & x_{ i }\in\{0, 1\}, \forall i=1\ldots,n.\label{c:2}
\end{alignat}
\end{problem}

\end{document}

enter image description here

答案2

只需重新定义\theequation

\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{newtxtext,newtxmath}

\renewcommand*{\theequation}{P\arabic{equation}}

\begin{document}

\begin{subequations}
  \label{pb:1}
  \begin{align}
    & {\underset{\mathbf{ x }}{\text{maximize}}}
    & & \sum_{i=1}^nv_ix_i\label{obj:1}\\
    & \text{subject to}
    & &  \sum_{i=1}^nw_ix_{i}\leqslant W,\label{c:1}\\
    & & & x_{ i }\in\{0, 1\}, \forall i=1\ldots,n.\label{c:2}
  \end{align}
\end{subequations}

Problem~\eqref{pb:1} is a binary programming problem.

\end{document}

P1a-P1c

如果您需要不同的前缀,您可以使用类似以下内容:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{newtxtext,newtxmath}

\renewcommand*{\theequation}{\theequationprefix\arabic{equation}}
\newcommand*{\theequationprefix}{}
\newcommand*{\equationprefix}[2][0]{%
  \renewcommand*{\theequationprefix}{#2}% Change the prefix
  \setcounter{equation}{#1}% reset the counter to optional argument
}

\begin{document}

\equationprefix{P}% must be before subequation!
\begin{subequations}
  \label{pb:1}
  \begin{align}
    & {\underset{\mathbf{ x }}{\text{maximize}}}
    & & \sum_{i=1}^nv_ix_i\label{obj:1}\\
    & \text{subject to}
    & &  \sum_{i=1}^nw_ix_{i}\leqslant W,\label{c:1}\\
    & & & x_{ i }\in\{0, 1\}, \forall i=1\ldots,n.\label{c:2}
  \end{align}
\end{subequations}

Problem~\eqref{pb:1} is a binary programming problem.

\equationprefix{}% no prefix
\begin{align}
  x=y\label{noprefix}
\end{align}
Equation~\eqref{noprefix}.

\end{document}

相关内容