19

I want to write my linear program so that it looks like in this picture: integer program

meaning I want to have the following:

  • text aligned to the left
  • all the x_{j} variables to be one another
  • the i= and j= statement to be again to be one under the other

This is what I have so far:

\begin{equation}
\begin{split}
\text{minimize} \displaystyle\sum\limits_{j=1}^{m} w_{j}*&x_{j} \\
\text{subject to} \displaystyle\sum\limits_{j:e_{i} \in S_{j}} &x_{j} \geq 1, i=1 ,..., n\\
                                         &x_{j} \in \{0,1\}, j=1 ,..., m
\end{split}
\end{equation}

I tried using multiple &s, but either I use them and nothing changes, or I get the following error: Extra alingment tab has been changed to \cr \end{split}. I have searched this error and I know that it appeares if I forget to write a & or I have an extra one, but this is not my case. This is the code:

\begin{equation}
\begin{split}
&\text{minimize} \displaystyle\sum\limits_{j=1}^{m} w_{j}*&x_{j} & \\
&\text{subject to} \displaystyle\sum\limits_{j:e_{i} \in S_{j}} &x_{j} \geq 1, &i=1 ,..., n\\
&                                             &x_{j} \in \{0,1\}, &j=1 ,..., m
\end{split}
\end{equation}

How can I align them in the way that I want?

And also is it possible to do it without align? Because this program is at the end of my page and if I use align it gets placed on the next page and I don't want that.

Jessy09
  • 717
  • 1
    Does http://tex.stackexchange.com/questions/97942/using-an-array-environment-inside-an-xparse-command help? See also http://tex.stackexchange.com/questions/98141/a-macro-for-optimization-problems – egreg Jun 15 '14 at 09:37
  • I find no reason for having x_{j} aligned horizontally. – egreg Jun 15 '14 at 13:39
  • How do you think it should be aligned? – Jessy09 Jun 15 '14 at 14:42
  • I think it should be aligned to the left with the summation; what if the first condition were \sum a_jx_j? Would you align the x_j? – egreg Jun 15 '14 at 14:45
  • So you're saying that in the second line x_j should be next to the sum? And only the x_j in the 2nd and 3rd line should be one under the other, right? If yes, that's how they are aligned in the second answer. – Jessy09 Jun 15 '14 at 14:55
  • The conditions contain the same variables, but the alignment is on the conditions, not on the variables. But it's just my opinion. I wouldn't align the variables in any of the line, to be clear. – egreg Jun 15 '14 at 14:59

3 Answers3

20

Something like this. Use of array

enter image description here

Code

\documentclass[a4paper]{article}
\usepackage[paper size={10cm,5cm}]{geometry}
\pagestyle{empty}
\usepackage{amsmath,amssymb}
\begin{document}
\begin{equation*}
\begin{array}{ll@{}ll}
\text{minimize}  & \displaystyle\sum\limits_{j=1}^{m} w_{j}&x_{j} &\\
\text{subject to}& \displaystyle\sum\limits_{j:e_{i} \in S_{j}}   &x_{j} \geq 1,  &i=1 ,\dots, n\\
                 &                                                &x_{j} \in \{0,1\}, &j=1 ,\dots, m
\end{array}
\end{equation*}
\end{document}
JKHA
  • 473
Jesse
  • 29,686
15

The = signs are not quite vertically aligned. Here is a solution with alignat* that gives a correct alignment, and has a somewhat simpler syntax. I also propose a variant, where the sums are aligned, and the x_j are only partially aligned, that look better in my opinion:

\documentclass[a4paper]{article}
\usepackage[paper size={10cm,5cm}]{geometry}
\pagestyle{empty}
\usepackage{mathtools,amssymb}

\begin{document}

\begin{alignat*}{3}
 & \text{minimize} & \sum_{j=1}^{m} w_{j}x_{j}& \\
 & \text{subject to} \quad& \sum_{\mathclap{{j:e_{i} \in S_{j}}}}x_{j}& \geq 1, & i &=1 ,..., n\\
                 && x_{j}& \in \{0,1\},\quad & j &=1 ,..., m
\end{alignat*}

\begin{alignat*}{2}
  & \text{minimize: } & & \sum_{j=1}^{m} w_{j}x_{j} \\
   & \text{subject to: }& \quad & \sum_{\mathclap{{j:e_{i} \in S_{j}}}}\begin{aligned}[t]
                    x_{j} & \geq 1,& i & =1, \dots, n\\[3ex]
                  x_{j} & \in \{0,1\}, & \quad j &=1 ,\dots, m
                \end{aligned}
\end{alignat*}

\end{document} 

enter image description here

Variant: enter image description here

logstar
  • 257
  • 1
  • 6
Bernard
  • 271,350
  • You can use \mathclap for j:e_{i} \in S_{j}}. –  Jun 15 '14 at 13:16
  • @HarishKumar: Judicious remark. I've modified my code, but had to move the &x_j alignment point to & \geq (and similarly &\in). – Bernard Jun 15 '14 at 13:37
  • @HarishKumar: But if the Σ's align, there will be a gap between Σ and x_j in the second line! Or I didn't grasp well what you mean? – Bernard Jun 15 '14 at 13:52
  • @HarishKumar: Personally I would have aligned the Σ's and only the x_j's in the last two lines. But so required th O.P… – Bernard Jun 15 '14 at 13:55
  • @Bernard: When I wrote the question I didn't think about the \sum being aligned, I only wanted the text and the i= and j= to be aligned. But thank you for your response and I will consider aligning the \sum as well. – Jessy09 Jun 15 '14 at 14:40
  • @Jessy09: I'll add in a moment the variant as suggested in my previous comment – Bernard Jun 15 '14 at 14:50
  • ... should be \dots. – Svend Tveskæg Jun 15 '14 at 20:11
  • @SvendTveskæg: Oops ! That comes from the O.P.'s original code, but I didn't notice it, because my editor is configured to display \dots as . – Bernard Jun 15 '14 at 20:42
3

Using package you can obtain the same result like this:

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{optidef}

\begin{document} \begin{mini} {}{\sum_{j=1}^{m} w_{j}x_{j}}{}{} \addConstraint{\sum_{j:, e_{i} \in S_{j}} } {x_{j}\geq 1,}{}{\mkern53mu i=1 ,\ldots, n} \addConstraint{ }{x_{j}}{ \in {0,1},}{\quad j=1 ,\ldots, m} \end{mini} \end{document}

enter image description here

Sebastiano
  • 54,118