TransWikia.com

Formatting linear programs

TeX - LaTeX Asked by Mathias Vorreiter Pedersen on April 30, 2021

When stating LP problems I usually use something along the following

begin{tabular}{ l c p{1pt} c }
max     & multicolumn{3}{l}{$300x + 100y$} 
s.t.    & $6x + 3y$ & $leq$ & $40$
        & $x - 3y$  & $leq$ & $0$ 
        & $x + frac{1}{4}y$ & $leq$ & $4$ 
end{tabular}

But this forces the use of inline math, and require me to adjust column width manually (and it’s still not perfect since the spacing between the right hand side and left hand side of leq is wrong).

What I really would like was a solution that allowed for display math, with correct spacing and no need for manual formatting with p{}.

How would I write a version that fixes this?

4 Answers

To get the wider spacing associated with display-style math, you could use a combination of the gather* and aligned environments (both provided by the amsmath package):

enter image description here

documentclass{article}
usepackage{amsmath}
begin{document}
begin{gather*}
max_{x,y}quad 300x + 100y 
begin{aligned}
textup{s.t.}quad 6x + 3y  &leq  40
                   x - 3y  &leq  0 
         x + tfrac{1}{4}y  &leq  4 
end{aligned}
end{gather*}
end{document}

Correct answer by Mico on April 30, 2021

Using the array environment, you can do as follows:

documentclass{article}

usepackage{amsmath}

begin{document}

begin{equation*}
setlengtharraycolsep{1.5pt}
  begin{array}{l@{quad} r c r c r}
    max          & 300x & + &         100y &      &    
    mathrm{s.t.} &   6x & + &           3y & geq & 40 
                  &    x & - &           3y & geq &  0 
                  &    x & + & frac{1}{4}y & geq &  4
  end{array}
end{equation*}

end{document}

output

This gives a proper alignment IMHO.

Answered by Svend Tveskæg on April 30, 2021

The gather environment is working well in this example only because the objective function and the constraints have similar sizes.

If you don't need to align the objective function with your constraints, you can nest an array in an align environment:

documentclass{article}
usepackage{amsmath}
usepackage{mathtools,amssymb}

begin{document}

begin{align*}
    &mathrm{max} quad 300x + 100y  [2pt] % if you need space between objectif and constraints
    &mathrm{s.t.} quad 
    begin{array}[t]{r c r c r}
         6x & + &           3y & geq & 40 
          x & - &           3y & geq &  0 
          x & + & frac{1}{4}y & geq &  4
     end{array}
end{align*}

end{document}

enter image description here

I have noticed that the max command does not align very well in the align environment, I have replaced it by mathrm{max}.

Answered by gdejaege on April 30, 2021

This is an old question but since of april 23, 2019, exists also a specific package for the formatting the linear programs named optidef, very fast and simple.

I have put the parameter |s| (to write only max and not maximize), and the option <b> it is uses for to create a break on the objective to have a blank space. Here there is a MWE:

documentclass[a4paper,12pt]{article}
usepackage{amsmath,amssymb}
usepackage{optidef}


begin{document}
begin{maxi*}|s|<b>
{scriptstyle x,y}{300x + 100y}{}{}%%%% <----objective
addConstraint {6x+3y}{leq 40}{}
addConstraint {x-3y}{leq  0}{}
addConstraint {x+tfrac{1}{4} y}{leq  4}{}
end{maxi*}
end{document}

enter image description here

Answered by Sebastiano on April 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP