\documentclass[]{report}
\usepackage{remreset}
\usepackage{optidef}
\usepackage{listings}
\lstset{
	language=TeX,
	breaklines=true,
}
% Title Page
\title{\textit{\textbf{Optidef}} \\ A Latex library for optimization problems\\ \textnormal{Version - 1.2}}

\author{Jesus Lago Garcia}

\makeatletter
\renewcommand \thesection {\@arabic\c@section}
\@removefromreset{section}{chapter}
\makeatother


\begin{document}
\maketitle

\section{Introduction and features}

This small library provides a standard set of environments for writing minimization problems. The most important features are:
\begin{enumerate}
	\item It automatically aligns the problems in the most convenient way allowing different output formats:
		\begin{enumerate}
			\item Alignment at the beginning of the words \textit{minimize/argmin} and \textit{subject to}:
					\begin{mini*}
						{w}{f(w)+ R(w+6x)}
						{\label{eq:Ex1}}{}
						\addConstraint{g(w)}{=0}
					\end{mini*}
			\item Double format for the location of the constraints: either to the right of  \textit{subject to} aligned with the objective function or below \textit{subject to}.
			\item Optional alignment at the $=,~>,~<$ signs of the constraints.
								\begin{mini*}
									{w}{f(w)+ R(w+6x)}
									{\label{eq:Ex1}}{}
									\addConstraint{g(w)+h(w)}{=0}
									\addConstraint{l(w)}{=5w.}
								\end{mini*}
			\item Optionally, the user can add a third alignment point on the constraints to set some constraint features. A clear example could be the constraints names:
						\begin{mini*}
							{w}{f(w)+ R(w+6x)}
							{\label{eq:Ex1}}{}
							\addConstraint{g(w)+h(w)}{=0,}{\text{(Topological Constraint)}}
							\addConstraint{l(w)}{=5w,\quad}{\text{(Boundary Constraint)}}
						\end{mini*}
			
			or the index of the constraints:
						\begin{mini*}
							{w,u}{f(w)+ R(w+6x)}
							{\label{eq:Ex1}}{}
							\addConstraint{g(w_k)+h(w_k)}{=0,}{k=0,\ldots,N-1}
							\addConstraint{l(w_k)}{=5u,\quad}{k=0,\ldots,N-1}
						\end{mini*}
		\end{enumerate}

\item It provides an easy interface to define optimization problem for three different reference situations:
\begin{enumerate}
	\item Where no equation is referenced/numbered.
	\item Where the problem is referenced with a single number.
	\item Where each equation has an individual reference.
\end{enumerate} 

\item It also allows a definition of any optimization problem without a limitless number of constraints.

\end{enumerate}
\section{Environment types}
There are three basic environments depending on the type of referencing that should be used.
\begin{enumerate}
	\item The \textbf{mini} environment for defining problems with a single reference label:
		\begin{mini}
			{w}{f(w)+ R(w+6x)}
			{\label{eq:Ex1}}{}
			\addConstraint{g(w)}{=0}
		\end{mini}
	\item The \textbf{mini*} environment if the problem does not have to be referenced:
			\begin{mini*}
				{w}{f(w)+ R(w+6x)}
				{}{}
				\addConstraint{g(w)}{=0}
			\end{mini*}
	\item  The \textbf{mini!} environment if each equation should be referenced:
			\begin{mini!}
				{w}{f(w)+ R(w+6x)\label{eq:Ex2}}
				{\label{eq:Ex1}}{}
				\addConstraint{g(w)}{=0}
			\end{mini!}		
\end{enumerate}

\section{Output formats}
\label{sec:format}
There are four basic output formats. 
 \begin{enumerate}
 	\item \textbf{Standard format}: by default the constraints are aligned with the objective function, to the right of \textit{subject to} and with a second alignment point at the $=,~\leq,~\geq$:
 	\begin{mini}
 		{w}{f(w)+ R(w+6x)}
 		{\label{eq:Ex1}}{}
 		\addConstraint{g(w)+h(w)}{=0}
 		\addConstraint{t(w)}{=0.}
 	\end{mini}
 	\item \textbf{Alternative 1}: a possible variation is to locate the constraints below \textit{subject to}:
 	\begin{mini}[1]
 		{w}{f(w)+ R(w+6x)}
 		{\label{eq:Ex1}}{}
 		\addConstraint{g(w)+h(w)}{=0}
 		\addConstraint{t(w)}{=0.}
 	\end{mini}
 	\item \textbf{Alternative 2}: a second variation is to copletely remove the second alignment point:
 	\begin{mini}[2]
 		{w}{f(w)+ R(w+6x)}
 		{\label{eq:Ex1}}{}
 		\addConstraint{g(w)+h(w)}{=0}
 		\addConstraint{t(w)}{=0.}
 	\end{mini} 	
 	\item \textbf{Alternative 3}: a third variation would be the combination of both of them:
 	\begin{mini}[3]
 		{w}{f(w)+ R(w+6x)}
 		{\label{eq:Ex1}}{}
 		\addConstraint{g(w)+h(w)}{=0}
 		\addConstraint{t(w)}{=0.}
 	\end{mini} 	
 \end{enumerate}
 
