%% Custom macros for Elsevier/Automatica submissions.
%% Author: Mohamad H. Kazma. See root.tex for usage.
%%Created by Mohamad H. Kazma
%%Date: January 10, 2024.
%%This preamble contains packages and defined commands 
%%that are usefull for writing journal papers in the field of control. 


%%To make first letter a two line letter and bold in the introduction
\usepackage{lettrine} %To make first letter a two line letter and bold in the introduction

%%To have a balanced final page
\usepackage{balance} % To balance the references in the final page


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%use this to decrease paper length; works only with subtle mode for IEEEtran
%\usepackage[moderate]{savetrees}
%\usepackage[subtle]{savetrees}
%\usepackage{titlesec} % Adjust section spacing
%\titlespacing*{\section}{0pt}{*0.3}{*0.2}
%\titlespacing*{\subsection}{0pt}{*0.3}{*0.2}
%\titlespacing*{\subsubsection}{0pt}{*0.3}{*0.2}
%% Equation display spacing and float (figure/table/algorithm) spacing:
%% apply at begin document so class/font-size resets do not override these values.
%\AtBeginDocument{%
%  % Force flush-left equations (override fleqn.clo default):
%  \setlength{\mathindent}{0pt}%
%  % Equation display spacing:
%  \setlength{\abovedisplayskip}{3.7pt}%
%  \setlength{\belowdisplayskip}{3.7pt}%
%  \setlength{\abovedisplayshortskip}{3.5pt}%
%  \setlength{\belowdisplayshortskip}{3.5pt}%
%  % Float spacing (figures, tables, algorithms):
%  \setlength{\textfloatsep}{1pt plus 1pt minus 2pt}%  % between top/bottom float and body text
%  \setlength{\floatsep}{1pt plus 1pt minus 1pt}%      % between consecutive floats on a float page
%  \setlength{\intextsep}{1pt plus 1pt minus 1pt}%     % around h-placed (in-text) floats
%  \setlength{\abovecaptionskip}{1pt}%                 % between float body and its caption
%  \setlength{\belowcaptionskip}{1pt}%                 % below the caption
%}


%%Adjusted fonts to reduce paragraph size    
%\fontdimen2\font=0.60ex% inter word space or 59ex
%\fontdimen3\font=0.62ex% inter word stretch
%\fontdimen4\font=0.62ex% inter word stretch

%%Adjusted for Subtle savetrees mode to reduce paragraph size    
%\fontdimen2\font=0.45ex% inter word space or 59ex
%\fontdimen3\font=0.5ex% inter word stretch
%\fontdimen4\font=0.2ex% inter word stretch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% To have hyperlinks in the document and to make sure that the hyperlinks do not cause compilation issues with elsarticle class
\usepackage{hyperref}
\makeatletter
\pdfstringdefDisableCommands{%
  \let\cnotenum\@gobble
  \let\@corref\@gobble
}
\makeatother
% Define LCSS color before first use
% (also defined below for \parstartc and theorem environments)
\definecolor{LCSS}{rgb}{0,0.502,0.675} %Automatica blue.
% Override hyperref colors at begin-document to ensure they take effect
% AFTER all packages (including preamble_eslvier's hyperref) are loaded.
\AtBeginDocument{%
  \hypersetup{
    colorlinks  = true,
    linkcolor   = LCSS,
    citecolor   = LCSS,
    urlcolor    = LCSS,
    anchorcolor = LCSS,
    filecolor   = LCSS,
  }%
}

% The following packages can be found on http:\\www.ctan.org
%\usepackage{graphics} % for pdf, bitmapped graphics files
%\usepackage{epsfig} % for postscript graphics files
%\usepackage{mathptmx} % assumes new font selection scheme installed
%\usepackage{times} % assumes new font selection scheme installed
%\usepackage{amsmath} % assumes amsmath package installed
%\usepackage{amssymb}  % assumes amsmath package installed

%%To draw figures an illustration on latex
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta, decorations.pathmorphing, decorations.markings, calc, 3d}
\usetikzlibrary{positioning, fit, backgrounds}
% Style for the arrows
\tikzset{
	tangent/.style={decoration={
			markings,mark=at position #1 with {
				\coordinate (ta) at (0,0);
				\coordinate (tb) at (0.1,0);
			}
		},postaction=decorate},
	tangent/.default=0.5
}

