Vous êtes sur la page 1sur 5

JC Notes

James' random thoughts

Formatting Optimization Problems with LaTeX

Posted on November 11, 2009 by James

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
(h p://www.stanford.edu/~boyd/cvxbook/)” by Stephen Boyd (h p://www.stanford.edu/~boyd/) and
Lieven Vandenberghe (h p://www.ee.ucla.edu/~vandenbe/). For example, a general optimization
problem has the form

This is generated by the following code:

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

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

1 \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:

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 code is

1 \begin{equation*}
2 \begin{aligned}
3 & \underset{X}{\text{minimize}}
4 & & \mathrm{trace}(X) \\
5 & \text{subject to}
6 & & X_{ij} = M_{ij}, \; (i,j) \in \Omega, \\
7 &&& X \succeq 0.
8 \end{aligned}
9 \end{equation*}

Recently, Mr. Garcia (h p://www.jesuslago.com) has brought to my a ention a very neat package
that he has contributed: optidef (h ps://www.ctan.org/pkg/optidef). The package documentation
(h p://ctan.mackichan.com/macros/latex/contrib/optidef/optidef.pdf) has all the details, and he has also
provided some quick examples (h p://tex.stackexchange.com/questions/19465/is-there-a-package-for-
specifying-optimization-problems). 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.


This entry was tagged latex, math. Bookmark the permalink.

This entry was tagged latex, math. Bookmark the permalink.

20 thoughts on “Formatting Optimization Problems

with LaTeX”

EdisonZou says:
on December 18, 2009 at 1:08 pm
Good reference! Also it leads me to read some paper in your website! Thoughtful man!

AK says:
on February 5, 2010 at 7:43 pm
Nice piece of code, I had this in Masters thesis too, but can’t find my LaTex scripts.


JPL says:
on March 9, 2010 at 5:42 pm
Thanks for sharing this code.
It turns out that book is pre y much my text book

bluejean says:
on March 12, 2010 at 12:24 am
Thank you so much for the code.
It helps a lot.

Quan says:
on May 13, 2010 at 1:40 pm
Thank you. It works like a charm.

bobbens says:
on May 19, 2011 at 4:37 am
Really nice, looks much be er than what I was doing. Just need to think of whether I prefer multiple
inputs to have parenthesis or not.

Alex says:
on June 17, 2011 at 3:21 am

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

Thanks a lot!

Mohamed says:
on June 18, 2011 at 1:01 pm
To insert the equation number, just remove the star (*) sign from \equation*

James says:
on June 25, 2011 at 12:25 pm
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:

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

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 li le space between columns. You can
adjust the space as you like.

Arya Iranmehr (@airanmehr) says:
on October 23, 2011 at 12:42 pm
Great! This is exactly what I was looking for

Stock says:
on October 26, 2011 at 10:21 am
Thanks a lot!

Ermi says:
on December 27, 2011 at 12:54 am
tnx man u just saved me

joe says:
on May 4, 2012 at 1:33 pm
works great. thanks.

smilynnzhang says:
smilynnzhang says:
on May 24, 2012 at 7:56 am
Thank you a lot !

Vahid says:
on December 18, 2012 at 12:09 pm
Thanks a lot. Very helpful.

Denny says:
on September 6, 2013 at 7:59 pm
Thank you so much. Saved me a lot of time.

17. Pingback: LaTeX Templates for Optimization Models « OR Complete | Collective Operations
Research Blog

Stephen says:
on November 23, 2014 at 1:42 pm
Instead of using the underset, you can use the following (needs standard AMS packages):

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

kayfane says:
on April 10, 2016 at 1:17 pm
Reblogged this on Kay Fan E and commented:
I hope that there will be a LaTex tool to help write optimization formulations.

jesus says:
on January 22, 2017 at 8:48 am
I have recently developed a small library to define optimization problems. The source and
documentation are in h ps://www.ctan.org/pkg/optidef, and I have wri en a small explanation in
here h p://tex.stackexchange.com/questions/19465/is-there-a-package-for-specifying-optimization-

If you find it useful, it would be great if you could also share it.


Create a free website or blog at WordPress.com.