19

I know how to do this the hackish way. However, what would be a more mature and clean way to state a linear program, as here on Wikipedia? That is, an expression of the form:

minimize f(x)

subject to

g_i(x) <= 0, for i in {1,...,n}

h_j(x) = 0, for j in {1,...,m}

Jack
  • 193
  • Could you describe what you consider hackish? – Björn Pollex Jan 24 '11 at 20:14
  • Hackish is very ill-defined, I agree. What I mean is using the few keywords I know and small hacks to get something which looks sort of nice. This might (a) take much more time than needed and (b) be very cumbersome to edit. – Jack Jan 24 '11 at 20:55

2 Answers2

27

I would consider the code below as the common way to do that.

\documentclass[11pt,a4paper]{article}
\usepackage{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}

\begin{document}
  \begin{alignat*}{2}
    \text{minimize }   & \sum_{i=1}^m c_i x_i + \sum_{j=1}^n d_j t_j\  \\
    \text{subject to } & \sum_{i=1}^m a_{ij} + e_j t_j \geq g_j &,\ & 1\leq j\leq n\\
                       & f_i x_i + \sum_{j=1}^n b_{ij}t_j \geq h_i\ &,\ & 1\leq i\leq m\\
                       & x\geq 0,\ t_j\geq 0\ &,\ & 1\leq j\leq n,\ 1\leq i\leq m
  \end{alignat*}
\end{document}
David Carlisle
  • 757,742
  • Your solution is at least as good. Would like to mark both as correct. – Jack Jan 24 '11 at 22:09
  • Thorsten Donig's solution is actually much more flexible than the current "best" answer, since you are not constrained (as in the definition of \linprog) to have a fixed number of constraint equations. That is, with Thorsten's answer you can simply add more lines in the body of the alignat* if you need more. –  May 16 '12 at 18:23
  • @Johnny: Welcome to TeX.SE. Note that this is not a forum and answer posts are only for solutions to the answer. I converted your post to a comment now. You can't post comments everywhere or up-vote posts yet. This requires reputations points first. See the FAQ and About links on the very top of this page for more information. – Martin Scharrer May 16 '12 at 19:03
  • I came to post this method (which is what I use), but Thorsten did it first and did it very well! – JohnD May 16 '12 at 19:09
  • Thank you, this helped me a lot! For what it's worth: in case of having an equal sign in the goal function, I always add a \quad after the first ampersand in the lines below. – henry Jun 20 '13 at 08:58
7

This is a short definition I sometimes use in my articles. More like a boilerplate, and you need to change some stuff if you happen to have more constraints. The \eqnlimit macro is a shorthand for index ranges.

\documentclass{article}
\usepackage[sumlimits]{amsmath}
\usepackage{xargs}

\newcommandx*\eqnlimit[3][1=1, 3=n]{\ensuremath{#1 \leq #2 \leq #3}}

\newcommand{\linprog}[6]{
    \begin{alignat}{5}
          \label{#6-1}
          \min        & \quad #1 & \\
          \label{#6-2}
          \text{s.t.} & \quad #2 &, & \quad #3\\
          \label{#6-3}
                      & \quad #4 &, & \quad #5
    \end{alignat}}

\begin{document}
    \linprog{f(x)}{g_i(x) \leq 0}{\eqnlimit[1]{i}[n]}{h_j(x) = 0}{\eqnlimit[1]{j}[m]}{eq:linprog1}
    \linprog{f(x)}{g_i(x) \leq 0}{\eqnlimit[1]{i}[n]}{h_j(x) = 0}{\eqnlimit[1]{j}[m]}{eq:linprog2}
    \\
    First linear program\\
    This is the objective: (\ref{eq:linprog1-1}), the inequality constraint set: (\ref{eq:linprog1-2}), and the equality constraint set(\ref{eq:linprog1-3}).
    \\ \\
    Second linear program\\
    This is the objective: (\ref{eq:linprog2-1}), the inequality constraint set: (\ref{eq:linprog2-2}), and the equality constraint set(\ref{eq:linprog2-3}).
\end{document}

Thanks to Caramdir for the help with the labelling.

David Carlisle
  • 757,742