17

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.

6
  • 1
  • I find no reason for having x_{j} aligned horizontally.
    – egreg
    Jun 15, 2014 at 13:39
  • How do you think it should be aligned?
    – Jessy09
    Jun 15, 2014 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, 2014 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, 2014 at 14:55

3 Answers 3

17

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}
3
  • Yes. This is what I was looking for. Thank you!
    – Jessy09
    Jun 15, 2014 at 9:45
  • 1
    ... should be \dots. Jun 15, 2014 at 20:11
  • 1
    In array environment, lines are vertically too near, which is sometimes ugly for mathematical equations.
    – xskxzr
    Jun 5, 2019 at 7:15
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

8
  • You can use \mathclap for j:e_{i} \in S_{j}}.
    – user11232
    Jun 15, 2014 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, 2014 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, 2014 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, 2014 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, 2014 at 14:40
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

1
  • 2
    This worked beatifully Jan 25, 2021 at 13:42

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.