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}

2
  • Could you describe what you consider hackish? 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
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.

6
  • Thanks. Can one also easily label the conditions "\quad #2 &, & \quad #3\\" and "\quad #4 &, & \quad #5\\" in order to refer to them later on?
    – Jack
    Jan 24 '11 at 21:06
  • 1
    @Jack: Indeed. See revised version. Jan 24 '11 at 21:25
  • 2
    @Martin: this is not going to work if you have more than one linear program.
    – Caramdir
    Jan 24 '11 at 21:29
  • @Caramdir: Ahhhhh yes, of course you're right. I'm open to suggestions on this then, I'll revert to the previous version. Jan 24 '11 at 21:33
  • 1
    The only thing I can think of is to add another parameter for the label.
    – Caramdir
    Jan 24 '11 at 21:48
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}
5
  • 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.
    – user14666
    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. 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 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.