14

How to edit a linear programming as the following:

Max  z = x1 + 12x2
s.t.  3x1 + x2 + 12x3 ≤ 5
       x1      +   x3 ≤ 16
     15x1 + x2        = 14
     xj ≥ 0, j=1,2,3.

I want the format to be strictly like the example.

Thanks for all the answers below. What I need is just like the picture as following

enter image description here

4 Answers4

10

A regular array would suffice here, since the alignment is pretty rigid horizontally:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\[
  \begin{array}{r@{}r@{}r@{}l}
    \text{Max} \quad z=x_1 &{} + 12x_2 \\[\jot]
    \text{s.t.}\qquad 3x_1 &{} + \phantom{12}x_2 &{} + 12x_3 &{} \leq 5 \\
                      x_1 &         &{} +   \phantom{12}x_3 &{} \leq 16 \\
                    15x_1 &{} + \phantom{12}x_2 &           &{} = 14 \\
     \multicolumn{4}{c}{x_j \geq 0, \quad j=1,2,3.}
  \end{array}
\]
\end{document}

The use of \phantom is to allow for proper spacing and alignment.


Here might be another alignment option, mainly for the first column:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\[
  \begin{array}{ll@{}r@{}r@{}l}
    \text{Max} & z=x_1 &{} + 12x_2 \\[\jot]
    \text{s.t.}& \phantom{15}\llap{3}x_1 &{} + \phantom{12}x_2 &{} + 12x_3 &{} \leq 5 \\
    &    \phantom{15}x_1 &     &{} +   \phantom{12}x_3 &{} \leq 16 \\
    &              15x_1 &{} + \phantom{12}x_2 &           &{} = 14 \\
    & \multicolumn{4}{l}{x_j \geq 0, \quad j=1,2,3.}
  \end{array}
\]
\end{document}
David Carlisle
  • 757,742
Werner
  • 603,163
  • Thanks a lot, I can do it like this. But could you please make the alignment much closer to my example. That is, "15x1" should be left aligned to "z = x1 + 12x2" and "xj ≥ 0, j=1,2,3." ? – zijuexiansheng Oct 03 '12 at 04:15
  • In your example you're using a fixed-width font, making the width of z=x1 to be exactly the same as 15x1. So, left-aligning 15x1 withz=x1 will not make it seem right-aligned also. It's doable, but what do you then do with the two elements 3x1 and x1? Keep them right-aligned with z=x1? – Werner Oct 03 '12 at 04:20
  • Actually, if we regard "Max" ans "s.t." as the left side, the rest as the right side. What I need is that the right side has three blocks, which are "z = x1 + 12x2", "3x1 + x2 + 12x3 ≤ 5 ...... 15x1 + x2 = 14" and "xj ≥ 0, j=1,2,3". These 3 blocks should be left aligned. That's why I want "15x1" to be left aligned to "z" and "xj". And in the inner side of the middle block, I want the corresponding element to be aligned, i.e. "x1", "x2", and "x3" to be aligned. I'm sorry that I didn't make my question clear. – zijuexiansheng Oct 03 '12 at 04:29
  • @zijuexiansheng: I've added another alignment option. If this doesn't seem to adhere to your constraints, please include an image of your desired layout, perhaps created in Word if you don't have anything else to showcase it in. – Werner Oct 03 '12 at 04:48
6

Basically you have a text column, then repeated (math) columns where first one is right-aligned, then a relation column, then left aligned, and again relation column. Except the last line isn't aligned at all, and the width of the first line shouldn't be taken into account with the alignment of subsequent lines.

One way to do this is:

{\let\DS\displaystyle\mathsurround=0pt\openup\jot
\halign{\hfil#\hfil\enspace&& $\hfil\DS#$& ${}#{}$& $\DS{}#\hfil$& ${}#{}$\crcr
  Max & z = x_1 & + & 12x_2 \hidewidth\cr
  s.t.& 3x_1    & + & x_2 & + & 12x_3 & \leq & 5 \cr
      & x_1     &   &     & + & x_3   & \leq & 16 \cr
      & 15x_1   & + & x_2 &   &       & =    & 14 \cr
  \noalign{\smallskip $\DS x_j \geq 0, \quad j=1,2,3$.}
}}
\bye

enter image description here

But please note that this is plain TeX way; it should work with LaTeX as well (sans the \bye at the end), but is generally shunned upon.

morbusg
  • 25,490
  • 4
  • 81
  • 162
6

mathtools provides \mathrlap.
amsmath provides the alignat* environment.

I assume Max and s.t. are not variables, therefore I've typeset them in upright shape.
Lonely and empty {} are providing the right amount of spacing.
The macro \plus is just a shortcut for +{}.

Code

