18

I would like to format an optimization problem by

\documentclass{article}
\usepackage{amsmath}

\begin{document}

We want to label each line separately. The inserted text and formulas
have to be aligned like in this example:
\begin{equation}
\begin{aligned}
\label{eq:3}
& \underset{X}{\text{minimize}} & &  \mathrm{trace}(X) \\
& \text{subject to} &&  X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\
& &&  X \succeq 0. 
\end{aligned}
\end{equation}

\end{document}

enter image description here

(Already described in How to label an optimization problem with a label such as (P)?).

An ideal alignment of the inserted text and formulas is achieved by the aligned environment. However, I want to label each line separately. By placing \label{...} at each line

\begin{equation}
\begin{aligned}
& \underset{X}{\text{minimize}} & &  \mathrm{trace}(X) \label{eq:6}\\
& \text{subject to} &&  X_{ij} = M_{ij}, \; (i,j) \in \Omega, \label{eq:7} \\
& &&  X \succeq 0. \label{eq:9} 
\end{aligned}
\end{equation}

I get the error message: "Package amsmath Error: Multiple \label's: label 'eq:4' will be lost."

How can I label each line individually?

  • 4
    Using align environment instead of equation and aligned will work: \begin{align} & \max \sum p_t s_t \label{eq:3}\& \text{s.t. } s_t \leq d_t \label{eq:4}\& \sum s_t \leq I \label{eq:5} \end{align} – Peter Grill Mar 23 '13 at 9:14
  • I need the tighter alignment provided by the aligned environment. The original example did not show the relevant point, so I inserted another example. – Robinaut Mar 23 '13 at 15:30
25

Only one label is possible within the equation environment. However, multiple labels are possible within an align environment. In your case, as in many other, using an align is preferable to using an aligned inside an equation. Moreover, the subequations environment lends itself well to your problem.

Additional note: in math mode, it's good practice to put a thin space (\,) before punctuation marks (such as full stops and commas), so that the reader does not confuse them as part of the math quantities they precede.

enter image description here

\documentclass{article}
\usepackage{amsmath}

\begin{document}

We want to label each line separately. The inserted text and formulas
have to be aligned like in this example:
\begin{subequations}
\label{eq:optim}
\begin{align}
    \underset{X}{\text{minimize}}
        & \quad \mathrm{trace}(X)   \label{eq:cost}\\
    \text{subject to} 
        & \quad X_{ij} = M_{ij}\,, \; (i,j) \in \Omega\,, \label{eq:const1}\\
        & \quad X \succeq 0 \,. \label{eq:const2}
\end{align}
\end{subequations}

Equation \ref{eq:cost} is the cost and Equations \ref{eq:const1} and \ref{eq:const2}
are the constraints of optimisation problem \ref{eq:optim}.

\end{document}
  • There is another approach with alignat which is more closely to the origonal formatting: \begin{alignat}{2} &\underset{X}{\text{minimize}} \quad& &\mathrm{trace}(X) \label{eq:cost}\ &\text{subject to} & &X_{ij} = M_{ij}\,, \; (i,j) \in \Omega\,, \label{eq:const1}\ & & &X \succeq 0 \,. \label{eq:const2} \end{alignat} – Robinaut Mar 24 '13 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.