11

I am quite new to LaTeX and want to typeset a optimisation problem. I found a very neat example at https://jcnts.wordpress.com/2009/11/11/formatting-optimization-problems-with-latex/:

enter image description here

The only problem is it don't show a new equation number for each line. So I changed it and now get the equation numbers but there's a huge gap between min/subject to and the cost function/constraints now. Is there any possibility to combine the best of both approaches?

Code for both approaches:

\documentclass{report}
\usepackage{amsmath}
\usepackage{hyperref}
\newcommand{\norm}[1]{\lVert#1\rVert_2}

\begin{document}
my approach
\begin{subequations}
    \begin{align}
    &\underset{x}{\text{min}}
    &&\norm{f(x)}^2\label{eq:optProb}\\
    &\text{subject to}
    &&\alpha \geq 0,\label{eq:constraint1}\\
    &&&\beta \geq 0.\label{eq:constraint2}
    \end{align}
\end{subequations}
%
%
approach from\\ \url{https://jcnts.wordpress.com/2009/11/11/formatting-optimization-problems-with-latex/}:
\begin{equation}
    \begin{aligned}
        &\underset{x}{\text{min}}&&\norm{f(x)}^2\\
        &\text{subject to} &&\alpha \geq 0,\\
        &&&\beta \geq 0.
    \end{aligned}
\end{equation}
\end{document}
Mico
  • 506,678
hamalo
  • 113

2 Answers2

10

Just change align into alignat:

\documentclass{report}
\usepackage{amsmath}

\newcommand{\norm}[1]{\lVert#1\rVert_2}

\begin{document}

\begin{subequations}
\begin{alignat}{2}
&\!\min_{x}        &\qquad& \norm{f(x)}^2\label{eq:optProb}\\
&\text{subject to} &      & \alpha \geq 0,\label{eq:constraint1}\\
&                  &      & \beta \geq 0.\label{eq:constraint2}
\end{alignat}
\end{subequations}

\end{document}

enter image description here

egreg
  • 1,121,712
3

I rather use alignat because allows aligning the equals\inequality signs also:

\begin{alignat}{3}
\max_x              &\quad&  z = \sum_{j=1}^n c_i x_j  &&          & \\
\text{subject to: } &\quad&  \sum_{j=1}^n a_{ij} x_j   && \leq b_i &\quad \forall i=1,m\\
                    &\quad&  x_j                       && \geq 0   &\quad \forall j=1,n
\end{alignat}

enter image description here

The numbers appear by default.

Ariel
  • 131