\documentclass{article}
\usepackage{amsmath,mathtools}
\newcommand*\plus{+{}}
\newcommand*\boxSizeOfMax[1]{\makebox[\widthof{Max}][c]{#1}}
\begin{document}

\begin{alignat*}{5}
           \text{Max} \quad & \mathrlap{z = x_1 + 12 x_2}   & & & & & & & & \\
  \boxSizeOfMax{s.t.} \quad & & 13 x_1 & \plus & x_2 & \plus & 12x_3 & \leq{}  &  5 & \\
                            & &    x_1 &       &     & \plus &   x_3 & \leq    & 16 & \\
                            & & 15 x_1 & \plus & x_2 &       &       & =       & 14 & \\
                            & \mathrlap{x_J\geq 0, j = 1, 2, 3.} & & & & & & & & 
\end{alignat*}
\end{document}

Output

Output

David Carlisle
  • 757,742
Qrrbrbirlbel
  • 119,821
  • Good use of \mathrlap since the operators in the first equation have no business being aligned with the rest. You’re right about the “variables”: “max” stands for “maximise” and “s.t.” for “subject to”. However, I’d left-align “s.t.” (even though OP didn’t). – Konrad Rudolph Oct 03 '12 at 10:29
  • @KonradRudolph Oh, I didn't even notice your updated examples. Then this is of course very similar. For the exception of italics vs upshape, I just mimicked the OP's picture. I also don't think that it would be wrong to align the “Max” equation in the same scheme (z being on the right side), but this is just a matter of style and can be easily adjusted, I guess. – Qrrbrbirlbel Oct 04 '12 at 01:05
5

You can use \begin{matrix} \end{matrix} too. Here's how your example will be typeset:

\documentclass{amsart}
\usepackage{amssymb}
\begin{document}
Maximize $z=x_1 + 12 x_2$ such that 
\[
\begin{matrix}
\phantom{15}x_1 +           &x_2&+&12x_3            &\leqslant 5 &           \\
\phantom{15}x_1 \phantom{+} &   &+&\phantom{12}x_3  &\leqslant 16&           \\
15x_1           +           &x_2& &                 &=         14&           \\
                            &   & & &x_j            &\geqslant 0 & j=1,2,3.  \\
\end{matrix}
\]

\end{document}

Output:

LP (Trying Hard!)


Attempt 2:

This time, I tried using alignat* as Peter Grill suggested below:

\begin{alignat*}{6}
  &x_1&{}+{}&x_2&{}+{}&12&&x_3   &&\leqslant 5  &      \\
  &x_1&     &   &{}+{}&  &&      &&\leqslant 16 &      \\
15&x_1&{}+{}&x_2&     &  &&      &&=14          &      \\
  &   &     &   &     &  &&x_3   &&\geqslant0   &\quad j=1,2,3.
\end{alignat*}

Output:

Another beautification!


Attempt 3 :(Just fitting in Peter Grill's suggestions)

\begin{alignat*}{7}
\text{Max}\quad\rlap{$z = x_1 + 12x_2$}                               \\               
\text{s.t.}\quad&13&x_1&{}+{}&x_2&{}+{}&12&&x_3   &&\;\leqslant &\;5  \\ 
                &  &x_1&     &   &{}+{}&  &&x_3   &&\;\leqslant &\;16 \\
                &15&x_1&{}+{}&x_2&     &  &&      &&\; =        &\;14 \\
                & \rlap{$x_j \geqslant 0,\; j=1,2,3.$}
\end{alignat*}

Awesome!

Thanks Peter for the suggestion. The output now looks much better.

kan
  • 4,545
  • But the math spacing is not quite correct around the binary/relational operators. – Peter Grill Oct 03 '12 at 04:34
  • It seems that your answer isn't like my example. Could you please look at Werner's answer and make it much closer to my example? I have been tried this for days but still in vain. Thanks. – zijuexiansheng Oct 03 '12 at 04:34
  • @PeterGrill I agree with you about the spacing between the operators. I'll think about how we can overcome that. – kan Oct 03 '12 at 04:37
  • 1
    @zijuexiansheng Commenting on specific aspects like Peter's comment can be much more helpful than your previous comment. Thank you. – kan Oct 03 '12 at 04:37
  • @KannappanSampath: Not sure how to do it with matrix, but perhaps with alignat? – Peter Grill Oct 03 '12 at 04:48
  • @PeterGrill Oh! I think that should work. I'll test it now. Thanks for the clue. :-) – kan Oct 03 '12 at 04:51
  • @PeterGrill I'd be glad to know how my second attempt has turned out to be? – kan Oct 03 '12 at 05:36
  • @KannappanSampath: Much better. Three suggestions: First line: \text{Max}\quad\rlap{$z = x_1 + 12x_2$}\\, Second line: \text{s.t.}\quad &13x_1&{}+{}&x_2&{}+{}&12&&x_3 &&\leqslant 5 & \\, and LastLine: & \rlap{$x_j \geqslant 0, j=1,2,3.$}. This should yield the desired output as per image in question. Oh and you'll need some tweaking now that I corrected this comment to include the 13. – Peter Grill Oct 03 '12 at 05:44