\section{Environment Syntaxes}
The three environments use the same syntax with the same number of parameters. In particular, considering that \textit{Const} stands for constraint ,\textit{LHS} stands for Left-hand-side and \textit{RHS} for Right-hand-side, the basic structure to define a general optimization problem is:
\begin{verbatim}

\begin{mini#}[Format]
   {Optimization variable}
   {Objective function \label{Objective function referece}}
   {\label{Global referece of Optimization Problem}}  
   {Result of the optimization problem or any expression on 
   the left of the minimize word}

   \addConstraint{LHS Const 1}{RHS Const 1 \label{Reference
    Const 1}}{Extra Const 1 Info}
   \addConstraint{LHS Const 2}{RHS Const 2 \label{Reference
    Constraint 2}}{Extra Const 2 Info}
   .
   .
   \addConstraint{LHS N}{RHS Const N \label{Reference 
   Const N}}{Extra Const N Info}
\end{mini#}
\end{verbatim}

\noindent where \# $\in \{*, !, ~ \}$.
\subsection{Parameters definition}
The number of parameters that the environment uses is 6:

\begin{enumerate}
	\item \verb|Optimization variable|, e.g. $w \in \Re^N$.
	\item \verb|Objective function|, e.g. $\|w\|_2$.
	\item \verb|\label{Global referece of Optimization Problem}|: it defines the main and general reference for the optimization problem. It is used for the \verb|mini| and \verb|mini!| enviroments. In the \verb|mini*| environment leave it blank, i.e. \{\}.   
	\item \verb|Result of the optimization problem|: optionally a term expressing the result of the optimization problem, e.g. $J(w^*)~=$.
	\item \verb|Format|: optional parameter to change the format of the constraints. By default the constraints are located at the right of \textit{subject to} and aligned on the $=,\leq,\geq$ signs. However, as represented in Section \ref{sec:format}), the following variations are possible:
	\begin{enumerate}
		\item If Format=1: same alignment as the default format but the constraints appear right below \textit{subject to}. Check  Alternative 1 in Section \ref{sec:format}).
		\item If Format=2: constraints are located on the right of \textit{subject to} but left aligned. Check  Alternative 2 in Section \ref{sec:format}).
		\item If Format=3: constraints are located below \textit{subject to} but left aligned. Check  Alternative 3 in Section \ref{sec:format}).
	\end{enumerate}

\end{enumerate}

\noindent Notice that only the first two mandatory parameters (variable and objective) will be really necessary in every definition, nevertheless and for the sake of having homogeneous definitions, we opted for requiring the 4 parameters in every definition and expecting empty parameters definitions, i.e. \{\}, when they are not needed.


After the definition of this parameters, the environment accepts the definition of an infinite number of constraints. For this definitions the command:
~\\

\verb|\addConstraint{LHS Constraint k}|

\verb|{RHS Constraint k \label{Reference Constraint k}}| 

\verb|{Extra Infor Contraint k}| 
~\\

\noindent has to be used, where:
\begin{enumerate}
	\item \verb|LHS Constraint|: the left-hand side of the any constraint, e.g. $3w^\top w$.
	\item \verb|RHS Constraint|: the right-hand side of the first constraints including the equal, less and greater signs, e.g. $\leq \|w\|_\infty$, as well as the constraint label.\textbf{ Keep in mind that this parameter is optional and if not included or leave it blank all the constraints will be aligned to the left (See alternative format 2 and 3 in Section \ref{sec:format}).}
	\item \verb|Extra Info|: optional parameter to add extra info such as contraints set definition or constraint name. Look Example 8 or the fourth described feature in Section 1.
\end{enumerate}

