
While writing a 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{equation} \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} \end{equation}
This code produces the following;
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{equation} \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} \end{equation}
This code produces the following;
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{equation} \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} \end{equation}
This code produces the following;
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;
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 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 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
New #orblog post LaTeX Templates for Optimization Models http://t.co/easrPpZP8E #orms @orcomplete
If you write your LP assignment in LaTeX, this will be helpful. “@orcomplete: LaTeX Templates for Optimization Models http://t.co/zCABbneqax
LaTeX templates for optimization models http://t.co/GD0D03RwCd
#tex formulas can be tricky. This reference can help. http://t.co/Z77nkBP0ff
Great post – thanks to Vince Knight on G+ for sharing this, and for suggesting sharing the examples as a writeLaTeX document so everyone can easily see the link between the code and the output.
Here are all four examples: https://www.writelatex.com/read/gfzmhpgfswyc
Hey John, thank you and Vince Knight, it is really a good idea! I’ll put a link inside the post. Thanks for this great document.
LaTeX Templates for Optimization Models « OR Complete | Collective Operations Research Blog http://t.co/rjViWVp3fN
LaTeX Templates for Optimization Models http://t.co/esL4v90Ohh #ORMS
My intern has started using them! tx RT @ciamk LaTeX Templates for Optimization Models http://t.co/go3VZRsgI3 #ORMS
Array, align, aligned, matrix. RT @ciamk: LaTeX Templates for Optimization Models http://t.co/HdZiiUiMUw #ORMS
For my friends and their optimization models: LaTeX templates for all those equations and constraints. http://t.co/tVs6HuJPf1
Hey, thanks for sharing! In order to prevent writing \displaystyle in front of all sum signs you can also simply write once \everymath{\displaystyle} before your \begin{document}.
Hey Markus thanks for the useful tip! I didn’t know such an elegant solution.
wow!! I like the result by using \displaystyle, but writing it all the time…Thank you so much.
I have recently developed a small library to define optimization problems. The source and documentation are in https://www.ctan.org/pkg/optidef, and I have written a small explanation in here http://tex.stackexchange.com/questions/19465/is-there-a-package-for-specifying-optimization-problems
If you find it useful, it would be great if you could also share it.
Hi Jesus, thanks for letting us know. I added a link to your package at the end of the post.
[…] templates for optimization models […]
Extremely helpful. Thanks for sharing!