%%%==============================================================================
%% Copyright 2022-23 by Alceu Frigeri
%%
%% This work may be distributed and/or modified under the conditions of
%%
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
%%   version 1.3c (or later), and/or
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
%%   version 3 (or later)
%%
%% This work has the LPPL maintenance status *maintained*.
%%
%% The Current Maintainer of this work is Alceu Frigeri
%%
%% This is version 1.10 (2023/05/11)
%%
%% The list of files that compose this work can be found in the README.md file at
%% https://ctan.org/pkg/ufrgscca
%%
%%%==============================================================================
%% UFRGS stands for "Federal University of Rio Grande do Sul" in south Brazil
%% EE    stands for "Engineering School"
%% CCA   stands for "Control and Automation Engineering Course" (Portuguese acronym)
%%%==============================================================================
\NeedsTeXFormat{LaTeX2e}[2022/06/01]
\def\tc@tmp@packname{ufrgscca-abnt}
\def\tc@tmp@altpackname{ufrgsccaabnt}
\def\tc@tmp@packdesc{UFRGS/CCA ABNT layout}
\def\tc@tmp@packdate{2023/05/11}
\def\tc@tmp@packversion{1.10}
\let\@exp\expandafter
\@exp\edef\csname\tc@tmp@altpackname version\endcsname{\tc@tmp@packversion}
\@exp\@exp\@exp\ProvidesPackage\@exp\@exp\@exp{\@exp\tc@tmp@packname\@exp}\@exp[\tc@tmp@packdate\space v\tc@tmp@packversion\space\tc@tmp@packdesc]

\def\tc@tocdepth{3}  % Table of Contents depth
\def\tc@secdepth{4} % Sectioning numbers depth (3 -> subsubsection)

\DeclareKeys[ufrgscca-abnt]{
    strict-abnt.usage = load,
		strict-abnt.if = tcif@strictabnt,

        pretextontoc.usage = load,
        pretextontoc.if = tcif@pretextontoc,

        yearsonly.usage = load,
        yearsonly.if = tcif@yearsonly,


		chapternopagenum.usage = load,
		chapternopagenum.if = tcif@chapternopagenum,

		english.usage = load,
		english.if = tcif@english,
		relnum.usage = load,
		relnum.if = tcif@relnum,
		repeatfields.usage = load,
		repeatfields.if = tcif@repeatfields,

		showframes.usage = load,
		showframes.if = tcif@showframes,
		showlabels.usage = load,
		showlabels.if = tcif@showlabels,

		tocdepth.usage = load,
		tocdepth.store = \tc@tocdepth,
		secdepth.usage = load,
		secdepth.store = \tc@secdepth,

		nomicrotype.usage = load,
		nomicrotype.if = tcif@nomicrotype,

		article.usage = load,
		article.if = tcif@article,
}

\ProcessKeyOptions[ufrgscca-abnt]\relax

%%%%%%%%%%%%%%%%%%%%%
%%% Packages Loading
%%%%%%%%%%%%%%%%%%%%%
\RequirePackage[english,brazilian]{babel}% see ufrgscca.cls in case of newtx* being loaded!
\RequirePackage{csquotes}%

\iftcif@strictabnt
  \RequirePackage[a4paper,inner=2cm,outer=3cm,top=3cm,bottom=2cm,twoside]{geometry}
\else
  \iftcif@nogeometry
%    \RequirePackage[a4paper,margin=5cm,right=2cm,marginparwidth=3.75cm,asymmetric,top=2.5cm,bottom=1.5cm]{geometry}
  \else
    \RequirePackage[a4paper,inner=2.5cm,outer=2.5cm,top=3cm,bottom=2cm,twoside]{geometry}
  \fi
\fi

\RequirePackage[toc,page,title]{appendix}
\RequirePackage[pagestyles,clearempty]{titlesec}
\RequirePackage{titletoc}
\RequirePackage[inline]{enumitem}

\RequirePackage{chngcntr}