\subsubsection{Constraints referencing}
Notice that the label for the constraints is always included in the right hand side expression and it only makes sense for the case of using the \verb|mini!| enviroment. The label of the objective function can also be included in a similar way.

\subsubsection{Example 1 - mini environment}
The code:

\begin{verbatim}
\begin{mini}
   {w}{f(w)+ R(w+6x)}
   {\label{eq:Example1}}{}
   
   \addConstraint{g(w)}{=0} 
   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \addConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex11}}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 2 - mini* environment}
On the other hand:

\begin{verbatim}
\begin{mini*}
   {w}{f(w)+ R(w+6x)}
   {}{}

   \addConstraint{g(w)}{=0}   
   \addConstraint{n(w)}{= 6,}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \addConstraint{h(x)}{=0.}  
\end{mini*}
\end{verbatim}

\noindent it is almost the same but removing the reference:

\begin{mini*}
	{w}{f(w)+ R(w+6x)}
	{}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini*}

\subsubsection{Example 3 - mini! environment}

\noindent Finally, the multireferencing environment outputs:

\begin{verbatim}
\begin{mini!}
   {w}{f(w)+ R(w+6x) \label{eq:ObjectiveExample1}}
   {\label{eq:Example1}}{}

   \addConstraint{g(w)}{=0 \label{eq:C1Example3}}
   \addConstraint{n(w)}{= 6 \label{eq:C2Example1}}
   \addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example1}}
   \addConstraint{h(x)}{=0. \label{eq:C4Example1}}
\end{mini!}
\end{verbatim}

\begin{mini!}
	{w}{f(w)+ R(w+6x)\label{eq:ObjectiveExample3}}
	{\label{eq:Example3}}
	{}
	\addConstraint{g(w)}{=0 \label{eq:C1Example3}}
	\addConstraint{n(w)}{= 6 \label{eq:C2Example3}}
	\addConstraint{L(w)+r(x)}{=Kw+p \label{eq:C3Example3}}
	\addConstraint{h(x)}{=0.\label{eq:C4Example3}}
\end{mini!}

\subsubsection{Example 4 - mini + problem result}

\noindent Adding the problem result:

\begin{verbatim}
\begin{mini}
{w}{f(w)+ R(w+6x)}
{\label{eq:Example1}}
{J(w^*)=}

\addConstraint{g(w)}{=0}
\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\addConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex1}}{J(w^*)~=~}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 5 - second format}

\noindent If including a 1 as optional parameter, the first constraint will appear aligned to the left right below \textit{subject to}.

\begin{verbatim}
\begin{mini}[1]
{w}{f(w)+ R(w+6x)}
{\label{eq:Example1}}
{}

\addConstraint{g(w)}{=0}
\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\addConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}[1]
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex1}}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 6 - third format}

\noindent If including a 2 as optional parameter, the constraint will appear to the right of  \textit{subject to} but a single alignment point.

\begin{verbatim}
\begin{mini}[2]
{w}{f(w)+ R(w+6x)}
{\label{eq:Example1}}
{}

\addConstraint{g(w)}{=0}
\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\addConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}[2]
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex1}}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 7 - fourth format}

\noindent If including a 3 as optional parameter, the first constraint will appear aligned to the left right below \textit{subject to} and with a single alignment point.

\begin{verbatim}
\begin{mini}[3]
{w}{f(w)+ R(w+6x)}
{\label{eq:Example1}}
{}

\addConstraint{g(w)}{=0}
\addConstraint{n(w)}{= 6}
\addConstraint{L(w)+r(x)}{=Kw+p}
\addConstraint{h(x)}{=0.}
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}[3]
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex1}}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{mini}

\subsubsection{Example 8 - mini* + extra constraint alignment}
Adding the fourth optional alignment to add constraint names:

\begin{verbatim}
\begin{mini*}
	{w}{f(w)+ R(w+6x)}
	{}{}
	\addConstraint{g(w)}{=0,}{ \quad  \text{(Dynamic constraint)}}
	\addConstraint{n(w)}{= 6,}{ \quad  \text{(Boundary constraint)}}
	\addConstraint{L(w)+r(x)}{=Kw+p,}{ \quad  \text{(Random constraint)}}
	\addConstraint{h(x)}{=0,}{ \quad  \text{(Path constraint).}}
\end{mini*}
\end{verbatim}


