9
$\begingroup$

I am looking for a LaTeX template where a MIP problem formulation will look well-formatted.

Can anyone provide some example LaTeX code?

$\endgroup$
2
  • 7
    $\begingroup$ FYI, the inverse problem, converting latex to solver code, can be performed with tex2solver.com . $\endgroup$ Feb 22 at 13:04
  • 1
    $\begingroup$ I typically use the alignat math environment from the amsmath package. $\endgroup$
    – prubin
    Feb 22 at 16:45

6 Answers 6

6
$\begingroup$

I personally like the align environment with the following syntax:

\begin{align}
\label{eq:f.0} \mbox{minimize}~& \;\sum_{k\in K}\sum_{(i,j)\in A}c_{ijk}x_{ijk} + f_{ij}y_{ij}& \\
\label{eq:f.1} \mbox{s.t. } &\sum_{(o_k,j)\in A}x_{o_kj}^{k}=1  & \forall k\in K\\
\label{eq:f.2}&\sum_{(j,i)\in A}x_{ji}^{k}=\sum_{(i,j)\in A}x_{ij}^{k} & \forall k\in K, i\in V \setminus \{o_k,d_k\}\\
\label{eq:f.3}&\sum_{(i,d_k)\in A}x_{id_k}^{k}=1 & \forall k\in K\\
\label{eq:f.4}&0\leq x_{ijk}\leq y_{ij} & \forall k\in K, (i,j)\in A\\
\label{eq:f.5}& y_{ij} \in \{0,1\} & \forall (i,j)\in A
\end{align}

The result:

\begin{align} \label{eq:f.0} \mbox{minimize}~& \;\sum_{k\in K}\sum_{(i,j)\in A}c_{ijk}x_{ijk} + f_{ij}y_{ij}& \\ \label{eq:f.1} \mbox{s.t. } &\sum_{(o_k,j)\in A}x_{o_kj}^{k}=1 & \forall k\in K\\ \label{eq:f.2}&\sum_{(j,i)\in A}x_{ji}^{k}=\sum_{(i,j)\in A}x_{ij}^{k} & \forall k\in K, i\in V \setminus \{o_k,d_k\}\\ \label{eq:f.3}&\sum_{(i,d_k)\in A}x_{id_k}^{k}=1 & \forall k\in K\\ \label{eq:f.4}&0\leq x_{ijk}\leq y_{ij} & \forall k\in K, (i,j)\in A\\ \label{eq:f.5}& y_{ij} \in \{0,1\} & \forall (i,j)\in A \end{align}

Now it happens that your objective function has multiple terms and is quite long. This can mess up your formatting. One practical way to fix that is to use mathrlap:

\begin{align}
\label{eq:f.0} \mbox{minimize}~& \;\sum_{k\in K}\sum_{(i,j)\in A}c_{ijk}x_{ijk} + f_{ij}y_{ij} \mathrlap{+ \sum_{k\in K}\sum_{(i,j)\in A}c'_{ijk}x_{ijk} + f'_{ij}y_{ij}}& \\
\label{eq:f.1} \mbox{s.t. } &\sum_{(o_k,j)\in A}x_{o_kj}^{k}=1  & \forall k\in K\\
\label{eq:f.2}&\sum_{(j,i)\in A}x_{ji}^{k}=\sum_{(i,j)\in A}x_{ij}^{k} & \forall k\in K, i\in V \setminus \{o_k,d_k\}
\end{align}

