Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
2  
Welcome to TeX.sx! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Such questions tend to get closed because they are "too localized". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. –  henrique Oct 3 '12 at 4:04
    
Related Question: Stating a linear program. –  Peter Grill Oct 3 '12 at 4:33
    
add comment

4 Answers

up vote 10 down vote accepted

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}
share|improve this answer
    
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 3 '12 at 4: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 with` z=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 3 '12 at 4: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 3 '12 at 4: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 3 '12 at 4:48
    
Thank you very much. That's exactly what I need. –  zijuexiansheng Oct 3 '12 at 4:51
add comment

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.

share|improve this answer
    
But the math spacing is not quite correct around the binary/relational operators. –  Peter Grill Oct 3 '12 at 4: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 3 '12 at 4:34
    
@PeterGrill I agree with you about the spacing between the operators. I'll think about how we can overcome that. –  kan Oct 3 '12 at 4:37
1  
@zijuexiansheng Commenting on specific aspects like Peter's comment can be much more helpful than your previous comment. Thank you. –  kan Oct 3 '12 at 4:37
    
@KannappanSampath: Not sure how to do it with matrix, but perhaps with alignat? –  Peter Grill Oct 3 '12 at 4:48
show 3 more comments

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

share|improve this answer
    
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 3 '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 4 '12 at 1:05
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.