\begin{mini*}
	{w}{f(w)+ R(w+6x)}
	{}{}
	\addConstraint{g(w)}{=0,}{ \quad  \text{(Dynamic constraint)}}
	\addConstraint{n(w)}{= 6,}{ \quad  \text{(Boundary constraint)}}
	\addConstraint{L(w)+r(x)}{=Kw+p,}{ \quad  \text{(Random constraint)}}
	\addConstraint{h(x)}{=0,}{ \quad  \text{(Path constraint).}}
\end{mini*}


\subsubsection{Example 9 - mini environment on the unconstrained case}

\begin{verbatim}

\begin{mini}
   {w}{f(w)+ R(w+6x)}  
   {\label{eq:Ex4}}
   {}   
\end{mini}
\end{verbatim}

\noindent outputs:

\begin{mini}
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex4}}{}
\end{mini}


\section{The \textit{argmini} environment}
 Similar to the \verb|mini|, \verb|mini*| and \verb|mini!| environments, the environments \verb|argmini|, \verb|argmini*| and \verb|argmini!| are very similar environments that use the same syntax but the output is slightly different. The following code serves for illustration:
 
\begin{verbatim}
\begin{argmini}
   {w}{f(w)+ R(w+6x)}
   
   {\label{eq:Example1}}{w^*=}
   
   \addConstraint{g(w)}{=0}
   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \addConstraint{h(x)}{=0.}
\end{argmini}
\end{verbatim}

\noindent outputs:

\begin{argmini}
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Ex1}}{w^*~=~}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{argmini}

\section{The \textit{maxi} and \textit{argmaxi} environments}
Exactly the same syntax and definition as the previous environments, but now for defining maximization environments. The following code serves for illustration:

\begin{verbatim}
\begin{maxi}
   {w}{f(w)+ R(w+6x)}
   {g(w)}{=0}

   {\label{eq:Example1}}{}

   \addConstraint{g(w)}{=0}
   \addConstraint{n(w)}{= 6}
   \addConstraint{L(w)+r(x)}{=Kw+p}
   \addConstraint{h(x)}{=0.}
\end{maxi}
\end{verbatim}

\noindent outputs:

\begin{maxi}
	{w}{f(w)+ R(w+6x)}
	{\label{eq:Example1}}{}
	\addConstraint{g(w)}{=0}
	\addConstraint{n(w)}{= 6}
	\addConstraint{L(w)+r(x)}{=Kw+p}
	\addConstraint{h(x)}{=0.}
\end{maxi}


\section{Problems and limitations}
One current limitation is that the size of the optimization variables must be smaller than the word minimize, otherwise, the output is not properly aligned. Example:

\begin{mini!}
	{x_0,u_0,x_1,\hdots,u_{N-1},x_N}
	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
	{\label{eq:OCP}}{}
	\addConstraint{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
	\addConstraint{h(x_k,u_k)}{\leq 0,  \quad k=0,\dots,N-1}
	\addConstraint{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
\end{mini!}

\noindent A possible way to avoid this is to stack them with the command: \begin{verbatim}
\substack{x_0,u_0,x_1,\hdots,\\u_{N-1},x_N}
\end{verbatim}

\begin{mini!}
	{\substack{x_0,u_0,x_1,\hdots,\\ u_{N-1},x_N}}
	{\sum_{k=0}^{N-1} L(x_k,u_k)\!\!+\!\!E(x_N)\label{OCPobj}}
	{\label{eq:OCP}}{}
	\addConstraint{x_{k+1}-f(x_k,u_k)}{=  0, \label{dOCP:modelc}\quad k=0,\dots,N-1}
	\addConstraint{h(x_k,u_k)}{\leq 0,  \quad k=0,\dots,N-1}
	\addConstraint{r(x_0,x_N)}{= 0.  \label{dOCP:boundary}}
\end{mini!}

\section{Code definition}
\begin{lstlisting}
% optidef - Version 1.2
%
%Copyright 2016 J. Lago Garcia
%
%This work may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version 1.3 of this license or (at your option) any later version.
%The latest version of this license is in http://www.latex-project.org/lppl.txt and version 1.3 or later is part of all distributions of LaTeX version 2005/12/01 or later.
%
%This work has the LPPL maintenance status 'maintained'. The Current Maintainer of this work is J. Lago Garcia, under the supervision of Prof. Dr. Moritz Diehl and Prof. Dr. Sebastien Gross.
%
%E-mail: jesus.lago.garcia@venus.uni-freiburg.de
%
%This work consists of the file optidef.sty.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{optidef}[2016/06/28 - version=1.2,  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}

% 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}{\mathrm{#3}} \quad #2\span\span\span\span
&\underset{\displaystyle #1}{\mathrlap{\mathrm{#3}}\phantom{\mathrm{subject~to}}} \quad #2\span\span\span\span
}
{
%#4 ~~ &\underset{\displaystyle #1}{\mathrm{#3}} \quad #2\span\span\span\span
#4 ~~ &\underset{\displaystyle #1}{\mathrlap{\mathrm{#3}}\phantom{\mathrm{subject~to}}} \quad #2\span\span\span\span
}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  -------- DEFINITION DIFFERENT TYPE OF BODY CONSTRAINTS  --------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\DeclareDocumentCommand{\bodyconst}{m G{}}
{	\ifthenelse{\equal{#2}{}}{
\\ &\mathrm{subject~to} \quad &&#1 #2 
}{
\\ &\mathrm{subject~to} \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}
}
}

% 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}
}
}