\def\tc@captionwidth{0.75\textwidth}
\def\tc@notewidth{0.635\textwidth}  %% 0.84666667\tc@captionwidth
\RequirePackage[position=above,font=small,labelfont=bf,textfont=md,textfont+=sl,width=\tc@captionwidth]{caption}%
%%%
%%% commands for 'floats'
%%%
%
\NewDocumentCommand{\sourcecitation}{m}{{\small{\sourcename\ #1}}}%
%
\newlength{\noteindent}%%auxiliary length
\NewDocumentCommand{\note}{O{}+m}{{\footnotesize\settowidth{\noteindent}{\notename#1:}\addtolength{\noteindent}{3pt}\begin{center}\begin{minipage}{\tc@notewidth}{\parindent=-\the\noteindent\notename#1:\hspace{2.9pt}#2}\end{minipage}\end{center}}}%%
%
%


%%%%%%%%%%%%%%%%%%%%%
%%% Packages Loading
%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{array}
\RequirePackage{nicematrix}
\NiceMatrixOptions{cell-space-limits = 1pt}

\newcolumntype{P}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{B}[1]{>{\raggedleft\arraybackslash\bfseries}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{J}[1]{>{\arraybackslash}p{#1}}


%%%%%%%%%%%%%%%%%%%%%
%%% Packages Loading
%%%%%%%%%%%%%%%%%%%%%
\WarningFilter{biblatex}{File 'brazilian-abnt-abnt.lbx'}%
\WarningFilter{biblatex}{Conflicting options}%
\iftcif@repeatfields%
	\RequirePackage[backend=biber,style=abnt,pretty,giveninits,extrayear,repeatfields]{biblatex}%
\else%
	\RequirePackage[backend=biber,style=abnt,pretty,giveninits,extrayear]{biblatex}%
\fi%
%%
%%%
%%% Language selection {package babel}
%%%
\iftcif@english%%
  \selectlanguage{english}%
  \AtBeginDocument{\selectlanguage{english}}%
\else
  \selectlanguage{brazilian}%
  \AtBeginDocument{\selectlanguage{brazilian}}%
\fi%


%%%
%%% typeset adjusting {package microtype}
%%%
\iftcif@timesroman\else%
\iftcif@nomicrotype\else\RequirePackage[tracking,kerning,spacing,babel]{microtype}\fi%
\fi
%\iftcif@nomicrotype\else\RequirePackage[babel]{microtype}\fi%
%
% pdftex native protruding
%
\pdfprotrudechars=2
\pdfadjustspacing=2

%%%
%%% \so \ul command {package soul}... contour is just to make a fancy underline
%%%
\RequirePackage{contour}
\RequirePackage{soul}


\iftcif@showframes%
  \RequirePackage{showframe}%%
\fi
%%
\iftcif@showlabels%
	\RequirePackage{showlabels}%%
	\showlabels{cite}%%
	\showlabels{textcite}%%
	\RenewDocumentCommand{\showlabelfont}{}{\tiny\ttfamily\color{blue}}%%
\fi%%


%%%
%%% ToC and Headings format. {package titlesec/titletoc/titleps}
%%%

\setcounter{tocdepth}{\tc@tocdepth}  % Table of Contents depth
\setcounter{secnumdepth}{\tc@secdepth} % Sectioning numbers depth (3 -> subsubsection)

\NewDocumentEnvironment{tocless}{}%
   {\setcounter{secnumdepth}{-1}\addtocontents{toc}{\protect\setcounter{tocdepth}{-1}\ignorespaces}}%
   {\setcounter{secnumdepth}{\tc@secdepth}\addtocontents{toc}{\protect\setcounter{tocdepth}{\tc@tocdepth}\ignorespaces}}%
\def\notoc#1#2{\begin{tocless}#1{#2}\end{tocless}}

\NewDocumentEnvironment{numberless}{}{\setcounter{secnumdepth}{-1}}{\setcounter{secnumdepth}{\tc@secdepth}}
\def\nonum#1#2{\begin{numberless}#1{#2}\end{numberless}}

%%%
%%% Relative/Absolute numbering... argh !!!
%%%
  \def\tc@chapterspc{\vspace{2.5mm}}
  \def\tc@sectionspc{\vspace{0.75mm}}
  
\iftcif@article
\else
\iftcif@relnum%
%  \def\tc@chapterspc{\vspace{2.5mm}}
%  \def\tc@sectionspc{\vspace{0.75mm}}
\else%
%  \def\tc@chapterspc{}
%  \def\tc@sectionspc{}
  \patchcmd{\@chapter}{\addtocontents{lof}{\protect\addvspace{10\p@}}}{}{}{{PATCH FAILED}}% LoF ... to supress extra space (figures of diverse chapters)
  \patchcmd{\@chapter}{\addtocontents{lot}{\protect\addvspace{10\p@}}}{}{}{{PATCH FAILED}}% LoT
\fi
\fi


%%%
%%% ToC definitions {package titletoc}
%%%
\def\tc@lblmargin{22mm}
\contentsmargin{2.2em}
\def\tc@dotspacing{0.5pc}
\titlecontents{chapter}[\tc@lblmargin]{\tc@chapterspc\normalfont\scshape\selectfont}{\contentslabel{\tc@lblmargin}}{\hspace{-\tc@lblmargin}}{\titlerule*[\tc@dotspacing]{.}\normalsize\normalfont\contentspage}
\titlecontents{section}[\tc@lblmargin]{\tc@sectionspc}{\contentslabel{\tc@lblmargin}}{\hspace{-\tc@lblmargin}}{\titlerule*[\tc@dotspacing]{.}\contentspage}

\dottedcontents{subsection}[\tc@lblmargin]{}{\tc@lblmargin}{\tc@dotspacing}

\dottedcontents{subsubsection}[\tc@lblmargin]{}{\tc@lblmargin}{\tc@dotspacing}

\dottedcontents{figure}[\tc@lblmargin]{}{\tc@lblmargin}{\tc@dotspacing}
\dottedcontents{table}[\tc@lblmargin]{}{\tc@lblmargin}{\tc@dotspacing}

\renewcommand{\restoreapp}{} % to reset appendix counters each time (from appendix doc)
\RenewDocumentEnvironment{appendix}{O{\defappendixpagename}O{\defappendixname}}{%
       \titleformat{\chapter}{\bfseries\Large\sffamily}{#2 \thechapter\hspace{1.5ex}-}{2ex}{\MakeUppercase}%
			 \titlecontents{chapter}[\tc@lblmargin]{\vspace{3mm}\sc}{\hspace{-\tc@lblmargin}{#2~\thecontentslabel\ -\enspace}}{\hspace{-\tc@lblmargin}}{\titlerule*[\tc@dotspacing]{.}\normalsize\normalfont\contentspage}
		   \def\appendixname{#2}%
			 \def\appendixpagename{#1}%
			 \def\appendixtocname{#1}%
			\begin{appendices}%
			\let\tc@appendixautorefname@tmp\appendixautorefname
			\def\appendixautorefname{#2}
		}%
		{\let\appendixautorefname\tc@appendixautorefname@tmp\end{appendices}}

\NewDocumentEnvironment{annex}{O{\defannexpagename}O{\defannexname}}{\begin{appendix}[#1][#2]}{\end{appendix}}

%%%
%%% Headings definitions {package titlesec}
%%%
%\RenewDocumentCommand{\thechapter}{}{\arabic{chapter}}
\titleformat{\chapter}{\sffamily\bfseries\selectfont\Large}{\thechapter}{2ex}{\MakeUppercase}
\titleformat{name=\chapter,numberless}{\sffamily\bfseries\selectfont\Large\center}{}{0pt}{\MakeUppercase}
\titlespacing{\chapter}{0pt}{\tc@chapbefore}{\tc@chapafter}

%\def\tc@chapbefore{18pt plus 3pt minus 6pt}
%\def\tc@chapafter{9pt plus 3pt minus 3pt}
%\def\tc@secbefore{12pt plus 6pt minus 3pt}
%\def\tc@secafter{6pt plus 2pt minus 2pt}

\def\tc@chapbefore{70pt plus 12pt minus 8pt}
\def\tc@chapafter{36pt plus 8pt minus 8pt}
\def\tc@secbefore{27pt plus 6pt minus 6pt}
\def\tc@secafter{18pt plus 4pt minus 4pt}

\titleformat{\section}[hang]{\normalfont\scshape\selectfont\large}{\thesection}{.5em}{}
\titlespacing{\section}{0pt}{\tc@secbefore}{\tc@secafter}

\titleformat{name=\section,numberless}[hang]{\normalfont\large\bfseries\scshape\selectfont\center}{}{0pt}{}
\titlespacing{name=\section,numberless}{0pt}{\tc@secbefore}{\tc@secafter}

\titleformat{\subsection}[hang]{\normalfont\sffamily\bfseries\slshape\selectfont}{\thesubsection}{.5em}{}
\titlespacing{\subsection}{0pt}{\tc@secbefore}{\tc@secafter}

\titleformat{\subsubsection}[hang]{\normalfont\sffamily\slshape\selectfont}{\thesubsubsection}{.5em}{}
\titlespacing{\subsubsection}{0pt}{\tc@secbefore}{\tc@secafter}

\titleformat{\paragraph}[hang]{\normalfont\sffamily\selectfont}{\theparagraph}{.5em}{\tightul}
\titlespacing{\paragraph}{0pt}{\tc@secbefore}{\tc@secafter}

\titleformat{\subparagraph}[hang]{\normalfont\sffamily\selectfont}{\thesubparagraph}{.5em}{\tightul}
\titlespacing{\subparagraph}{0pt}{\tc@secbefore}{\tc@secafter}

%%%
%%% Relative/Absolute counters... {package chngcntr}
%%%
\iftcif@article
\else
\iftcif@relnum%
  \counterwithin{figure}{chapter}%
  \counterwithin{table}{chapter}%
  \counterwithin{equation}{chapter}%
\else%
  \counterwithout{figure}{chapter}%
  \counterwithout{table}{chapter}%
  \counterwithout{equation}{chapter}%
  %\meaning\@chapter
  %\patchcmd{\@chapter}{\addtocontents{lof}{\protect\addvspace{10\p@}}}{}{}{{PATCH FAILED}}% LoF
  %\patchcmd{\@chapter}{\addtocontents{lot}{\protect\addvspace{10\p@}}}{}{}{{PATCH FAILED}}% LoT
\fi
\fi
%%%
%%% Page Heading format {titlesec/titleps}
%%%
\renewpagestyle{plain}{\sethead[\ifthechapter{\sffamily\thepage}{}][][]{}{}{\ifthechapter{\sffamily\thepage}{}}}
\newpagestyle{main}{\sethead[\ifthechapter{\sffamily\thepage}{}][][]{}{}{\ifthechapter{\sffamily\thepage}{}}}
\newpagestyle{bibliography}{\sethead[\sffamily\thepage][][]{}{}{\sffamily\thepage}}
\pagestyle{main}

\iftcif@chapternopagenum\renewpagestyle{plain}{\sethead[][][]{}{}{}}\fi

\let\tc@temp@printbibliography\printbibliography

\defbibheading{bibliography}[\refname]{\nonum\chapter{#1}\pagestyle{bibliography}}

\iftcif@pretextontoc
    \let\tc@temp@listoffigures\listoffigures
    \def\listoffigures{\tc@temp@listoffigures\addcontentsline{toc}{chapter}{\textsc\listfigurename}}
%%
    \let\tc@temp@listoftables\listoftables
    \def\listoftables{\tc@temp@listoftables\addcontentsline{toc}{chapter}{\textsc\listtablename}}
\fi

%%


\newlength{\tc@lblwidth}
\NewDocumentCommand{\NewChapListEnv}{mm}{
    \NewDocumentEnvironment{#1}{O{}m}{%
        \settowidth{\tc@lblwidth}{##2***}%
        \iftcif@pretextontoc
            \nonum\chapter{\textsc{#2}}%
        \else
            \notoc\chapter{\textsc{#2}}%
        \fi
        \begin{description}[tcc,noindent,leftmargin=\tc@lblwidth,labelwidth=!,##1]%
		}{\end{description}}
}

\NewChapListEnv{listofabbrv}{\listabbrvname}
\NewChapListEnv{listofsymbols}{\listsymbolname}


%%%
%%% lists formating {package enumitem}
%%%

\def\tc@enumbase{6mm}
\def\tc@enummarging{12mm}
\def\tc@enummargingB{18mm}
\def\tc@enumlabelsep{2.5mm}
\setlength{\parindent}{\tc@enummarging}

\SetEnumitemValue{label}{arabic}{\arabic*.}
\SetEnumitemValue{label}{roman}{\roman*.}
\SetEnumitemValue{label}{alpha}{\alph*.}
\SetEnumitemValue{label}{bullet}{\hfill\textopenbullet}

\SetEnumitemKey{ppc}{leftmargin=\tc@enummarging,labelindent=\tc@enumbase,labelsep=\tc@enumlabelsep,labelwidth=!,style=nextline}
\SetEnumitemKey{tcc}{leftmargin=\tc@enummarging,labelindent=\tc@enumbase,labelsep=\tc@enumlabelsep,labelwidth=!,style=nextline}
%
\SetEnumitemKey{parindent}{leftmargin=\tc@enummargingB,labelindent=\tc@enummarging,labelwidth=!}
\SetEnumitemKey{noindent}{labelindent=0pt,labelwidth=!}
\SetEnumitemKey{tight}{leftmargin=0pt,listparindent=5mm,labelwidth=5mm}
%
\SetEnumitemKey{miditemsep}{parsep=0ex,itemsep=0.4ex}

%\SetEnumitemKey{ppctight}{leftmargin=0pt,listparindent=5mm,labelwidth=5mm}


\SetEnumitemKey{arabic}{label=\arabic*.}
\SetEnumitemKey{arabic)}{label=\arabic*)}
\SetEnumitemKey{(arabic)}{label=(\arabic*)}
\SetEnumitemKey{arabic*}{label*=\arabic*.}
\SetEnumitemKey{arabic*)}{label*=\arabic*)}
\SetEnumitemKey{roman}{label=\roman*.}
\SetEnumitemKey{roman)}{label=\roman*)}
\SetEnumitemKey{(roman)}{label=(\roman*)}
\SetEnumitemKey{roman*}{label*=\roman*.}
\SetEnumitemKey{roman*)}{label*=\roman*)}
\SetEnumitemKey{Roman}{label=\Roman*.}
\SetEnumitemKey{Roman)}{label=\Roman*)}
\SetEnumitemKey{(Roman)}{label=(\Roman*)}
\SetEnumitemKey{Roman*}{label*=\Roman*.}
\SetEnumitemKey{Roman*)}{label*=\Roman*)}
\SetEnumitemKey{alpha}{label=\alph*.}
\SetEnumitemKey{alpha)}{label=\alph*)}
\SetEnumitemKey{(alpha)}{label=(\alph*)}
\SetEnumitemKey{alpha*}{label*=\alph*.}
\SetEnumitemKey{alpha*)}{label*=\alph*)}
\SetEnumitemKey{Alpha}{label=\Alph*.}
\SetEnumitemKey{Alpha)}{label=\Alph*)}
\SetEnumitemKey{(Alpha)}{label=(\Alph*)}
\SetEnumitemKey{Alpha*}{label*=\Alph*.}
\SetEnumitemKey{Alpha*)}{label*=\Alph*)}
\SetEnumitemKey{bullet}{label=\hfill\textopenbullet}

%%%
%%% tight underline {packages soul, countour}
%%%
\contourlength{0.17ex}%
\contournumber{96}%

\NewDocumentCommand{\tightul}{m}{%
  \setul{0.355ex}{0.075ex}%
  \ul{{\phantom{{#1}}}}%
  \llap{\contour{white}{#1}}%
}

%%%
%%% keyword command definition (for abstract)
%%%
\newcounter{tc@kwcounter}%
\NewDocumentCommand{\keyword}{m}{%%
        \stepcounter{tc@kwcounter}%%
        \expandafter\gdef\csname keyw\alph{tc@kwcounter}\endcsname{#1}%%
}%
\NewDocumentCommand{\tcabnt@abstractkw}{}{%%
        \@ifundefined{keywa}{}{\expandafter\MakeUppercase\keywa}%%
        \@ifundefined{keywb}{}{, \keywb}%%
        \@ifundefined{keywc}{}{, \keywc}%%
        \@ifundefined{keywd}{}{, \keywd}%
        \@ifundefined{keywe}{}{, \keywe}%
        \@ifundefined{keywf}{}{, \keywf}%
        \@ifundefined{keywg}{}{, \keywg}%
        \@ifundefined{keywh}{}{, \keywh}%
        \@ifundefined{keywi}{}{, \keywi}%
        \@ifundefined{keywj}{}{, \keywj}%
}%

\let\tc@org@abstract\abstract
\let\tc@org@endabstract\endabstract

\RenewDocumentEnvironment{abstract}{}{%%
        \notoc\chapter{\abstractname}%
}{%%
        \par\vspace{2ex} %\vfill%
        \noindent\textbf{\keywordsname:} \tcabnt@abstractkw.%
}%

\NewDocumentEnvironment{otherabstract}{O{english}m}{%%
        \gdef\tc@otherkeywords{#2}%%
        \begin{otherlanguage}{#1}%
        \notoc\chapter{\abstractname}%
}{%
        \par\vspace{2ex} %\vfill%
        \noindent\textbf{\keywordsname:} \tc@otherkeywords.%
        \end{otherlanguage}%
}%



%==============================================================================%
% date redefinition (separated month and year)
%==============================================================================%
\RenewDocumentCommand{\date}{O{\number\day}mm}{%
	\gdef\tc@publmonth{#2}%
	\gdef\tc@publyear{#3}%
	\gdef\tc@publday{\ifnum #1<10 0\fi #1}%
}%
\date{\monthname}{\number\year}%

%for english%
\@namedef{captionsenglish}{%%
 \input{ufrgscca-en-base.def}%%
}%
\@namedef{dateenglish}{%%
	\def\today{\monthname\space\number\day,\space\number\year}%%
	\def\monthname{%%
		\ifcase\month\or%%
			January\or February\or March\or April\or%%
			May\or June\or July\or August\or September\or%%
			October\or November\or December%%
		\fi%%
	}%%
    \iftcif@yearsonly
        \def\tc@date{\tc@publyear}%%
    \else
    	\def\tc@date{\tc@publmonth~\tc@publyear}%%
    \fi
}%
%
%para portugues%
\@namedef{captionsbrazilian}{%%
 \input{ufrgscca-ptBR-base.def}%%
}%
\@namedef{datebrazilian}{%%
	\def\today{\number\day\space de\space\monthname\space de\space\number\year}%%
	\def\monthname{%%
		\ifcase\month\or%%
			Janeiro\or Fevereiro\or Março\or Abril\or%%
			Maio\or Junho\or Julho\or Agosto\or Setembro\or%%
			Outubro\or Novembro\or Dezembro%%
		\fi%%
	}%%
    \iftcif@yearsonly
        \def\tc@date{\tc@publyear}%%
    \else
        \def\tc@date{\tc@publmonth~\tc@publyear}%%
    \fi
}%
%
\def\tc@monthyeardate{\tc@publmonth~\tc@publyear}
%
% Mainly for 'cover pages'
\def\tc@pagebreak{\if@openright\cleardoublepage\else\clearpage\fi}

