summaryrefslogtreecommitdiff
path: root/macros/generic/lpform/lpform.sty
blob: 59760c4e81c29f632bca21d6f14c785a418312e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lpform}[2015/04/18]

\usepackage{xifthen}

\makeatletter

\newcounter{subcounter} % This is the counter for the sub-equations
\newcounter{eqcounter} % This counts the number of equations in the lpformulation environment

% Creating custom labels
\newcommand{\customlabel}[2]{\protected@write \@auxout{}{\string \newlabel {#1}{{#2}{}}}}

% The name of the formulation
\newcommand{\lpname}{}

% The lpformulation environment
\newenvironment{lpformulation}[1][]{\setcounter{eqcounter}{0}\renewcommand{\lpname}{#1} \start@align\tw@\st@rredfalse\m@ne \ifthenelse{\isempty{#1}}{}{\lpname}}{\endalign}

% The method that is used to display the for all arguments
\newcommand{\lpforall}[1]{\quad \forall #1}

% The size of the left indent
\newcommand{\lpindent}{\hspace{20pt}}

% The method that is used to tag subequations
\newcommand{\lpsubeqtag}{\theequation\alph{subcounter}}

% The text that is shown on the 'subject to' line
\newcommand{\lpsubjectto}{s.t.}

% Labeling the lp formulation
\newcommand{\lplabel}[1]{\customlabel{#1}{\lpname}}

% Splitting one equation into two lines
\newcommand{\lpnewline}{\nonumber\\&\lpindent}

% \lpobj(*)[label]{min/max}{objective function}
\def\lpobj{\@ifstar\@@lpobj\@lpobj}
\newcommand{\@lpobj}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\textrm{#2 } #3 \ifthenelse{\isempty{#1}}{}{\label{#1}}&\\&\lpindent\nonumber\textrm{\lpsubjectto}&}
\newcommand{\@@lpobj}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\textrm{#2 } #3 \nonumber \ifthenelse{\isempty{#1}}{}{\label{#1}}&\\&\lpindent\nonumber\textrm{\lpsubjectto}&}

% \lpeq(*)[label]{constraint}{for all}
\def\lpeq{\@ifstar\@@lpeq\@lpeq}
\newcommand{\@lpeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
\newcommand{\@@lpeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \nonumber\ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}

% \lpnewsubeq(*)[label]{constraint}{for all}
\def\lpnewsubeq{\@ifstar\@@lpnewsubeq\@lpnewsubeq}
\newcommand{\@lpnewsubeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{equation}\setcounter{subcounter}{1} #2 \ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\tag{\lpsubeqtag}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
\newcommand{\@@lpnewsubeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{equation}\setcounter{subcounter}{0} #2 \nonumber\ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}

% \lpsubeq(*)[label]{constraint}{for all}
\def\lpsubeq{\@ifstar\@@lpsubeq\@lpsubeq}
\newcommand{\@lpsubeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{subcounter} #2 \ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\tag{\lpsubeqtag}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
\newcommand{\@@lpsubeq}[3][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \nonumber\ifthenelse{\isempty{#3}}{}{\lpforall{#3}}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}

% Using the noforall option
\DeclareOption{noforall}{
	\renewcommand{\@lpeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \ifthenelse{\isempty{#1}}{}{\label{#1}}&}
	\renewcommand{\@@lpeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \nonumber\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
	\renewcommand{\@lpnewsubeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{equation}\setcounter{subcounter}{1} #2 \tag{\lpsubeqtag}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
	\renewcommand{\@@lpnewsubeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{equation}\setcounter{subcounter}{0} #2 \nonumber\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
	\renewcommand{\@lpsubeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent\refstepcounter{subcounter} #2 \tag{\lpsubeqtag}\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
	\renewcommand{\@@lpsubeq}[2][]{\ifthenelse{\value{eqcounter} = 0}{}{\displaybreak[0]\\}\stepcounter{eqcounter}&\lpindent #2 \nonumber\ifthenelse{\isempty{#1}}{}{\label{#1}}&}
}

% Using the alignedforall option
\DeclareOption{alignedforall}{
	\renewcommand{\lpforall}[1]{&& \forall #1}
}

\ExecuteOptions{}
\ProcessOptions\relax

\makeatother

\endinput