I recently learnt the best(?) way to format an linear program in LaTeX is something like this:
\begin{alignat}{3}
\text{min:} \quad & \omit{\rlap{$ \sum_{i=1}{n} c_i X_i $}} \\
\text{s.t.} \quad & a_t \leq \sum_{i=1}{n} X_i \leq b_t \quad && t=1,\cdots,T \\
& X_i \in \{0,1\} \quad && i=1,\cdots,n
\end{alignat}
This way, the constraints are all left-aligned after the "s.t." with a little space, all the constraint domains are left-aliged after the longest constraint with a little space, and even if the objective function is long(ish) then I don't get a huge gap as I would if I just put a &&
at the end without the \omit{\rlap{$[...]$}}
.
Anyway, the above is just some nonesense I wrote. But here is an actual one that is misbehaving in my document:
\begin{alignat}{3}
\text{min:} \quad & \omit{\rlap{$\alpha\sum_{i=1}^N\sum_{k=1}^K\sum_{l=1}^K b_{k,l}Y_{i,k}Y_{i,l} + \beta\sum_{i=1}^N\sum_{j=1}^M c_{i,j}X_{i,j} + \gamma(\sum_{j=1}^M S_j + T_j)$}} \\
\text{s.t.} \quad & \sum_{i=1}^N X_{i,j} + S_j - T_j = 1 \quad && j=1,\ldots,M \\
& S_j \in \mathbb{Z}^+ \quad && j = 1, \ldots, M \\
& T_j \in \mathbb{Z}^+ \quad && j = 1, \ldots, M
\end{alignat}
When I build this, the objective function (the first line) runs off the end of the page, however there is plenty of space on the left to fit it in.
I tried adding an extra empty alignment point &
at the end of each line, and after the end of the \omit
, but that did absolutely nothing.
I can fix it by splitting the first line into two, but I'd prefer to just move the whole thing to the left.
Thanks.
Edit 1: actually I didn't even notice this before, but it looks like the first equation is in-line math in text (which makes sense since it's in $
). The parameters of the sums are not above and below like in the other lines, but next to them. Any idea how to fix this?
Edit 2: ok, \displaystyle
after the $
fixes the problem with the first line being in-line math mode.
\omit
you're basically giving up withalignat
measuring the entry, so you are responsible for line breaking it. By the way,\rlap
makes a zero width object, so the overlapping is not a surprise.