# 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. EdisonZou says:

2. AK says:

Nice piece of code, I had this in Masters thesis too, but can’t find my LaTex scripts.

Cheers

3. JPL says:

Thanks for sharing this code.
It turns out that book is pretty much my text book

4. bluejean says:

Thank you so much for the code.
It helps a lot.

5. Quan says:

Thank you. It works like a charm.

6. 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.

7. Alex says:

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!

8. Mohamed says:

To insert the equation number, just remove the star (*) sign from \equation*

9. James says:

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) \\
& & 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.

10. Great! This is exactly what I was looking for

11. Stock says:

Thanks a lot!

12. Ermi says:

tnx man u just saved me

13. joe says:

works great. thanks.

14. Vahid says:

15. Denny says:

Thank you so much. Saved me a lot of time.

16. […] is a template I found at JC Notes. It produces left-aligned blocks so you need to use constraints without […]

17. 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

18. kayfane says:

Reblogged this on Kay Fan E and commented:
I hope that there will be a LaTex tool to help write optimization formulations.

19. Rai says:

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