Formatting Optimization Problems with LaTeX

Constrained optimization problems are almost everywhere in engineering research. A mathematical description of those problems with a single objective is to minimize or maximize an objective function over a set of decision variables under a set of constraints. There are different ways to format optimization problems; personally, I follow the format used in the book “Convex Optimization” by Stephen Boyd and Lieven Vandenberghe. For example, a general optimization problem has the form

\displaystyle \begin{aligned}& \underset{x}{\text{minimize}} & & f_0(x) \\ & \text{subject to} & & f_i(x) \leq b_i, \; i = 1, \ldots, m.\end{aligned}

This is generated by the following \LaTeX code:

\begin{equation*}
\begin{aligned}
& \underset{x}{\text{minimize}}
& & f_0(x) \\
& \text{subject to}
& & f_i(x) \leq b_i, \; i = 1, \ldots, m.
\end{aligned}
\end{equation*}

As seen in the code, the formatting is done by the aligned environment, which is defined in the amsmath package, so you need to include the following line in the preamble:

\usepackage{amsmath}

Unlike the tabular environment, in which you can specify the alignment of each column, in the aligned environment, each column (separated by &) has a default alignment, which alternates between right and left-aligned. Therefore, all the odd columns are right-aligned and all the even columns are left-aligned.

We conclude with a real example:

\displaystyle \begin{aligned}& \underset{X}{\text{minimize}} & &\mathrm{trace}(X) \\ & \text{subject to} & & X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\ &&& X \succeq 0.\end{aligned}

The above problem is formulated for completing low-rank positive semidefinite matrices. It is convex, or more precisely, it is a semidefinite program.  The corresponding \LaTeX code is

\begin{equation*}
\begin{aligned}
& \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*}

Recently, Mr. Garcia has brought to my attention a very neat \LaTeX package that he has contributed: optidef. The package documentation has all the details, and he has also provided some quick examples. I’d definitely recommend this package since its syntax aligns with how you define instead of how you format an optimization problem, which I assume that most people would prefer.

21 thoughts on “Formatting Optimization Problems with LaTeX

  1. Really nice, looks much better than what I was doing. Just need to think of whether I prefer multiple inputs to have parenthesis or not.

  2. Hi,

    Thanks a lot for the code, really helpful. I was also wondering how do you include number for each equation?
    In each line, I would like an equation number (like \label{}), is that possible and how?

    Thanks a lot!

  3. As Mohamed suggested, you can remove the asterisk (*) from \begin{equation*} & \end{equation*} to assign a single number to the entire problem. If you’d like to assign a number to each line, then the following example should help:

    \begin{alignat}{2}
    & \underset{X}{\text{minimize}}
    & & \mathrm{trace}(X) \\
    & \text{subject to} \quad
    & & X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\
    &&& X \succeq 0.
    \end{alignat}

    I tried the “align” environment, but it left too much space between columns. Notice the \quad I added after \text{subject to}, without which there will be too little space between columns. You can adjust the space as you like.

  4. Instead of using the underset, you can use the following (needs standard AMS packages):
    \DeclareMathOperator*{\minimize}{minimize}

    then use \minimize_{x} and it will make a nice subscript for you

  5. THANK YOU SO MUCH. Surprisingly complicated, this solution works great, with two different formatting options.

Leave a reply to Rai Cancel reply