% 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}{
\bodyconstBelow{#1}{#2}{#3}
\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\\}



%MINIMIZATION ENVIRONMENTS

\NewEnviron{mini}[5][]{%
\selectConstraint{#1}
\let\addConstraint\standardAddConstraint
\begin{equation}
#4
\begin{alignedat}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
\end{alignedat}
\end{equation}
\setStandardMini
}

\NewEnviron{argmini}[5][]{%
\selectConstraint{#1}
\begin{equation}
#4
\begin{alignedat}{5}
\bodyobj{#2}{#3}{arg~min}{#5}
\BODY
\end{alignedat}
\end{equation}
\setStandardMini
}

\NewEnviron{mini*}[5][]{%
\selectConstraint{#1}
\begin{alignat*}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
\end{alignat*}
\setStandardMini
}

\NewEnviron{argmini*}[5][]{%
\selectConstraint{#1}
\begin{alignat*}{5}
\bodyobj{#2}{#3}{arg~mini}{#5}
\BODY
\end{alignat*}
\setStandardMini	
}


\NewEnviron{mini!}[5][]{%
\selectConstraintMult{#1}
\begin{subequations}
#4
\begin{alignat}{5}
\bodyobj{#2}{#3}{minimize}{#5}
\BODY
\end{alignat}
\end{subequations}
\setStandardMini
}

\NewEnviron{argmini!}[5][]{%
\selectConstraintMult{#1}
\begin{subequations}
#4
\begin{alignat}{5}
\bodyobj{#2}{#3}{arg~min}{#5}
\BODY
\end{alignat}
\end{subequations}	
\setStandardMini		
}

%MAXIMIZATION ENVIRONMENTS

\NewEnviron{maxi}[5][]{%
\selectConstraint{#1}
\begin{equation}
#4
\begin{alignedat}{5}
\bodyobj{#2}{#3}{maximize}{#5}
\BODY
\end{alignedat}
\end{equation}
\setStandardMini
}

\NewEnviron{argmaxi}[5][]{%
\selectConstraint{#1}
\begin{equation}
#4
\begin{alignedat}{5}
\bodyobj{#2}{#3}{arg~maxi}{#5}
\BODY
\end{alignedat}
\end{equation}
\setStandardMini
}

\NewEnviron{maxi*}[5][]{%
\selectConstraint{#1}
\begin{alignat*}{5}
\bodyobj{#2}{#3}{maximize}{#5}
\BODY
\end{alignat*}
\setStandardMini
}

\NewEnviron{argmaxi*}[5][]{%
\selectConstraint{#1}
\begin{alignat*}{5}
\bodyobj{#2}{#3}{arg~maxi}{#5}
\BODY
\end{alignat*}
\setStandardMini
}


\NewEnviron{maxi!}[5][]{%
\selectConstraintMult{#1}
\begin{subequations}
#4
\begin{alignat}{5}
\bodyobj{#2}{#3}{maximize}{#5}		
\BODY
\end{alignat}
\end{subequations}	
\setStandardMini		
}

\NewEnviron{argmaxi!}[5][]{%
\selectConstraintMult{#1}
\begin{subequations}
#4
\begin{alignat}{5}
\bodyobj{#2}{#3}{arg~maxi}{#5}		
\BODY
\end{alignat}
\end{subequations}
\setStandardMini			
}
\end{lstlisting}

\end{document}          
