summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/optidef/optidef.sty
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2016-06-27 20:38:25 +0000
committerKarl Berry <karl@freefriends.org>2016-06-27 20:38:25 +0000
commit30396a603a178a5cf062125e543547313701f87a (patch)
tree647b0cb57171ec576ce3b50d60c208fe1c17d810 /Master/texmf-dist/tex/latex/optidef/optidef.sty
parente747f7b4250571d8894112eb0d47e37ae61928c9 (diff)
optidef (27jun16)
git-svn-id: svn://tug.org/texlive/trunk@41551 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex/optidef/optidef.sty')
-rw-r--r--Master/texmf-dist/tex/latex/optidef/optidef.sty471
1 files changed, 378 insertions, 93 deletions
diff --git a/Master/texmf-dist/tex/latex/optidef/optidef.sty b/Master/texmf-dist/tex/latex/optidef/optidef.sty
index 9967b8919ad..b3115bbf9a9 100644
--- a/Master/texmf-dist/tex/latex/optidef/optidef.sty
+++ b/Master/texmf-dist/tex/latex/optidef/optidef.sty
@@ -1,4 +1,4 @@
-% optidef - Version 1.0
+% optidef - Version 1.1
%
%Copyright 2016 J. Lago Garcia
%
@@ -12,54 +12,368 @@
%This work consists of the file optidef.sty.
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{optidef}[2016/06/06 Package for defining optimization problems]
+\ProvidesPackage{optidef}[2016/06/26 - version=1.1, Package for defining optimization problems]
\RequirePackage{environ}
\RequirePackage{mathtools}
\RequirePackage{xifthen}
\RequirePackage{etoolbox}
+\RequirePackage{xparse}
+\RequirePackage{multirow}
+
+% Toogle to indicate if during the addConstraint command the first constraint should be built together with "subject to"
\newtoggle{bodyCon}
\toggletrue{bodyCon}
-% Input minimization evironments
+% If the previous constraints has 3 elements, we avoid setting \span\span at the beginning of the next constraint. If there is no previous third element, \span\span must be included for correct alignment
+\newtoggle{previousThird}
+\togglefalse{previousThird}
+\newcommand{\spanit}{}
+
% Macros for objective definition, constraint definition and extra constraint definition
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% % -------- DEFINITION COMMAND OBJECTIVE -----------
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\bodyobj}[4]
{
\ifthenelse{\isempty{#4}}
{
- &\underset{\displaystyle #1}{\mbox{#3}} \quad #2\span\span\span\span
+ &\underset{\displaystyle #1}{\mathrm{#3}} \quad #2\span\span\span\span
}
{
- #4 ~~ &\underset{\displaystyle #1}{\mbox{#3}} \quad #2\span\span\span\span
+ #4 ~~ &\underset{\displaystyle #1}{\mathrm{#3}} \quad #2\span\span\span\span
}
}
-\newcommand{\bodyconst}[2]
-{\\ &\text{subject to} \quad &#1 & #2}
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% -------- DEFINITION DIFFERENT TYPE OF BODY CONSTRAINTS --------%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\newcommand{\bodyconstRight}[2]
-{\\ &\text{subject to} \quad &#1 & #2}
+\DeclareDocumentCommand{\bodyconst}{m G{}}
+{ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \quad &&#1 #2
+ }{
+ \\ &\mathrm{subject~to} \quad &#1 & #2
+ }
+}
-\newcommand{\bodyconstBelow}[2]
-{\\ &\text{subject to} \span\span \\
- &\quad&#1 & #2}
+% Standard version
+\DeclareDocumentCommand{\bodyconstRight}{m G{} G{}}
+{ \ifthenelse{\equal{#3}{}}{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \quad &&#1 #2
+ }{
+ \\ &\mathrm{subject~to} \quad &#1 & #2
+ }
+ \togglefalse{previousThird}
+ }{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \quad &&#1 #2 &&#3
+ }{
+ \\ &\mathrm{subject~to} \quad &#1 & #2 &&#3
+ }
+ \toggletrue{previousThird}
+ }
+}
-\newcommand{\bodyconstBelowMult}[2]
-{\\ &\text{subject to} \span\span\span\span \nonumber \\
- &\quad&#1 & #2}
+% Single alignment point
+\DeclareDocumentCommand{\bodyconstOneAlign}{m G{} G{}}
+{
+ \ifthenelse{\equal{#3}{}}{
+ \\ &\mathrm{subject~to} \quad &&#1 #2 \togglefalse{previousThird}
+ }{
+ \\ &\mathrm{subject~to} \quad &&#1 #2 &&#3
+ \toggletrue{previousThird}
+ }
+}
-\newcommand{\addConstraint}[2]{
+% Contraints below subject to and with a single alignment point
+\DeclareDocumentCommand{\bodyconstOneAlignBelow}{m G{} G{}}
+{
+ \ifthenelse{\equal{#3}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&&#1 #2 \togglefalse{previousThird}
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&&#1 #2 &&#3
+ \toggletrue{previousThird}
+ }
+}
+
+% Contraints below subject to
+\DeclareDocumentCommand{\bodyconstBelow}{m G{} G{}}
+{
+ \ifthenelse{\equal{#3}{}}{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&&#1 #2
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&#1 & #2
+ }
+ \togglefalse{previousThird}
+ }{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&&#1 #2 &&#3
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \\
+ &&#1 & #2 &&#3
+ }
+ \toggletrue{previousThird}
+ }
+}
+
+% Contraints below subject to for the case of having multiple equations
+\DeclareDocumentCommand{\bodyconstBelowMult}{m G{} G{}}
+{
+ \ifthenelse{\equal{#3}{}}{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&&#1 #2
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&#1 & #2
+ }
+ \togglefalse{previousThird}
+ }{
+ \ifthenelse{\equal{#2}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&&#1 #2 &&#3
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&#1 & #2 &&#3
+ }
+ \toggletrue{previousThird}
+ }
+}
+
+% Contraints below subject to and with a single alignment point for the case of having several equations
+\DeclareDocumentCommand{\bodyconstOneAlignBelowMult}{m G{} G{}}
+{
+ \ifthenelse{\equal{#3}{}}{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&&#1 #2 \togglefalse{previousThird}
+ }{
+ \\ &\mathrm{subject~to} \span\span\span\span \nonumber \\
+ &&&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+}
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% -------- DEFINITION DIFFERENT TYPE OF ADDING CONSTRAINTS --- --%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\DeclareDocumentCommand{\addConstraint}{m G{} G{}}{
+\ifthenelse{\equal{#3}{}}{
+ \ifthenelse{\equal{#2}{}}{
+ \iftoggle{bodyCon}{
+ \bodyconst{#1}
+ \togglefalse{bodyCon}
+ }{
+ ,\\&\quad &&#1 #2\span\span
+ \togglefalse{bodyCon}
+ }
+ }{
+ \iftoggle{bodyCon}{
+ \bodyconst{#1}{#2}
+ \togglefalse{bodyCon}
+ }{
+ ,\\&\quad &#1 & #2\span\span
+ \togglefalse{bodyCon}
+ }
+ }
+ \togglefalse{previousThird}
+ }{
+ \iftoggle{bodyCon}{
+ \bodyconst{#1}{#2}{#3}
+ \togglefalse{bodyCon}
+ }{
+ \ifthenelse{\equal{#2}{}}{
+ ,\\&\quad &&#1 #2 && #3
+ }{
+ ,\\&\quad &#1 & #2 && #3
+ }
+ \togglefalse{bodyCon}
+ }
+ \toggletrue{previousThird}
+ }
+}
+
+% Standard version of adding constraints
+\DeclareDocumentCommand{\standardAddConstraint}{m G{} G{}}{
+ \iftoggle{previousThird}
+ {\renewcommand{\spanit}{}}
+ {\renewcommand{\spanit}{\span\span}}
+\iftoggle{bodyCon}{
+ \bodyconstRight{#1}{#2}{#3}
+ \togglefalse{bodyCon}
+}{
+ \ifthenelse{\equal{#2}{}}{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\&\quad &&#1 #2
+ \togglefalse{previousThird}
+ }{
+ ,\spanit\\&\quad &&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\&\quad &#1 & #2
+ \togglefalse{previousThird}
+ }{
+ ,\spanit\\&\quad &#1 & #2 && #3
+ \toggletrue{previousThird}
+ }
+}
+\togglefalse{bodyCon}
+}
+}
+
+% Adding constraints below subject to
+\DeclareDocumentCommand{\BelowAddConstraint}{m G{} G{}}{
\iftoggle{bodyCon}{
- \bodyconst{#1}{#2}
+ \bodyconstBelow{#1}{#2}{#3}
\togglefalse{bodyCon}
}{
- ,\\&\quad &#1 & #2
- \togglefalse{bodyCon}
+ \ifthenelse{\equal{#2}{}}{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\&&&#1 #2 \togglefalse{previousThird}
+ }{
+ ,\spanit\\&&&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\ &&#1 &#2 \togglefalse{previousThird}
+ }{
+ ,\spanit\\ &&#1 &#2 && #3
+ \toggletrue{previousThird}
+ }
+ }
+ \togglefalse{bodyCon}
+ }
+}
+
+% Adding constraints for a single alignment point
+\DeclareDocumentCommand{\oneAlignAddConstraint}{m G{} G{}}{
+ \iftoggle{bodyCon}{
+ \bodyconstOneAlign{#1}{#2}{#3}
+ \togglefalse{bodyCon}
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\&\quad &&#1 #2 \togglefalse{previousThird}
+ }{
+ ,\spanit\\&\quad &&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+ \togglefalse{bodyCon}
+ }
+}
+
+% Adding constraints for a single alignment point and with the constraints below
+\DeclareDocumentCommand{\oneAlignBelowAddConstraint}{m G{} G{}}{
+ \iftoggle{bodyCon}{
+ \bodyconstOneAlignBelow{#1}{#2}{#3}
+ \togglefalse{bodyCon}
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\& &&#1 #2\togglefalse{previousThird}
+ }{
+ ,\spanit\\& &&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+ \togglefalse{bodyCon}
}
}
+% Adding constraints below subject to for multiple references
+\DeclareDocumentCommand{\BelowAddConstraintMult}{m G{} G{}}{
+ \iftoggle{bodyCon}{
+ \bodyconstBelowMult{#1}{#2}{#3}
+ \togglefalse{bodyCon}
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ \ifthenelse{\equal{#2}{}}{
+ ,\spanit\\&&&#1 #2
+ }{
+ ,\spanit\\ &&#1 &#2
+ }
+ \togglefalse{previousThird}
+ }{
+ \ifthenelse{\equal{#2}{}}{
+ ,\spanit\\&&&#1 #2 && #3
+ }{
+ ,\spanit\\ &&#1 &#2&& #3}
+ \toggletrue{previousThird}
+ }
+ \togglefalse{bodyCon}
+ }
+}
+
+% Adding constraints for a single alignment point and with the constraints below for multiple references
+\DeclareDocumentCommand{\oneAlignBelowAddConstraintMult}{m G{} G{}}{
+ \iftoggle{bodyCon}{
+ \bodyconstOneAlignBelowMult{#1}{#2}
+ \togglefalse{bodyCon}
+ }{
+ \ifthenelse{\equal{#3}{}}{
+ ,\spanit\\& &&#1 #2 \togglefalse{previousThird}
+ }{
+ ,\spanit\\& &&#1 #2 && #3
+ \toggletrue{previousThird}
+ }
+ \togglefalse{bodyCon}
+}
+}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% ------------- SELECTING TYPE OF FORMAT -----------%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\newcommand{\selectConstraint}[1]{
+ \ifthenelse{\equal{#1}{1}}{
+ \let\addConstraint\BelowAddConstraint
+ }{
+ \ifthenelse{\equal{#1}{2}}{
+ \let\addConstraint\oneAlignAddConstraint
+ }{
+ \ifthenelse{\equal{#1}{3}}{
+ \let\addConstraint\oneAlignBelowAddConstraint
+ }{
+ \let\addConstraint\standardAddConstraint}
+}
+}
+}
+
+% Selecting for multiple references
+\newcommand{\selectConstraintMult}[1]{
+ \ifthenelse{\equal{#1}{1}}{
+ \let\addConstraint\BelowAddConstraintMult
+ }{
+ \ifthenelse{\equal{#1}{2}}{
+ \let\addConstraint\oneAlignAddConstraint
+ }{
+ \ifthenelse{\equal{#1}{3}}{
+ \let\addConstraint\oneAlignBelowAddConstraintMult
+ }{
+ \let\addConstraint\standardAddConstraint}
+}
+}
+}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% ------------- SETTING DEFAULT FORMAT -------------%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\newcommand{\setStandardMini}{
+ \toggletrue{bodyCon}
+ \let\addConstraint\standardAddConstraint
+}
+
+
\newcommand{\breakObjectiveOneConstraint}[1]{&&&#1\\}
@@ -67,167 +381,138 @@
%MINIMIZATION ENVIRONMENTS
\NewEnviron{mini}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
+\selectConstraint{#1}
+\let\addConstraint\standardAddConstraint
\begin{equation}
#4
- \begin{alignedat}{4}
+ \begin{alignedat}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
\end{alignedat}
\end{equation}
-\toggletrue{bodyCon}
-\let\bodyconst\bodyconstRight
+\setStandardMini
}
\NewEnviron{argmini}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
+\selectConstraint{#1}
\begin{equation}
#4
- \begin{alignedat}{4}
+ \begin{alignedat}{5}
\bodyobj{#2}{#3}{arg min}{#5}
\BODY
\end{alignedat}
\end{equation}
+\setStandardMini
}
\NewEnviron{mini*}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
- \begin{equation*}
- #4
- \begin{alignedat}{4}
+\selectConstraint{#1}
+ \begin{alignat*}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
- \end{alignedat}
-\end{equation*}
-\toggletrue{bodyCon}
-\let\bodyconst\bodyconstRight
+ \end{alignat*}
+\setStandardMini
}
\NewEnviron{argmini*}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
- \begin{equation*}
- #4
- \begin{alignedat}{4}
- \bodyobj{#2}{#3}{arg min}{#5}
+\selectConstraint{#1}
+ \begin{alignat*}{5}
+ \bodyobj{#2}{#3}{arg~mini}{#5}
\BODY
- \end{alignedat}
- \end{equation*}
+ \end{alignat*}
+\setStandardMini
}
\NewEnviron{mini!}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelowMult
- }{}
+\selectConstraintMult{#1}
\begin{subequations}
#4
- \begin{alignat}{4}
+ \begin{alignat}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
\end{alignat}
\end{subequations}
- \toggletrue{bodyCon}
-\let\bodyconst\bodyconstRight
+\setStandardMini
}
\NewEnviron{argmini!}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelowMult
- }{}
+\selectConstraintMult{#1}
\begin{subequations}
#4
- \begin{alignat}{4}
+ \begin{alignat}{5}
\bodyobj{#2}{#3}{arg min}{#5}
\BODY
\end{alignat}
- \end{subequations}
+ \end{subequations}
+\setStandardMini
}
%MAXIMIZATION ENVIRONMENTS
\NewEnviron{maxi}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
+\selectConstraint{#1}
\begin{equation}
#4
- \begin{alignedat}{4}
+ \begin{alignedat}{5}
\bodyobj{#2}{#3}{maximize}{#5}
\BODY
\end{alignedat}
\end{equation}
+\setStandardMini
}
\NewEnviron{argmaxi}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
+\selectConstraint{#1}
\begin{equation}
#4
- \begin{alignedat}{4}
+ \begin{alignedat}{5}
\bodyobj{#2}{#3}{arg maxi}{#5}
\BODY
\end{alignedat}
\end{equation}
+\setStandardMini
}
\NewEnviron{maxi*}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
- \begin{equation*}
- #4
- \begin{alignedat}{4}
+\selectConstraint{#1}
+ \begin{alignat*}{5}
\bodyobj{#2}{#3}{maximize}{#5}
\BODY
- \end{alignedat}
- \end{equation*}
+ \end{alignat*}
+\setStandardMini
}
\NewEnviron{argmaxi*}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelow
- }{}
- \begin{equation*}
- #4
- \begin{alignedat}{4}
+\selectConstraint{#1}
+ \begin{alignat*}{5}
\bodyobj{#2}{#3}{arg maxi}{#5}
\BODY
- \end{alignedat}
- \end{equation*}
+ \end{alignat*}
+\setStandardMini
}
\NewEnviron{maxi!}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelowMult
- }{}
+\selectConstraintMult{#1}
\begin{subequations}
#4
- \begin{alignat}{4}
+ \begin{alignat}{5}
\bodyobj{#2}{#3}{maximize}{#5}
\BODY
\end{alignat}
- \end{subequations}
+ \end{subequations}
+\setStandardMini
}
\NewEnviron{argmaxi!}[5][]{%
- \ifthenelse{\equal{#1}{1}}{
- \let\bodyconst\bodyconstBelowMult
- }{}
+\selectConstraintMult{#1}
\begin{subequations}
#4
- \begin{alignat}{4}
+ \begin{alignat}{5}
\bodyobj{#2}{#3}{arg maxi}{#5}
\BODY
\end{alignat}
- \end{subequations}
+ \end{subequations}
+\setStandardMini
} \ No newline at end of file