[Tex/LaTex] How to edit the linear programming in LaTeX

horizontal alignmentmath-mode

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

Best Answer

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}