Formatting linear programs

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?

56.8k 17 17 gold badges 87 87 silver badges 197 197 bronze badges asked Feb 16, 2014 at 22:35 Mathias Vorreiter Pedersen Mathias Vorreiter Pedersen 497 4 4 silver badges 12 12 bronze badges

Something like an array environment would probably suffice here (see the first answer on this question; I don’t have time for a full answer right now).

Commented Feb 16, 2014 at 22:41 A good thing is to read the fine document MathMode.pdf of Herbert Voß. Commented Feb 16, 2014 at 22:56

4 Answers 4

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 \usepackage \begin \begin \max_\quad 300x + 100y \\ \begin \textup\quad 6x + 3y &\leq 40\\ x - 3y &\leq 0 \\ x + \tfracy &\leq 4 \\ \end \end \end
answered Feb 16, 2014 at 22:59 519k 56 56 gold badges 708 708 silver badges 1.3k 1.3k bronze badges I love it! Even better than I could've imagined! Commented Feb 16, 2014 at 23:19

Using the array environment, you can do as follows:

\documentclass \usepackage \begin \begin \setlength\arraycolsep \begin r c r c r> \max & 300x & + & 100y & & \\ \mathrm & 6x & + & 3y & \geq & 40 \\ & x & - & 3y & \geq & 0 \\ & x & + & \fracy & \geq & 4 \end \end \end

output

This gives a proper alignment IMHO.

answered Feb 16, 2014 at 23:02 Svend Tveskæg Svend Tveskæg 31.4k 18 18 gold badges 81 81 silver badges 191 191 bronze badges

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 it is uses for to create a break on the objective to have a blank space. Here there is a MWE:

\documentclass[a4paper,12pt] \usepackage \usepackage \begin \begin|s| <><>%%%% <\leq 40><> \addConstraint <\leq 0><> \addConstraint  y><\leq 4><> \end \end

enter image description here

answered Feb 5, 2021 at 13:10 Sebastiano Sebastiano 56.8k 17 17 gold badges 87 87 silver badges 197 197 bronze badges

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 \usepackage \usepackage \begin \begin &\mathrm \quad 300x + 100y \\[2pt] % if you need space between objectif and constraints &\mathrm \quad \begin[t] 6x & + & 3y & \geq & 40 \\ x & - & 3y & \geq & 0 \\ x & + & \fracy & \geq & 4 \end \end \end

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 .