2

I want to formulate a linear program but I have not been able to find exactly what I am looking for. It should look similar to this picture:

enter image description here

The main difference is that I want automatic numbering on right side (like an equation) and a label on the left side i.e. (LP)

I need at least 5 columns (+numbering)

With what should I start array, equation, alignat, or align? How can achieve this?

Endresult This is the final code (a modified version of Werner's code). I wrote some new commands, now everybody can use these easy commands without caring about proper alignment. Thank you Werner.

\documentclass{article}
\usepackage[a4paper,margin=1in]{geometry}
\usepackage{mathtools,zref-savepos}

% Linear Programm Tex Example 
\newcommand{\objective}[2] { & \text{#1} \quad \mathrlap{#2}}
% includes s.t. in front
\newcommand{\stconstraint}[4] { & \text{s.t.} & #1 & \quad #2 \quad #3 && \quad  #4}
\newcommand{\constraint}[4] { & & #1 & \quad #2 \quad #3  && \quad #4}

\newcommand{\lpnamereset}{\noindent\zsavepos{text-left-margin}}
\newcommand{\lpnamebegin}{\zsavepos{top-lp}}
\newcommand{\lpnameend}[1]{ \zsavepos{bottom-lp}
    \raisebox{0.5\dimexpr\zposy{top-lp}sp-\zposy{bottom-lp}sp}[0pt][0pt]{%
      \makebox[0pt][r]{\rlap{(#1)}\hspace*{\dimexpr\zposx{bottom-lp}sp-\zposx{text-left-margin}sp}}}}
 % End Linear Programm Tex Example      


\begin{document}

\setlength{\jot}{10pt}
\lpnamereset
\begin{alignat}{4}
 \lpnamebegin
 \objective{min}{\sum_{a \in A} k_a u_a + \sum_{r \in R} \sum_{a \in A} f_a^r y_a^r}  \\
 \stconstraint{\smashoperator{\sum_{a \in \delta_i^+}} y_a^r - \smashoperator{\sum_{a \in \delta_i^-}} y_a^r}{=}{\left\{\begin{array}{@{}rl@{}}
       1, & \text{falls $i = O^r$} \\
      -1, & \text{falls $i = D^r$} \\
       0, & \text{sonst} \\
    \end{array}\right.}{(\forall r \in R)(\forall i \in V)} \\
    \constraint{y_a^r}{\leq}{ u_a}{ (\forall r \in R)(\forall a \in A)} \\
    \constraint{u_a}{\in}{\{0,1\}}{(\forall a \in A)} \\
    \constraint{y_a^r}{\in}{[0,1}{(\forall r \in R)(\forall a \in A)} 
     \lpnameend{LP}
\end{alignat}

\end{document}
jGaboardi
  • 502
  • 1
    A good place to start would be the mathmode documentation, which should be available as part of your TeX distribution. It has lots of useful examples and discussion of best practices. – Alan Munn Jan 13 '15 at 16:57
  • There are several questions on the site that deal with the problem. – egreg Jan 13 '15 at 18:56
  • Related: http://tex.stackexchange.com/questions/125820/how-can-i-align-the-terms-of-this-equation-nicely http://tex.stackexchange.com/questions/19465/is-there-a-package-for-specifying-optimization-problems http://tex.stackexchange.com/questions/213998/align-text-in-equation http://tex.stackexchange.com/questions/160789/formatting-linear-programs http://tex.stackexchange.com/questions/75108/how-to-edit-the-linear-programming-in-latex – egreg Jan 13 '15 at 19:07
  • Note that if you're using the code to generate multiple LPs, the macros using zref-savepos will also have to be updated to use a counter, otherwise you'll obtain multiply-defined labels. – Werner Jan 13 '15 at 22:17
  • what do you mean, if a test two different LPs it works? they have different names – user3613886 Jan 13 '15 at 22:56

3 Answers3

4

I've written a package called lpform which does the formatting and labelling automatically.

https://www.ctan.org/pkg/lpform

Example:

An example of the lpform package in LaTeX

Marijn
  • 41
  • 1
3

The setup for the linear program is straight-forward using alignat. It's the placement of (LP) on the left that is more difficult if you wish to also include the auto-numbered equation-style provided by alignat. For this I've used the savepos module from zref to store the horizontal position of certain elements, and then move the label (LP) into position; horizontally against the left margin (using text-left-margin as reference); vertically halfway between the first line/top-lp and last/bottom-lp):

enter image description here

\documentclass{article}

\usepackage[a4paper,margin=1in]{geometry}
\usepackage{mathtools,zref-savepos}

\begin{document}

\noindent\zsavepos{text-left-margin}%
Here is some text.

%\setlength{\jot}{10pt}
\begin{alignat}{4}
  & \zsavepos{top-lp}\text{min} \quad \mathrlap{\sum_{a \in A} k_a u_a + \sum_{r \in R} \sum_{a \in A} f_a^r y_a^r} \\
  & \text{s.t.} \quad & \smashoperator{\sum_{a \in \delta_i^+}} y_a^r - \smashoperator{\sum_{a \in \delta_i^-}} y_a^r
    &= \left\{\begin{array}{@{}rl@{}}
       1, & \text{falls $i = O^r$} \\
      -1, & \text{falls $i = D^r$} \\
       0, & \text{sonst}
    \end{array}\right. & \quad & (\forall r \in R)(\forall i \in V) \\
  & & y_a^r &\leq u_a && (\forall r \in R)(\forall a \in A) \\
  & &   u_a &\in \{0,1\} && (\forall a \in A) \\
  & & y_a^r &\in [0,1] && (\forall r \in R)(\forall a \in A)\zsavepos{bottom-lp}
    \raisebox{0.5\dimexpr\zposy{top-lp}sp-\zposy{bottom-lp}sp}[0pt][0pt]{%
      \makebox[0pt][r]{\rlap{(LP)}\hspace*{\dimexpr\zposx{bottom-lp}sp-\zposx{text-left-margin}sp}}}
\end{alignat}

\end{document}

Since zref uses (La)TeX's \label-\ref system you need to compile at least twice on the first go. Thereafter, with an intact .aux, things should run smoothly.

Werner
  • 603,163
  • this looks fantastic. Thank you! The columns have more vertical empty space between each other! Is there a general property you can modify like the $jot$ for the height? – user3613886 Jan 13 '15 at 21:02
  • @user3613886: alignat sets its contents next to one another without any space in between (see How to write a mathematical formula in 2 lines?). If you want space, you should insert this manually wherever needed, typically in the entry longest entry in that particular column. – Werner Jan 13 '15 at 21:05
0

A way of writing explicit systems of equations (or inequalities) is given by the systeme package. It's documentation is in French, and how to get some stuff done is far from intuitive. But it works fine. Try:

\begin{equation}
    \sysdelim..  % No '{' or other decorations
    \systeme[x_1x_2s_1s_2s_3z]{  % Order for the variables
        x_1 +   x_2 + s_1     = \phantom{0}9,
        x_1 + 3 x_2 + s_2     = 12,
      - x_1 + 2 x_2 + s_3     = \phantom{0}2,
      - x_1 - 2 x_2       + z = \phantom{0}0
    }
TeXnician
  • 33,589
vonbrand
  • 5,473