Subequations and align: ERROR: Package amsmath Error: \tag not allowed here?

I have an issue with my optimization problem. It is given in the following.

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


When I remove the \tag{P1}, I got no errors. How to fix this?

This problem comes when trying to label the problem as (P1), (P1a), etc. So I find that I should add the \tag but when I did this the only way I can reference (P1) is by calling it each time (P1). For example if I write \eqref{pb:1} I will get:

In the above figure, I would like to replace (1) by (P1) whenever I call \eqref{pb:1}. That's why I added \tag after the subequations.

However, if I remove the tags, I get:

but that's not what I want.

• \tag can only be used inside a math display , what do you want it to do in that position? – David Carlisle Jan 10 '17 at 17:22
• also why are you numbering by hand with \tag on each line???, it defeats the whole purpose of the subequations environment. – David Carlisle Jan 10 '17 at 17:24
• When I would like to reference P1, I write \eqref{pb:1} but it produces a number like (1). I would like to call it (P1) instead. Thanks. – Ribz Jan 10 '17 at 17:24
• Because I would like to name the problem as (P1), (P1a), etc. not (1), (1a), etc. – Ribz Jan 10 '17 at 17:25
• You have not got an equation 1 normally you would have an equation which is numbered say 2 then a bunch of equations inside subequations and they will automatically be numbered 2.1, 2.2, ... – David Carlisle Jan 10 '17 at 17:25

Here you can use independent counters for equations and problems:

\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
$$a=b$$
Some other text before the problem
\begin{problem}\label{pb:1}
\begin{alignat}{2}
& \maximize_{\mathbf{x}}
& \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
$$a=b$$
And another problem
\begin{problem}\label{pb:2}
\begin{alignat}{2}
& \maximize_{\mathbf{x}}
& \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}


• This is great! Thank you. Just a quick question: If I want to split a constraint (say the last one) in two lines using this code, should I do \\ & & & last constraint\\ & & & continued constraint. I did this but I am trying to shift the continued constraint to the right a little. So I did \\ & & & last constraint\\ & & & \quad\quad\quad\quad continued constraint but (1) the code looks ugly and (2) it doesn't shift further as I add more \quad. – Ribz Jan 11 '17 at 14:39
• @Riebuoz That's a completely different question. – egreg Jan 11 '17 at 15:42
• Is this possible in beamer? – Ribz Mar 3 '17 at 17:21
• @Ribz Numbered equations in beamer should be an exception: the audience won't remember what's equation 3 after just a few slides – egreg Mar 3 '17 at 17:44

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


If you need different prefixes you can use something like:

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

• If I have many other equations in my document, does this change all my \eqref{x} to (Px)? I mean later in my document, I have an equation called (20), will this be called (P20) if I call \eqref{20}? – Ribz Jan 10 '17 at 17:39
• and there are two c:1 labels. – Ulrike Fischer Jan 10 '17 at 17:39
• @Riebuoz: Sure. If you want different prefixes you have to redefine \theequation several times or define a markup command to change the prefix. – Schweinebacke Jan 10 '17 at 17:41
• How to define it only once or twice? (For some specific equations.) – Ribz Jan 10 '17 at 17:42