4

I'm trying to typeset linear programs:

\usepackage{amsmath,amsthm,amssymb}
\usepackage{array}

\begin{document}

\begin{align*}
    \text{maximize}\quad   3x + 2y &+ 3x + 2y \\
    \text{subject to}\quad 2x + y  &\leq 18 \\
                           2x + 3y &\leq 42 \qquad\forall x\\
                           3x + y  &\leq 2 \qquad\forall y\\
                           x, y    &\geq 0
\end{align*}

\begin{center}
    \begin{tabular}{r>{\(\displaystyle}r<{\)}@{\(\;\)}>{\(\displaystyle}c<{\)}@{\(\;\)}>{\(\displaystyle}l<{\)}>{\(\displaystyle}l<{\)}}
        maximize  &\multicolumn{4}{>{\(\displaystyle}l<{\)}}{3x + 2y + 3x + 2y}\\
        subject to& 2x + y  & \leq & 18 \\
                  & 2x + 3y & \leq & 42 & \forall x\\
                  & 3x + y  & \leq & 2 & \forall y\\
                  & x, y    & \geq & 0  \\
    \end{tabular}
\end{center}

\end{document}

enter image description here

tabular is easier to get aligned properly, but as you can see, it's more compact than align.

I'd like to get the same vertical spacing (or interline) as in display math for tables. I've found the \arraystretch property, but I don't know what value I need to put.

mrBen
  • 165

3 Answers3

5

Alignments increase the line spacing by the amount 1\jot so

enter image description here

\documentclass{article}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{array}

\begin{document}

\begin{align*}
    \text{maximize}\quad   3x + 2y &+ 3x + 2y \\
    \text{subject to}\quad 2x + y  &\leq 18 \\
                           2x + 3y &\leq 42 \qquad\forall x\\
                           3x + y  &\leq 2 \qquad\forall y\\
                           x, y    &\geq 0
\end{align*}

\begin{center}

\openup\jot
\setbox\strutbox\hbox{\vrule height.7\baselineskip depth.3\baselineskip width 0pt}

    \begin{tabular}{r>{\(\displaystyle}r<{\)}@{\(\;\)}>{\(\displaystyle}c<{\)}@{\(\;\)}>{\(\displaystyle}l<{\)}>{\(\displaystyle}l<{\)}}
        maximize  &\multicolumn{4}{>{\(\displaystyle}l<{\)}}{3x + 2y + 3x + 2y}\\
        subject to& 2x + y  & \leq & 18 \\
                  & 2x + 3y & \leq & 42 & \forall x\\
                  & 3x + y  & \leq & 2 & \forall y\\
                  & x, y    & \geq & 0  \\
    \end{tabular}

\end{center}

\end{document}
David Carlisle
  • 757,742
3

I'd suggest looking into using alignat* (the * suppresses the numbering). One writes this as \begin{alignat*}{ncols}, where ncols is the number of columns you wish to align.

See Sections 3.3 to 3.7 in the amsmath documentation. You can also read about it on TeX.SE questions, for example in an egreg answer to this question.

I'm far from an expert, so can't really advise much. I find it takes a bit of playing with to get the correct justification (right/left) and positioning, but once it's right it does look good! Maybe someone who is better with LaTeX will post a similar, but more embellished, version of this solution...

schtandard
  • 14,892
Sam OT
  • 1,329
  • I should have mention that I've already try it. I prefer the tabular because I can just wrap it in an environment and then I don't have a bunch of && or &&& in my code. – mrBen Jun 07 '19 at 08:59
  • Personally I think the alignat* solution (see @Bernard) is much more elegant. Your document, your choice! :) – Sam OT Jun 07 '19 at 10:32
  • @Bernard also mention the optidef package that I didn't know, and that I will probably use ;) – mrBen Jun 07 '19 at 12:37
3

You have a dedicated package (optidef) for optimisation problems. You also can easily align it with the alignat* environment. Here are both solutions:

\documentclass{article}
\usepackage{amsmath}
\usepackage{optidef}

\begin{document}

\begin{alignat*}{3}
     & \text{maximize} & \quad 3x + 2y &+ 3x + 2y \\
    & \text{subject to} & 2x + y &\leq 18 \\
       & & 2x + 3y &\leq 42 & & \forall x\\
         & & 3x + y &\leq 2 & & \forall y\\
          & & x, y &\geq 0
\end{alignat*}

\begin{maxi*}
  {}{3x + 2y + 3x + 2y}{}{}
\addConstraint{2x + y }{\leq 18}
\addConstraint{2x +3y }{\leq 42}{\qquad\forall x}
\addConstraint{3x + y }{\leq 2}{\qquad\forall y}
\addConstraint{x, y }{\geq 0}
\end{maxi*}

\end{document} 

enter image description here

Bernard
  • 271,350