2

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.

enter image description here

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.

  • Please, provide complete small document with all package used in this equation and page formatting. – Zarko Jan 5 '17 at 0:20
  • 1
    With \omit you're basically giving up with alignat 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. – egreg Jan 5 '17 at 0:23
3

Add some space by hand, in the code below it's \hspace{9em}.

I made also some small changes: a macro for the hidden equation and \bigg for the parentheses around the summation. The \ldots should be \dots.

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{showframe} % just for the example

\newcommand{\eqhide}[1]{%
  \omit\makebox[0pt][l]{$\displaystyle#1$}%
}

\begin{document}

\begin{alignat}{2}
\text{min:} \quad & \eqhide{\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\biggl(\,\sum_{j=1}^M S_j + T_j\biggr)} \\
\text{s.t.} \quad & \sum_{i=1}^N X_{i,j} + S_j - T_j = 1 \quad && j = 1 ,\dots, M \hspace{9em} \\
                  & S_j \in \mathbb{Z}^+                 \quad && j = 1, \dots, M \\
                  & T_j \in \mathbb{Z}^+                 \quad && j = 1, \dots, M
\end{alignat}

\end{document}

enter image description here

  • Wouldn't {alignat}{2} be enough? – Bernard Jan 5 '17 at 1:19
  • @Bernard Yes, I didn't count. – egreg Jan 5 '17 at 9:57
2

Here is a solution without \rlap, nor \omit, which induces the bounds of sums are types at the right side of the sym symbol, but with a better use of the &. B.t.w. please note 3 alignment points require 5&. I also propose to use the subequations` environment, to have the main equation number for the equation itself (1st line) and 3 subsidiary counters for the constraints:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{showframe}%
 \renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{amssymb}%
\usepackage{mathtools}

\begin{document}

\begin{subequations}
\begin{alignat}{3}
  \min &\colon & \quad \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) \tag{1} \\
  \text{s.t.} &\phantom{:} & & \sum_{i=1}^N X_{i,j} + S_j - T_j = 1 & & 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}
\end{subequations}

\end{document} 

enter image description here

  • I think the \colon is for maps only but I'm not sure. – Svend Tveskæg Jan 5 '17 at 0:59
  • Thanks Bernard. I like both solutions so far. This one is a lot more complicated than egreg's, but it avoids having to eyeball the width correction. I've seen \phantom before but don't really understand what's going on here. Can you please explain how this works? Also, the constraints are aligned to the sum in the top, whereas they should be aligned to the start, i.e. the alpha. I don't think it's standard practice to use this subequation notation? At least I have never seen it before. – Ozzah Jan 5 '17 at 1:08
  • I use it instead of a plain : just to be sure to not delete it unintentionally. But you may use the plain version as well, of course. – Bernard Jan 5 '17 at 1:09
  • @Ozzah: the \phantom is to have an alignment point similar the alignment point of the line above, without printing it. I aligned the constraints on the Σ because I found it more pleasing to the eye, but you may easily change it. As to the subequations notation, it's just a proposal. I think it makes more sense, semantically speaking: the constraints do not have the same rank as the equation itself (without the equation, they're meaningless. But it's only a personal opinion… – Bernard Jan 5 '17 at 1:16

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.