The result (this is a picture since mathjax doesn't seem to understand mathrlap):

enter image description here

if your objective is really long, you want to split it over multiple lines. Simply add a \notag to prevent the numbering of your equations to increment. Here's again the code and corresponding result:

\begin{align}
\label{eq:f3.0} \mbox{minimize}~& \;\sum_{k\in K}\sum_{(i,j)\in A}c_{ijk}x_{ijk} + f_{ij}y_{ij} + xyz + & \\\notag
\label{eq:f3.1} & \sum_{k\in K}\sum_{(i,j)\in A}c'_{ijk}x_{ijk} + f'_{ij}y_{ij} & \\
\label{eq:f3.2} \mbox{s.t. } &\sum_{(o_k,j)\in A}x_{o_kj}^{k}=1  & \forall k\in K\\
\label{eq:f3.3}&\sum_{(j,i)\in A}x_{ji}^{k}=\sum_{(i,j)\in A}x_{ij}^{k} & \forall k\in K, i\in V \setminus \{o_k,d_k\}
\end{align}

enter image description here

$\endgroup$
5
$\begingroup$

I like to use the align environment, as follows: \begin{align} &\text{minimize} &\sum_j c_j x_j + \sum_i d_i y_i \\ &\text{subject to} &\sum_j a_{ij}^1 x_j &\le b_i^1 &&\text{for all $i$} \\ &&\sum_j a_{ij}^2 x_j &\le b_i^2 y_i &&\text{for all $i$} \\ &&x_j &\ge 0 &&\text{for all $j$} \\ &&y_i &\in \{0,1\} &&\text{for all $i$} \end{align}

If you need to save horizontal space because you have very wide constraints: minimize $\sum_j c_j x_j + \sum_i d_i y_i$ subject to \begin{align} \sum_j a_{ij}^1 x_j &\le b_i^1 &&\text{for all $i$} \\ \sum_j a_{ij}^2 x_j &\le b_i^2 y_i &&\text{for all $i$} \\ x_j &\ge 0 &&\text{for all $j$} \\ y_i &\in \{0,1\} &&\text{for all $i$} \end{align}

If you need to save horizontal space but still want to display the objective: minimize $$\sum_j c_j x_j + \sum_i d_i y_i$$ subject to \begin{align} \sum_j a_{ij}^1 x_j &\le b_i^1 &&\text{for all $i$} \\ \sum_j a_{ij}^2 x_j &\le b_i^2 y_i &&\text{for all $i$} \\ x_j &\ge 0 &&\text{for all $j$} \\ y_i &\in \{0,1\} &&\text{for all $i$} \end{align}

$\endgroup$
1
  • 3
    $\begingroup$ Note for newcomers to stackexchange, you can right click on the math TeX and select 'Show Math As' ->'TeX Commands' to see how they were written. $\endgroup$
    – Andy W
    Feb 23 at 14:03
5
$\begingroup$

I often use the following simple template based on the align environment from amsmath:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
& \min_{w,b,\xi} & & \frac{1}{2}w^{t}w+C\sum_{i=1}^{N}{\xi_{i}} \nonumber \\
& \textrm{s.t.} & & y_{i}(w\phi(x_{i}+b))+\xi_{i}-1 \label{opt_prob:misc} \tag{42}\\
& & & \xi\geq0 \nonumber \\
\end{align} 
\end{document}

Note that the alignment for consecutive columns will switch between left/right aligned. Hope this helps.

$\endgroup$
3
$\begingroup$

Well this naturally depends on you problem. Here is something JuMP generates using the latex_formulation function. I hope this helps. This is what i would use.

$$ \begin{aligned} \max\quad & preparedness\\ \text{Subject to} \quad & flunk_{1} = 0.0\\ & 8 preparedness - workdone_{1} - 10000 flunk_{1} \leq 0.0\\ & 5 preparedness - workdone_{2} - 10000 flunk_{2} \leq 0.0\\ & preparedness \geq 0.0\\ & preparedness \leq 1.5\\ & flunk_{1} \in \{0, 1\}\\ & flunk_{2} \in \{0, 1\}\\ \end{aligned} $$

$\endgroup$
2
$\begingroup$

I prefer to work with LaTeX with the hand as it is so cool and also allows you many kinds of flexibility, but an interesting tool to compare the different kinds of LaTeX for expression is SNIP software. You only need to capture your formulation with a camera and ask the SNIP to convert it in the different LaTeX formats. As far as I know, it has a free version. An example is: enter image description here

$\endgroup$
2
$\begingroup$

Another package worth mentioning is optidef. You can find some examples in this answer on TeX.SE.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.