%To adjust margins of a paper
%\usepackage{geometry}
%%For conference papers L-CSS (CDC AND ACC)
%\geometry{
%	left=0.75in,   % Left margin
%	right=0.75in,  % Right margin
%	top=0.75in,    % Top margin
%	bottom=0.75in, % Bottom margin
%}


%%The lineno packages adds line numbers. Start line numbering with
%%\begin{linenumbers}, end it with \end{linenumbers}. Or switch it on
% %for the whole article with \linenumbers.
% \usepackage{lineno}

%%This package supports editorial comments and gives some extra support for writing papers
%\usepackage[editing]{coop-writing}
%\usepackage[submit]{coop-writing}
%\usepackage[publish]{coop-writing}

%%Allows to begin verbatim section
%\usepackage{fancyvrb}

%%Connects to links external to the document \href{URL}{text}
%\usepackage{hyperref}
%\hypersetup{colorlinks=true,linkcolor=blue,urlcolor=red}

%%This packages allows easier representation of ODEs
%\usepackage[<options>]{diffcoeff}[=v5]

%\usepackage{lipsum}
%\usepackage{multicol}
%\usepackage{tcolorbox}
%\usepackage{witharrows}
%\usepackage{minted}
%\usetikzlibrary{positioning}
%\usepackage{pgfplots}
%\pgfplotsset{compat=1.18}
%\usepackage{tikz-cd}
%\usepackage{xcolor}
%\usepackage{tcolorbox}
%\usepackage{siunitx}
%\usepackage{cleveref}
%\usepackage{coordsys}
%\usepackage{fitbox}
%\usepackage{bchart}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Colors that can be used; These colors were first used in system paritioning paper
% Subsystem (Overall grey)
\definecolor{Vgray}{RGB}{247,247,247}
\definecolor{Vedge}{RGB}{200,200,200}

% Define colors for the subsystems
\definecolor{Sub1purple}{RGB}{200,200,255}
\definecolor{Sub1edge}{RGB}{205,215,238}

\definecolor{Sub2yellow}{RGB}{251,234,206}
\definecolor{Sub2edge}{RGB}{251,234,206} % Same for fill and draw

\definecolor{Sub3green}{RGB}{215,238,210}
\definecolor{Sub3edge}{RGB}{215,232,210}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definecolor{Sub1color}{rgb}{0.85, 0.9, 1}
\definecolor{Sub2color}{rgb}{1, 0.9, 0.7}
\definecolor{Sub3color}{rgb}{0.8, 1, 0.8}
\definecolor{Sub4color}{RGB}{200,200,255}

%\colorlet{Sub1edge}{blue!10}
%\colorlet{Sub2edge}{orange!10}
%\colorlet{Sub3edge}{green!10}
\definecolor{Sub4edge}{RGB}{205,215,238}

\definecolor{Sub5color}{RGB}{255,210,210}
\definecolor{Sub5edge}{RGB}{240,180,180}

%%%New
%\definecolor{Sub1edge}{RGB}{120,120,120}
%\definecolor{Sub1color}{RGB}{230,230,255}
%
%\definecolor{Sub2edge}{RGB}{140,100,100}
%\definecolor{Sub2color}{RGB}{255,230,230}
%
%\definecolor{Sub3edge}{RGB}{100,140,100}
%\definecolor{Sub3color}{RGB}{230,255,230}
%
%\definecolor{Sub4edge}{RGB}{100,100,140}
%\definecolor{Sub4color}{RGB}{230,230,255}

\definecolor{Sub5edge}{RGB}{140,120,80}
\definecolor{Sub5color}{RGB}{255,245,225}

\definecolor{Sub6edge}{RGB}{100,130,150}
\definecolor{Sub6color}{RGB}{220,240,255}

\definecolor{Sub7edge}{RGB}{130,100,150}
\definecolor{Sub7color}{RGB}{245,230,255}

\definecolor{Sub8edge}{RGB}{150,130,100}
\definecolor{Sub8color}{RGB}{255,250,230}

\definecolor{Sub9edge}{RGB}{90,140,140}
\definecolor{Sub9color}{RGB}{220,250,250}

