LaTeX Templates for Optimization Models

Share

While writing a $\text{\LaTeX}$ file, many times I needed to insert an optimization model in an elegant way. While it may seem easier to find a template that works well, there are different ways to do it. Here, I will compile a list of different methods, so you don’t have to Google it or copy from your old documents every time you need it.

1) Array

This is my favorite style for many reasons. It’s very flexible and provides tidy output even if your model is large.

$$\begin{array}{rrclcl} \displaystyle \min_{x} & \multicolumn{3}{l}{c^T x} \\ \textrm{s.t.} & A x & \leq & b \\ &\displaystyle \sum_{i=0}^{n} x_i & = & 1 \\ & x_j & \geq & 0 & & \forall j \in N \\ \end{array}$$

This code produces the following;

$\begin{array}{rrclcl} \displaystyle \min_{x} & \multicolumn{3}{l}{c^T x} \\ \textrm{s.t.} & A x & \leq & b \\ &\displaystyle \sum_{i=0}^{n} x_i & = & 1 \\ & x_j & \geq & 0 & & \forall j \in N \\ \end{array}$

As you see, first column in the array is used for “min” and “subject to”. Hence, all lines except first two start with “&” symbol. Second, third and fourth columns are used to define constraints.

Note that, using eqnarray instead of array may lead some spacing inconsistencies.

Advantages: Clean and tidy output, works well even for long constraints / models
Disadvantages: You need to add “\displaystyle” every time you use a summation symbol
(Tip: Adding \everymath{\displaystyle} before \begin{document} is another option as noted in comments)

2) Aligned

This is a template I found at JC Notes. It produces left-aligned blocks so you need to use constraints without align.

\begin{aligned} & \underset{x}{\text{min}} & & c^T x \\ & \text{s.t.} & & Ax \leq b_i \\ & & & \sum_{i=1}^{n} x_i =1 \\ & & & x_j, \; \forall j \in N. \\ \end{aligned}

This code produces the following;

\begin{aligned} & \underset{x}{\text{min}} & & c^T x \\ & \text{s.t.} & & Ax \leq b_i \\ & & & \sum_{i=1}^{n} x_i =1 \\ & & & x_j \geq 0, \; \forall j \in N. \end{aligned}

Advantages: No need for “\displaystyle”
Disadvantages: Alignment is not flexible (everything is left-aligned), multi-column is not available (if you have a long objective function, there will be some problems)

3) Matrix

This method provides centered blocks.

$$\begin{matrix} \displaystyle \min_x & c^T x \\ \textrm{s.t.} & A x & \leq & b \\ & \displaystyle \sum_{i=1}^{n} x_i & = & 1 \\ & x_j & \geq & 0 & & \forall j \in N \end{matrix}$$

This code produces the following;

$\begin{matrix} \displaystyle \min_x & c^T x & & & \\ \textrm{s.t.} & A x & \leq & b & & \\ & \displaystyle \sum_{i=1}^{n} x_i & = & 1 & & \\ & x_j & \geq & 0 & & \forall j \in N \end{matrix}$

This has similar problems to Aligned method. If your constraints have similar size, then you may like the result. To me, it looks good for this example.

Disadvantages: Again, you need to switch between style modes (\displaystyle)

4) Align

You can also get the same result if you use “aligned”, but this one is a different approach. You can think align as an array with two columns, where first column is always right-aligned and second column is always left-aligned. I trimmed “s.t.” since it leads a dirty result.

\begin{align*}
\min_x \quad c^T x \\
Ax &\leq b \\
\sum_{i=1}^n x_i &= 1\\
x_j &\geq 0 \quad \forall j \in N
\end{align*}


which gives;

\begin{aligned} \min_x \quad c^T x \\ Ax &\leq b \\ \sum_{i=1}^n x_i &= 1\\ x_j &\geq 0 \quad \forall j \in N \end{aligned}

Advantages: Clean and easy-to-edit
Disadvantages: Manual alignment of extra parts (for all, etc.), objective function may shift if you use long constraints

Conclusion

There’s no method that is simply the best, it’s all about applying the one which works for your needs. If you already published some papers, you’ll already have a method in your mind to do it. I tried some of them for my research notes, and I prefer the first method. Flexibility of the first method is the key for my models. But, in the end, all methods has some features that serve you better than others.

Update: John Hammersley prepared an online $\text{\LaTeX}$ file that shows the difference between codes at Vince Knight‘s suggestion. Thank you both!

Update 2: Jesus Lago Garcia commented that they have a $\LaTeX$ package for defining optimization problems named optidef. Thanks for the package and letting us know Jesus.

Have an alternative to add to this list? Share it in the comments!

Photo by Jonathan Joseph Bondhus

Sertalp Bilal Çay

PhD Candidate and Teaching Assistant in Industrial and Systems Engineering Department at Lehigh University. Researcher on Conic Optimization, Inventory Theory, Supply Chain Management and Simulation. Blog: sertalpbilal.com