\definecolor{Sub10edge}{RGB}{110,110,110}
\definecolor{Sub10color}{RGB}{240,240,240}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%User Defined Commands: (mkazma)
%\newcommand{cmd}[args]{def}
%\renewcommand{cmd}[args]{def}

%cmd: A command name beginning with a \. For \newcommand it must not be already defined and must not begin with \end; for \renewcommand it must already be defined.
%args: An integer from 1 to 9 denoting the number of arguments of the command being defined. The default is for the command to have no arguments.
%def: The text to be substituted for every occurrence of cmd; a parameter of the form #n in cmd is replaced by the text of the nth argument when this substitution takes place.

%Text  Intro start with Letterine \\\ requires package lettrine
\newcommand{\introstart}[2]{\noindent \lettrine[lines=2]{#1}{#2}}

%Text Bold for Notation and paper organization
\newcommand{\parstart}[1]{\noindent \textbf{#1.}\;}

% Text color for Notation and paper organization (Automatica uses black)
% \definecolor{LCSS}{HTML}{004498}
\definecolor{LCSS}{rgb}{0,0.502,0.675} %Automatica blue.
\newcommand{\parstartc}[1]{\noindent \textbf{\textcolor{LCSS}{#1.}}\;}

%Reference a section with number
%\newcommand{\sec}[1]{Section~\ref{#1}}

%Column vector  \mathrm{col}\{\gamma_j\}_{j=1}^{n_y}
\newcommand{\col}[4]{\mathrm{col}\left\{ {#1}_{#2}\right\}_{{#2}={#3}}^{#4}}
%same as above without the word col
\newcommand{\nocol}[4]{\left\{ {#1}_{#2}\right\}_{{#2}={#3}}^{#4}}

%Real set
\newcommand{\R}{\mathbb{R}}
\newcommand{\Rn}[1]{\mathbb{R}^{#1}}

%%L2 and Linf norms
\newcommand{\linf}{\mathcal{L}_{\infty}}
\newcommand{\ltwo}{\mathcal{L}_{2}}
\newcommand{\lone}{\mathcal{L}_{1}}

%%Logdeterminant
\newcommand{\logdet}[1]{\mr{log} \mr{det}(#1)}
\newcommand{\logdett}{\mr{log} \mr{det}}

%% Colored cross-reference macros (all output text in LCSS blue)
%% Uses \hyperref[label]{text} so the ENTIRE text (word + number) becomes
%% a single clickable hyperlink colored by hyperref's linkcolor = LCSS.
%%  Usage: \theoref{label}, \lemref{label}, \corref{label}, \propref{label},
%%         \secref{label}, \defref{label}, \remref{label}, \asmpref{label},
%%         \figref{label}, \tabref{label}, \probref{label}, \eqref{label}
\newcommand{\theoref}[1]{\hyperref[#1]{Theorem~\ref*{#1}}}
\newcommand{\lemref}[1]{\hyperref[#1]{Lemma~\ref*{#1}}}
% Note: \corref is reserved by elsarticle for corresponding-author marks.
% Use \coref for Corollary references to avoid the clash.
\newcommand{\coref}[1]{\hyperref[#1]{Corollary~\ref*{#1}}}
\newcommand{\propref}[1]{\hyperref[#1]{Proposition~\ref*{#1}}}
\newcommand{\defref}[1]{\hyperref[#1]{Definition~\ref*{#1}}}
\newcommand{\remref}[1]{\hyperref[#1]{Remark~\ref*{#1}}}
\newcommand{\exmpref}[1]{\hyperref[#1]{Example~\ref*{#1}}}
\newcommand{\asmpref}[1]{\hyperref[#1]{Assumption~\ref*{#1}}}
\newcommand{\secref}[1]{\hyperref[#1]{Section~\ref*{#1}}}
\newcommand{\subsecref}[1]{\hyperref[#1]{Section~\ref*{#1}}}
\renewcommand{\eqref}[1]{\hyperref[#1]{(\ref*{#1})}}
\newcommand{\figref}[1]{\hyperref[#1]{Fig.~\ref*{#1}}}
\newcommand{\tabref}[1]{\hyperref[#1]{Table~\ref*{#1}}}
\newcommand{\probref}[1]{\hyperref[#1]{Problem~\ref*{#1}}}

%Title command
\newcommand{\titlesc}[1]{\title{\large \vspace{0.7cm} \Large\centering {\textsc{{#1}}}}}
\newcommand{\titlebf}[1]{\title{\Large \vspace{0.7cm} \LARGE \centering {\textsc{\textbf{#1}}}}}

%%To change figure caption 
\usepackage{caption} 
\captionsetup[figure]{name=Fig., font=smaller,font+=smaller,labelsep=period}
%Other option is
%\renewcommand{\figurename}{Fig.} 


% In elsarticle to adjust list spacing locally around thebibliography.
\usepackage{etoolbox}
\pdfstringdefDisableCommands{%
  % Define how \lettrine should behave *only inside PDF strings*.
  % Original syntax (simplified): \lettrine[<options>]{<first letter>}{<rest>}
  %
  % Here:
  %   #1 = the optional argument content inside [...]
  %   #2 = the first mandatory argument {<first letter>}
  %   #3 = the second mandatory argument {<rest>}
  %
  % Replacement: output just the text (#2#3), ignoring the fancy drop-cap formatting.
  \def\lettrine[#1]#2#3{#2#3}%
}

\makeatletter
\providecommand{\URLprefix}{} % ensure it exists even before the .bbl is read
\makeatother

% Compact bibliography: zero inter-item spacing and footnotesize font (Automatica style)
\setlength{\bibsep}{0pt plus 0.2ex}
\renewcommand{\bibfont}{\footnotesize}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Redefine the proof environment (colored, matching LCSS style)
\renewenvironment{proof}{%
	\par\vspace{1pt}%
	\noindent\textbf{\textit{\textcolor{LCSS}{Proof.}}}\enspace%
}{%
	\hfill$\blacksquare$%
	\par\vspace{1pt}%
}

%%% Proof-of environments for appendix
\newenvironment{proofoflemmac}[1]{%
	\vspace{1pt}%
	\noindent\textbf{\textit{\textcolor{LCSS}{Proof of Lemma~\ref{#1}.}}}\enspace%
}{%
	\hfill$\blacksquare$%
	\vspace{1pt}%
}
\newenvironment{proofoftheoc}[1]{%
	\vspace{1pt}%
	\noindent\textbf{\textit{\textcolor{LCSS}{Proof of Theorem~\ref{#1}.}}}\enspace%
}{%
	\hfill$\blacksquare$%
	\vspace{1pt}%
}
\newenvironment{proofofcorc}[1]{%
	\vspace{1pt}%
	\noindent\textbf{\textit{\textcolor{LCSS}{Proof of Corollary~\ref{#1}.}}}\enspace%
}{%
	\hfill$\blacksquare$%
	\vspace{1pt}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Theorems with color for LCSS colored with dot.
\newtheorem{theorem}{\textbf{\textcolor{LCSS}{Theorem}}}
\newtheorem{mylem}{\textbf{\textcolor{LCSS}{Lemma}}}
\newtheorem{mydef}{\textbf{\textcolor{LCSS}{Definition}}}
\newtheorem{mypro}{\textbf{\textcolor{LCSS}{Property}}}
\newtheorem{myrem}{\textbf{\textcolor{LCSS}{Remark}}}
\newtheorem{asmp}{\textbf{\textcolor{LCSS}{Assumption}}}
\newtheorem{mycor}{\textbf{\textcolor{LCSS}{Corollary}}}
\newtheorem{myprs}{\textbf{\textcolor{LCSS}{Proposition}}}
\newtheorem{exmpl}{\textbf{\textcolor{LCSS}{Example}}}
\newtheorem{problem}{\textbf{\textcolor{LCSS}{Problem}}}

%% Theorem header formatting
\makeatletter
\def\@begintheorem#1#2{%
	\item[\hskip\labelsep\textbf{\textcolor{LCSS}{\textit{#1 #2}}}]%
	\@ifnextchar[{\@withinfo}{\textbf{\textcolor{LCSS}{.}}\enspace\ignorespaces}}

\def\@withinfo[#1]{% Ensure optional argument stays inline and formatted correctly
	\textbf{\textcolor{LCSS}{\textit{ (#1)}}}\textcolor{LCSS}{.} \ignorespaces}
\makeatother
