%!TEX root=template.tex

%% ----------------------------------------------------------------------------
%%  novathesis — nt-render-matter.sty
%% ----------------------------------------------------------------------------
%%
%% Version 8.4.0 (2026-09-27)
%% Copyright (C) 2004-26 by João M. Lourenço <joao.lourenco@fct.unl.pt>
%% Departamento de Informática (www.di.fct.unl.pt)
%% Faculdade de Ciências e Tecnologia (www.fct.unl.pt)
%% Universidade NOVA de Lisboa (www.unl.pt)
%%
%% Matter rendering: front/main/back matter components, SDG, abstracts,
%% bibliography, sectioning, acknowledgements, keywords, and final hooks.
%% Loaded by novathesis.cls.
%% ----------------------------------------------------------------------------
\typeout{NT FILE StyFiles/nt-render-matter.sty}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% PAGE GEOMETRY & LAYOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\AtEndPreamble{\ntsetlayout}%

\newcommand*{\ntsetlayout}{%
    % \synctex=1% Use synctex
    \brokenpenalty=10000%
    \newgeometry{
        lmargin=\expanded{\GetMargin(\option{/novathesis/media},left)},
        rmargin=\expanded{\GetMargin(\option{/novathesis/media},right)},
        tmargin=\expanded{\GetMargin(\option{/novathesis/media},top)},
        bmargin=\expanded{\GetMargin(\option{/novathesis/media},bottom)},
    }%
    % \newgeometry recomputes \headheight from scratch, and on some
    % school/font combos the running head content is ~0.9pt taller than
    % that default, triggering a "material used in headers is too large"
    % warning and a repeating "Overfull \vbox" on every page. Grow the
    % head area by 1pt to fit it cleanly; \textheight is left alone, so
    % the text block shifts down 1pt and the 1pt comes out of the bottom
    % margin rather than costing a line of text height.
    \addtolength{\headheight}{1pt}%
    \ifoptionequal{/novathesis/media}{paper}{\openright}{\openany}%
}

% REMOVED in 8.0.4: \ntfixspacing / \@ntfixspacing.
%
% The macro rendered, after every bibliography, a minipage set in white at
% 0.01pt containing a credit line, a \cite of the template and a local
% \printbibliography — invisible to the reader, but present in the text layer.
% It was documented here as a "spacing fix (bibliography anchor)"; it was
% measured before removal, and it was not one: with the macro made a no-op the
% manual builds to exactly the same 109 pages, and a full text diff of the PDF
% shows no change anywhere except the disappearance of the hidden block itself.
%
% It went because text hidden from the reader but present for a crawler is
% indexing-guideline cloaking, and the sanction for it falls on the *student's*
% deposited thesis, not on the template. Screen readers also read it aloud, and
% similarity checkers extract it as body text.
%
% The template's citation is now visible and in the document's real reference
% list, via \@ntcitetemplate below, which is where indexers look anyway.

%% FRONT MATTER COMPONENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\@nt@printifmaindoc@i}[1]{%
  % \typeout{NT Attempt to load #1}%
  \ifmaindoc{% THEN it is a main document
    \NTRunHook{#1/pre}%
    \iffileadded{#1}{%
      \ChapterNoTitle{\GetBkmString(#1,\@LANG@COPYRIGHT)}
      \PrintAddedFile{#1}%
    }{}%
    \NTRunHook{#1/post}%
    \clearforchapter%
  }{}% It is a plan or proposal type document
}

\newcommand{\ntprintdedication}
  {\@nt@printifmaindoc@i{dedication}}

\newcommand{\ntprintquote}
  {\@nt@printifmaindoc@i{quote}}

\newcommand{\ntprintacknowledgements}
  {\@nt@printifmaindoc@i{acknowledgements}}

\newcommand*{\ntprintstatement}{%
  \ifdocworking{}{% Only print statement if provisional or final
    \ifmaindoc{% Skip thesis plan and similar documents
      \iftoggle{/novathesis/print/statement}{% if statement toggle is on
        \NTRunHook{statement/pre}%
        \iffileadded[\@LSHORT]{statement}
          {\PrintAddedFile[\@LSHORT]{statement}}
          {\PrintAddedFile{statement}}%
        \NTRunHook{statement/post}%
      }{}%
    }{}%
  }%
}

\newcommand*{\ntprintfrontpage}{%
  \ifdocworking{}{% Only print institutional statement if provisional or final
    \ifmaindoc{% Skip thesis plan and similar documents
      \iftoggle{/novathesis/print/frontpage}{% if frontpage toggle is on
        \NTRunHook{frontpage/pre}%
        \iffileadded[\@LSHORT]{frontpage}
          {\PrintAddedFile[\@LSHORT]{frontpage}}
          {\PrintAddedFile{frontpage}}%
        \NTRunHook{frontpage/post}%
      }{}%
    }{}%
  }%
}


\newcommand{\ntprintaidisclose}{%
  \ifoptionequal{/novathesis/print/aidisclosure}{aidisclose}{% use aidisclose
    \AIDrenderDeclaration[ncols=3]{\GetDocAuthor(name)}%
  }{\ifoptionequal{/novathesis/print/aidisclosure}{false}
      {} % do not print AI Disclosure
      {\IfFileExists{\DIRFRONTMATTER/\option{/novathesis/print/aidisclosure}}
        {%
          \input{\DIRFRONTMATTER/\option{/novathesis/print/aidisclosure}}%
        }
        {\ClassError{novathesis}
                    {AI Disclusre file not found: '\option{/novathesis/print/aidisclosure}'.}
                    {Please provide a valid AI Disclusre file name.}
        }%
      }%
  }%
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Print Sustainable Development Goals (SDG)
%% To be called from \ntprintabstracts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Row breaking is left to TeX.  The icons are ordinary boxes separated by
% \hspace glue, so the paragraph builder wraps them across as many rows as
% needed, for any icon width, page width or margin setting.
%
% Row separation is controlled through \lineskip rather than an explicit
% "\\[<vspace>]": because every icon box is far taller than the prevailing
% \baselineskip, TeX always falls back to \lineskip between these rows, so
% setting it *is* the vertical gap (this holds under \OnehalfSpacing too).
%
% A previous version tried to break rows by hand, accumulating a running width
% in a \sdgdim register and emitting "\\[<vspace>]" when it overflowed
% \textwidth.  That register was never actually accumulated, so the test read
% "is ONE icon wider than the whole text block?" and could never be true: the
% manual break never fired, and with it the print/sdgs/vspace option silently
% did nothing.  Rows still came out correctly, but only because TeX had been
% doing the wrapping all along.
\newcommand*{\ntprintsdgs}{%
  \ifoptionnil{/novathesis/print/sdgs/list}{}{%
      \vfill%
      \raggedright\noindent%
      % Fall back to the English SDG heading when the abstract language does
      % not define one (e.g. zhs/zht, whose string files are minimal), so that
      % SDGs + a Chinese abstract do not raise a memstore "unknown key" error.
      \textbf{\IfMemVoidTF{SdgString}{\@LSHORT}
                {\GetSdgString(en)}%
                {\GetSdgString(\@LSHORT)}:}\\[1ex]
      \edef\@sdgtype{\option{/novathesis/print/sdgs/type}}
      \ntchecksdgs{\@LSHORT}{\@sdgtype}%
      \IfFileExists{\DIRIMAGES/sdg-\@LSHORT/1-\@sdgtype.pdf}
          {\def\@spnfolder{\DIRIMAGES/sdg-\@LSHORT}}
          {\def\@spnfolder{\DIRIMAGES/sdg-en}}
      \begingroup
        \lineskip=\option{/novathesis/print/sdgs/vspace}\relax
        \noindent
        \optionlistdo{/novathesis/print/sdgs/list}{%
          \includegraphics[width=\option{/novathesis/print/sdgs/size}]
                          {\@spnfolder/##1-\@sdgtype}%
          \hspace{\option{/novathesis/print/sdgs/hspace}}%
        }%
        \par
      \endgroup
  }%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Print Abstracts
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ntprintabstracts}{%
  \NTRunHook{abstracts/pre}%
  \@for\nt@tmpi:=\expanded{\GetAbstractOrder(\@LANG@MAIN)}\do{\@ntprintabstract{\nt@tmpi}}%
  \NTRunHook{abstracts/post}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% Abstract, Acronyms and Glossary
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\ifskipabstract
\newcommand{\@ntprintabstract}[1]{%
  \iffileadded[#1]{abstract}{%
    \skipabstractfalse
    % \typeout{NT PRINT ABSTRACT=[#1]}\NTPRINTABSTRACT@I%
    \ifPDFTeX
      \IfStrEq{#1}{zhs}{\skipabstracttrue}{}%
      \IfStrEq{#1}{zht}{\skipabstracttrue}{}%
    \fi
    \ifskipabstract
      \ClassWarning{novathesis}{Skipping abstract '#1' under pdfLaTeX (CJK Unicode). Use 'make xe' or 'make lua' to include Chinese abstracts.}%
    \else
      % \typeout{NT PRINT ABSTRACT 2=[#1]}\NTPRINTABSTRACT@II
      \begingroup
        % \typeout{NT PRINT ABSTRACT #1}\bbbb
        \StrCut{#1}{-}\@ABS@LANG\@dummy%
        \ntselectlang{\@ABS@LANG}%
        \NTRunHook{abstract/pre}%
        \NTRunHook{abstract/#1/pre}%
        \printabstracttitle{#1}%
        \ifoptionequal{/novathesis/abstract/title}{after}{%
          \begingroup
            \noindent
            \ifoptionequal{/novathesis/abstract/title/align}{left}{\raggedright}{}%
            \ifoptionequal{/novathesis/abstract/title/align}{right}{\raggedleft}{}%
            \ifoptionequal{/novathesis/abstract/title/align}{center}{\centering}{}%
            \NTRunHook{abstract/title/after}%
          \endgroup
        }{}%
        \NTRunHook{abstract/mid}%
        \NTRunHook{abstract/#1/mid}%
        \PrintAddedFile[#1]{abstract}%
        \ntprintsdgs%
        \NTRunHook{abstract/#1/post}%
        \NTRunHook{abstract/post}%
      \endgroup%
      \clearforchapter%
    \fi
  }{}%
}

\newcommand{\ntprintindex}{%
  \iftoggle{/novathesis/print/index}{%
    \printindex%
    \clearforchapter%
  }{}%
}

\newif\if@frontmatter
\newcommand*{\ntthesisfrontmatter}{%
  \NTRunHook{frontmatter/pre}%
  \begingroup
    \def\clearforchapter{}
    \def\cleardoublepage{}
    \def\clearepage{}
    \frontmatter%
  \endgroup
  \@frontmattertrue
  \begingroup
  \pagestyle{novathesis@frontmatter}%
  \copypagestyle{chapter}{novathesis@frontmatter}
  % \ntselectlang{\@LANG@MAIN}%
  % Save the cover-page count before \pagenumbering{roman} resets the counter.
  % Schools with a 2-page front cover (e.g. nova-fct: pages 1-1 + 2-1) leave
  % \c@page = 2 here; front matter must start at page 3 (iii) to avoid a
  % duplicate 'page.ii' hyperref destination.
  \iftoggle{/novathesis/print/frontpage}
    {\edef\nt@lastcoverpage{\number\value{page}}}
    {}%
  \pagenumbering{roman}%
  \iftoggle{/novathesis/print/frontpage}
    {\setcounter{page}{\numexpr\nt@lastcoverpage+1\relax}}
    {\setcounter{page}{1}}
}

\newif\if@mainmatter
\newcommand*{\ntthesismainmatter}{%
  \@frontmatterfalse
  \endgroup
  \NTRunHook{frontmatter/post}%
  \ifbool{@openright}{}{% openany=true
    % Avoid cleardoublepage before "mainmatter"
    \let\oldcleardoublepage\cleardoublepage%
    \renewcommand{\cleardoublepage}{\clearforchapter}%
  }%
  \NTRunHook{mainmatter/pre}%
  \mainmatter%
  \NTRunHook{mainmatter/post}%
  \@mainmattertrue
  \ifbool{@openright}{}{% openany=true
    \let\cleardoublepage\oldcleardoublepage%
  }%
  \pagestyle{novathesis@mainmatter}%
}

\newif\if@backmatter
\newcommand*{\ntthesisbackmatter}{%
  \@backmattertrue
  \NTRunHook{backmatter/pre}%
}
\newcommand{\ntindex}[2][]{%
  % \typeout{NTINDEX [#1] [#2]}
  \bgroup%
    \ifoptioncontains{/novathesis/debug}{index}{\showlabelsinline{#1}{#2}}{}%
    \ifblank{#1}{%
      \def\@nt@index{#2}%
    }{%
      \StrRight{#1}{1}[\@nt@lastchar]%
      \if\@nt@lastchar!\relax
        \def\@nt@index{#1#2}%
      \else
        \def\@nt@index{#1}%
      \fi%
    }%
    \index{\@nt@index}#2%
  \egroup%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% MAIN & BACK MATTER COMPONENTS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \PrintAddedFile{chapter} already loops internally (via \@nt@loadallfiles)
% over every chapter file registered for the current doctype, so a single
% call -- not a per-item \@nt@foralllist wrapper -- is the complete behavior.
\newcommand*{\ntprintchapters}{%
    \PrintAddedFile{chapter}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Appendixes and Annexes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Extension point a chapter style can place next to its chapter number
% (same font, same box) to opt in to the print/appendixname option below.
% Empty by default -- i.e. inert for every style that never references it.
% Styles that already print \@chapapp unconditionally as part of their own
% design (bluebox, elegant: "Chapter"/"Appendix"/"Annex" on every chapter)
% have no reason to reference it at all.
%
% A single \pretocmd{\printchapternum}{\@chapapp\space}{}{} applied equally
% to every style used to live here instead, but several styles (bar, isel,
% ist, ist2, vz34, vz43) don't render \thechapter directly inside
% \printchapternum -- it's nested inside a \hfill-ed box, a \resizebox, or
% deferred entirely into \printchaptertitle -- so prepending text to the
% *call* just floats it at the left margin, disconnected from the number
% (e.g. bar.sty: "Appendix" top-left, the boxed "A" a line below). Each
% style instead places \ntchapnumappendixname itself, exactly where its
% own layout wants the label to sit.
\newcommand*{\ntchapnumappendixname}{}

% When print/appendixname is active, make \ntchapnumappendixname expand to
% "Appendix "/"Annex " for the rest of the appendix/annex section -- the
% PDF bookmark string already contains it (see the \texorpdfstring
% branches of \thechapter in \ntprintappendices/\ntprintannexes below), so
% this only affects what is drawn on the page, never causing a double
% prefix in the bookmarks. This does NOT touch \thechapter itself:
% \thechapter also feeds the chapter mark/running head, and embedding
% \@chapapp (a babel caption) there corrupts \reserved@a (see the comment
% further down).
\newcommand*{\nt@maybeaddappendixname}{%
  \iftoggle{/novathesis/print/appendixname}{%
    \renewcommand*{\ntchapnumappendixname}{\@chapapp\space}%
  }{}%
}

\newcommand{\shiftbookmarks}[2][1]{%
  \begingroup
    \@for\@secname:={part,chapter,section,subsection,subsubsection,paragraph,subparagraph}\do{%
      \expandafter\ifx\csname toclevel@\@secname\endcsname\relax
      \else
        \edef\@currentlevel{\number\csname toclevel@\@secname\endcsname}%
        \expandafter\edef\csname toclevel@\@secname\endcsname{%
          \the\numexpr\@currentlevel+#1\relax
        }%
      \fi
    }%
    #2%
  \endgroup
}
  
% Natural-width chapter number box: renders the label at its natural width
% instead of forcing it into the fixed \cftchapternumwidth box.  Used for
% annexes, where the Roman-numeral counter produces labels of varying width.
% The first argument (#1 = \cftchapternumwidth) is intentionally ignored so
% that "Annex I:" takes exactly as much space as its content requires.
\newcommand{\@nt@naturalnumberlinebox}[2]{\leavevmode\hbox{#2}}

\newcommand*{\ntprintappendices}{%
  \iffileadded{appendix}{%
    \appendix%
    \nt@maybeaddappendixname%
    % Both appendix and annex chapters use toclevel@appendix for their bookmark
    % level (memoir sets \anappendixtrue, which makes \@chapter call
    % \addcontentsline{toc}{appendix}{...}; hyperref then resolves the bookmark
    % depth via toclevel@appendix).  Set it to 1 so appendix chapters appear as
    % level-1 children of "Back Matter" (level 0).
    \expandafter\def\csname toclevel@appendix\endcsname{1}%
    % Write a marker into the .toc file so that when \tableofcontents renders
    % the appendix entries (next pass), \cftchapterpresnum is set to
    % "\appendixname: " — producing "Appendix: A Title" in the TOC.
    % Memoir's \l@appendix routes through \l@chapapp which uses
    % \cftchapterpresnum (not \cftappendixpresnum, which memoir never defines).
    % Using \addtocontents embeds the command sequentially in the .toc file,
    % so the prefix switches correctly between appendix and annex sections.
    % Crucially, \@chapapp / \appendixname is NOT placed in \thechapter's
    % typeset branch: doing so would embed \bbl@ensure@english (or
    % \foreignlanguage) in the running-head mark, which calls \@ifnextchar*
    % and corrupts \reserved@a, breaking the next \chapter call.
    \addtocontents{toc}{%
      \protect\renewcommand{\protect\cftchapterpresnum}%
                           {\protect\appendixname\protect\space}%
      \protect\renewcommand{\protect\cftchapteraftersnum}{:}%
      \protect\renewcommand{\protect\cftchapteraftersnumb}{\protect\space}%
      \protect\settowidth{\protect\cftchapternumwidth}%
        {\protect\cftchapterfont\protect\appendixname\protect\space A:\ }%
    }%
    % \thechapter typeset branch: bare letter "A" — safe in chapter headings,
    % running heads, and \IfStrEq comparisons (no babel-wrapped macro).
    % PDF-string branch: "\appendixname A" — appears in PDF bookmarks.
    \renewcommand{\thechapter}{%
      \texorpdfstring{\Alph{chapter}}{\@chapapp\space\Alph{chapter}:}}%
    % Redefine \thesection to use \Alph{chapter} directly (bypassing the
    % \texorpdfstring in \thechapter), so section bookmarks read "A.1 Title"
    % rather than "Appendix A.1 Title".  Subsection levels inherit this fix
    % automatically because \thesubsection = \thesection.\arabic{subsection}.
    \renewcommand{\thesection}{\Alph{chapter}.\arabic{section}}%
    % See \ntprintchapters: \PrintAddedFile already loops over every
    % registered appendix file internally, so a single call suffices.
    \PrintAddedFile{appendix}%
    % If there are no annexes, reset now; if annexes follow, they will
    % overwrite \cftchapterpresnum with \annexname:\space anyway.
    \iffileadded{annex}{}{%
      \addtocontents{toc}{%
        \protect\renewcommand{\protect\cftchapterpresnum}{}%
        \protect\renewcommand{\protect\cftchapteraftersnum}{}%
        \protect\renewcommand{\protect\cftchapteraftersnumb}{}%
        \protect\setlength{\protect\cftchapternumwidth}{1.5em}%
      }%
    }%
  }{}%
}

\newcommand*{\ntprintannexes}{%
  \iffileadded{annex}{%
    \annex%
    \nt@maybeaddappendixname%
    % Same toclevel@appendix key as for appendices (see comment above).
    \expandafter\def\csname toclevel@appendix\endcsname{1}%
    % Same .toc-marker approach as \ntprintappendices, but switching the
    % presnum to \annexname so annex entries read "Annex: I Title".
    % The reset marker after the list restores defaults for any entries that
    % might follow (e.g. if the document adds more chapter-level TOC entries).
    \addtocontents{toc}{%
      \protect\renewcommand{\protect\cftchapterpresnum}%
                           {\protect\annexname\protect\space}%
      \protect\renewcommand{\protect\cftchapteraftersnum}{:}%
      \protect\renewcommand{\protect\cftchapteraftersnumb}{\protect\space}%
      \protect\settowidth{\protect\cftchapternumwidth}%
        {\protect\cftchapterfont\protect\annexname\protect\space VIII:\ }%
      \protect\let\protect\chapternumberlinebox\protect\@nt@naturalnumberlinebox%
    }%
    % \thechapter typeset branch: bare Roman numeral "I" — safe (no babel macro).
    % PDF-string branch: "\annexname I" — appears in PDF bookmarks.
    \renewcommand{\thechapter}{%
      \texorpdfstring{\annexcounter\c@chapter}{\@chapapp\space\Roman{chapter}:}}%
    % Redefine \thesection to use \Roman{chapter} directly, so section
    % bookmarks read "I.1 Title" rather than "Annex I.1 Title".
    \renewcommand{\thesection}{\Roman{chapter}.\arabic{section}}%
    % See \ntprintchapters: \PrintAddedFile already loops over every
    % registered annex file internally, so a single call suffices.
    \PrintAddedFile{annex}%
    % Reset \cftchapterpresnum and \cftchapternumwidth to their memoir defaults
    % so any chapter-level TOC entries after annexes are not affected.
    \addtocontents{toc}{%
      \protect\renewcommand{\protect\cftchapterpresnum}{}%
      \protect\renewcommand{\protect\cftchapteraftersnum}{}%
      \protect\renewcommand{\protect\cftchapteraftersnumb}{}%
      \protect\setlength{\protect\cftchapternumwidth}{1.5em}%
      \protect\let\protect\chapternumberlinebox\protect\mem@cft@hb@xt@%
    }%
  }{}%
}

\ifdef{\annexcounter}{}{\def\annexcounter{\old@Roman}}
\newcommand*{\annex}{%
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  % Do NOT call \appendix here.  Hyperref's patched \appendix sets
  % \Hy@chapapp to "appendix", which would collide with appendix chapter
  % anchors (both appendix and annex would get "appendix.A" etc.).
  % Instead, manually replicate exactly what memoir's \appendix and
  % hyperref's patch do, substituting "annex" branding throughout.
  \gdef\@chapapp{\annexname}%           % memoir: display name in headings/TOC
  \anappendixtrue%                      % memoir: use appendix-style TOC entries
  \gdef\theHchapter{annex.\Hy@AlphNoErr{chapter}}% hyperref: annex.A, annex.B… — unique vs appendix A,B…
  \xdef\Hy@chapapp{annex}%             % hyperref: anchor prefix → annex.A etc.
  \gdef\chaptername{\annexname}%
  \gdef\thechapter{\annexcounter\c@chapter}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Print statements utils
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{tightcenter}
  {\par\centering}
  {\par}%

\NewDocumentCommand { \TheCandidateSignature } { m m }
  % #1 = place
  % #2 = ISO DATE
  {
    \begin{center}%
      \StatementOneSignature%
        {\expandafter\GetCandidateString\expandafter(\GetDocAuthor(gender),\@LANG@MAIN)}
        {\GetDocAuthor(name)}%
        {#1}% place
        {#2}% date
    \end{center}%
  }
% Deprecated misspelled alias, kept for backwards compatibility.
\NewCommandCopy\TheCandiadteSignature\TheCandidateSignature


\newcounter{nadv}
% \ntforeachperson callback for \TheAdvisersSignatures, curried with
% (space,role,place,date); title/position/institution are ignored, only
% the name and gender feed the signature line.
\newcommand{\@nt@advisersignature}[9]{%
  % #1=space #2=role #3=place #4=date (curried)
  % #5=title #6=name #7=position #8=institution #9=gender
  \stepcounter{nadv}
  \StatementOneSignature[#1]
    {\GetAdviserString(pref,#2,1,#9,\@LANG@MAIN)}
    {#6}
    {#3}% place
    {#4}% date
}
\NewDocumentCommand { \TheAdvisersSignatures } { O{20mm} m m m }
  % #1 = space to signature rule
  % #2 = a (only advisers) | a,c (both advisers and co-advisers)
  % #3 = place
  % #4 = ISO DATE
  {
    \begin{tightcenter}
      \ifnum\ntpersoncount{adviser}{#2}>0\relax%
        \ntforeachperson{adviser}{#2}{\@nt@advisersignature{#1}{#2}{#3}{#4}}%
      \fi%
    \end{tightcenter}
  }


\NewDocumentCommand { \StatementOneSignature } { O{20mm} m m m m }
  % #1 = space to rule
  % #2 = text above
  % #3 = name
  % #4 = place
  % #5 = date
  {
    \begin{minipage}[t]{6cm}
      \medskip%
      \begin{center}
        #2,\par
        \vspace*{#1}
        \rule{\linewidth}{0.75pt}\par
        \textit{#3}\\
        \ifblank{#4}{}{#4,~}\PrintDateISO{#5}
      \end{center}
    \end{minipage}
  }



%% Default list-of bookmark level (1 = direct child of Front/Back Matter group).
%% \ntprintalllistof overrides this to 2 when a "List of..." group header is present.
\def\@nt@lol@bkm@level{1}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Deal with optional lists in the frontmatter: listoftables, litoffigures, etc.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\ntprintalllistof}{%
    \NTRunHook{listof/pre}%
    \if@backmatter
      \def\@matter{backmatter}
    \else
      \def\@matter{frontmatter}
      \iftoggle{/novathesis/tocintoc}
            {\begingroup
               % \tableofcontents (non-starred) creates a \chapter* bookmark that
               % would be shifted by any active \shiftbookmarks.  Temporarily reset
               % toclevel@chapter to 0 so Contents always appears at the root level.
               \expandafter\def\csname toclevel@chapter\endcsname{0}%
               \tableofcontents
             \endgroup}%
            {\bookmark[level=0, dest=\HyperLocalCurrentHref]{\contentsname}
             \tableofcontents*}%
    \fi
    \@nt@listof@ifdefined{\@matter}{
      \edef\@nt@listof@bkmtitle{\GetBkmString(listof,\@LANG@MAIN)}%
      \clearforchapter%
      \phantomsection%
      \bookmark[level=0, dest=\HyperLocalCurrentHref]{\@nt@listof@bkmtitle}%
      \@nt@listof@run{\@matter}{##1}%
    }{}%
    \NTRunHook{listof/post}%
}


% Glossary sources are bib2gls .bib files.  Each file registered with
%     \ntaddfile{glossaries}[<type>]{<name>}
% becomes part of a bib2gls resource; no \write registers are consumed and
% there is no \makeglossaries.  See nt-packages.sty for the record-mode setup.

\newcommand*{\@nt@glssrc}{}

% Add one registered source to the accumulator, refusing 7.10-era .tex files
% with a message that says exactly what to do about it.
\newcommand*{\@nt@glsaddsrc}[1]{%
  \IfFileExists{#1.bib}
    {}
    {\IfFileExists{#1.tex}
       {\ClassError{novathesis}
          {Glossary source '#1.tex' is in the old (7.10.x) format}
          {novathesis 8.0 keeps glossary entries in .bib files, read by
           bib2gls, instead of .tex files read by makeglossaries.^^J
           Run 'make glsbib' to convert your entry files, then CHECK THE SORT
           KEYS: the converter silently drops them, which reorders any entry
           whose name is a macro or math markup.^^J
           See the migration appendix of the manual for the full procedure.}}
       {\ClassError{novathesis}
          {Glossary source '#1.bib' not found}
          {\ntaddfile{glossaries} registered '#1', but neither '#1.bib' nor
           '#1.tex' exists. Check the file name in 0-Config/4_files.tex.}}%
    }%
  \ifdefempty{\@nt@glssrc}
    {\xdef\@nt@glssrc{#1}}
    {\xdef\@nt@glssrc{\@nt@glssrc,#1}}%
}

% Emit one \GlsXtrLoadResources for the given glossary types, if any file was
% registered for them.  #1=comma list of types, #2=bib2gls selection.
\newcommand*{\@nt@glsresource}[2]{%
  \gdef\@nt@glssrc{}%
  \@for\nt@glstype:=#1\do{%
    \iffileadded[\nt@glstype]{glossaries}
      {\@nt@foralllist{file}{glossaries/\nt@glstype}{\@nt@glsaddsrc}}
      {}%
  }%
  \ifdefempty{\@nt@glssrc}{}{%
    \GlsXtrLoadResources[
      src={\@nt@glssrc},
      selection={#2},
      loc-prefix={\ntglslocprefixone,\ntglslocprefixmany},
    ]%
  }%
}

\newcommand*{\@loadglossaryfiles}{%
  % main + symbols reproduce what \glsaddall[types={main,symbols}] used to do:
  % every entry is listed whether or not it is cited.  Abbreviations stay
  % use-based, so unused acronyms do not pad the list.
  \@nt@glsresource{glossary,symbols}{all}%
  \@nt@glsresource{acronym}{recorded and deps}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% BIBLIOGRAPHY & INDEXING
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Add the template's own reference to the document's bibliography.
%
% The entry must land in refsection 0 — the document's *main* reference list —
% because that is the only place that counts: bibliographic indexers (Google
% Scholar among them) count a citation by parsing the reference list of the
% citing document, so a reference confined to a local \refsection, or merely
% printed as a formatted string elsewhere, is invisible to them.
%
% It is issued here, immediately before \printbibliography, rather than from
% \AfterPreamble: at \begin{document} biblatex has not yet read the .bbl, so an
% earlier \nocite raises a spurious "Citation 'novathesis-manual' undefined"
% warning on every single build.
%
% Opt out with \ntsetup{cite/template=false}.
\newcommand*{\@ntcitetemplate}{%
  \nocite{novathesis-manual}%
}

% Tell the author, once per build, that the reference was added and how to
% remove it.  Silent when the option is off (the choice was already made, and
% nagging about it would be rude) and when docstatus=final, so that the build
% of the document actually being submitted carries no chatter.
%
% Every line is prefixed with "[novathesis]" because the Makefile filters the
% engine output through texfot, which discards ordinary \typeout lines; that
% prefix is the pattern texfot is told to --accept, and it is used for nothing
% else.  Without it this message would exist only in the .log, where nobody
% would read it.
\AtEndDocument{%
  \ifdocfinal{}{%
    \iftoggle{/novathesis/cite/template}{%
      \typeout{^^J%
[novathesis] ---------------------------------------------------------^^J%
[novathesis] The reference to the template (v\novathesisversion) was added to your^^J%
[novathesis] bibliography.  novathesis is free, and citations are the only^^J%
[novathesis] currency in which the work is repaid -- please consider^^J%
[novathesis] keeping it.  DOI: 10.5281/zenodo.21938603^^J%
[novathesis]^^J%
[novathesis] ---------------------------------------------------------^^J%
      }%
    }{}%
  }%
}

\newcommand*{\ntprintbibsingle}[1][]{%
  \NTRunHook{printbib/pre}%
  \@ntcitetemplate%
  \printbibliography[#1]%
  \NTRunHook{printbib/post}%
}

\newcommand*{\ntprintbibseparate}[1][]{%
  \NTRunHook{printbib/pre}%
    \@ntcitetemplate%
    \printbibliography[nottype=online]% Print the main bibliography.
    % Print the web references separately, under their own title.
    \printbibliography[title=\option{/novathesis/print/webography},type=online]%
  \NTRunHook{printbib/post}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright page
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\printcompactbib}[1][]{%
  \begingroup
    \renewcommand*{\bibfont}{\tiny}% very small text
    \renewbibmacro*{pageref}{}% suppress backrefs
    % Remove vertical space before the list starts
    \setlength{\topsep}{0pt}
    \setlength{\partopsep}{0pt}
    % Remove vertical space between items (from your previous request)
    \setlength{\bibitemsep}{2pt}
    % fine tune label width
    \setlength{\labelnumberwidth}{10pt}%
    % Print the bibliography with no title, passing any extra options
    % The heading is set white at 0.01pt to occupy the heading slot without
    % showing the word "Bibliography" above a two-item list; the negative
    % vspace then pulls the list tight.  That is layout, and it stays.  The
    % tracking fingerprint that used to be appended to it does not: hidden
    % text is hidden text, whatever it says.
    \defbibheading{ntlocalbib}{%
      {\par\vspace*{-1.25ex}}
      \textcolor{white}
                {\fontsize{0.01}{1}\selectfont\noindent\hspace*{-\labelnumberwidth}
                 \bibname}%
      \vspace*{-\baselineskip-\parskip}
    }
    \newrefcontext[sorting=ynt]%
    \printbibliography[heading=ntlocalbib, #1]
  \endgroup
}

% No longer called automatically from \ntprintcopyright: it used to carry
% the "produced with novathesis" mini-colophon (\SetAcknowledgmentsString),
% which is now empty in every language file -- that credit lives on the
% Colophon page instead, and duplicating it here served no one. The
% mechanism itself (a small footnote-style block + a local citation list)
% stays available for a school to invoke deliberately for its own
% acknowledgements/funding credits, as ulisboa-fmv's statement file
% already shows (commented out): set \SetAcknowledgmentsString to your
% own text and call \ntprintacknowledgementsblock from wherever you want
% it to appear.
\newcommand{\ntprintacknowledgementsblock}[1][\scriptsize]{%
    \NTRunHook{acknowledgementsblock/pre}%
      \refsection%
      \renewcommand{\@ntcite}[1]{\cite{##1}}%
      \bigskip\footnoterule#1\smallskip\noindent\GetAcknowledgmentsString(\@LANG@MAIN)%
      \printcompactbib%
      \endrefsection%
    \NTRunHook{acknowledgementsblock/post}%
}

\newcommand*{\ntprintcopyright}{%
  \ifmaindoc{%
    \iftoggle{/novathesis/print/copyright}{%
      \begingroup
        \ntselectlang{\@LANG@COPYRIGHT}%
        \ChapterNoTitle{\GetBkmString(copyright,\@LANG@COPYRIGHT)}%
        \NTRunHook{copyright/pre}%
        \ntprintcopyrightpage%
        \NTRunHook{copyright/post}%
      \endgroup
    }{}%
   }{}%
}


\newcommand*{\ntprintcopyrightpage}{%
  \ifmaindoc{% only print if a main document
    \iftoggle{/novathesis/print/copyright}{% if copuyright toggle is on
      \InputIfFileExistsInDirsTF {copyright.clo}
        {
          {\DIRSCHOOLS/\@UNIV/\@SCHL},
          {\DIRSCHOOLS/\@UNIV},
          {\DIRSTYFILES},
        }
        {}
        {
          \ClassError{novathesis}
                     {No 'copyright.clo' file was found!}{}
        }
    }{}%
  }{}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Colophon
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A labelled Colophon page in the back matter: the traditional
% book-design place for production details (typesetting engine,
% template, PDF/A status, build date). Prints at every docstatus,
% including working, since it is now the template's only credit line --
% the old copyright-page mini-colophon (\SetAcknowledgmentsString) was
% removed rather than left to duplicate this page (see its own comment
% for schools that still want that block for their own purposes).
%
% Fully multilingual: \GetBkmString(colophon,...) for the page title
% and \GetColophonString / \GetColophonPdfaString /
% \GetColophonNotPdfaString / \GetColophonBuildString (all defined per
% language in novathesisFiles/Strings/strings-*.ldf) for the body, same
% as every other template-generated string, keyed by \@LANG@MAIN. No
% zhs/zht fallback needed: lang/main is a choice restricted to the same
% 14 languages these strings cover, so \@LANG@MAIN can never be zhs/zht
% (those exist only for abstract translations, via a separate mechanism).
%
% Opt out with \ntsetup{print/colophon=false}.
%
% Fixed constant, reused verbatim from the hidden white-text credit this
% colophon replaces (see the "REMOVED in 8.0.4" note near the top of this
% file) -- not a hash of anything computed at build time.
\def\@nt@colophonbuildid{12cc9022}

% Curated display name(s) for the FontStyles/*.sty style currently loaded
% by \ntsetup{style/font=...}. Each FontStyles file registers its own
% name(s) locally, right where it calls \setmainfont/\setromanfont (main),
% \setsansfont (sans), \setmonofont (mono/tt) and \setmathfont or a math
% package (math) -- see any file under novathesisFiles/FontStyles/ for the
% pattern; a font file that doesn't set a given role (e.g. no separate
% math font) simply doesn't touch that macro, and it stays empty.
%
% \FontPrettyName (main) is the only mandatory one: the safety-net
% \IfStrEq check below the font-loading code falls back to the raw
% style/font value itself if it is still empty after loading, so an
% unrecognised/custom school font never breaks the build -- it just
% prints plainly. The sans/mono/math variants are optional and stay
% empty when not set; consumers must check for blankness before use.
\newcommand*{\FontPrettyName}{}
\newcommand*{\FontPrettyNameSans}{}
\newcommand*{\FontPrettyNameMono}{}
\newcommand*{\FontPrettyNameMath}{}

% Optional colophon detail, appended right after \FontPrettyName -- which
% no longer carries its own trailing period (see \SetColophonString in
% strings-*.ldf): this macro supplies the sentence's final "." either way,
% so the colophon always ends in exactly one, never a dangling
% "). (sans: ...)" fragment tacked on after it.
%
% Folds whichever of \FontPrettyNameSans/Mono/Math the loaded font file
% set into ", with A for sans, B for mono and C for math." -- properly
% comma/"and"-joined (2 items use just "and", 3+ use a comma-separated
% list with "and" before the last) via \FormatNamesAsList
% (nt-render-persons.sty), the same joiner already used for adviser/
% committee name lists, so the "and" is pulled from \GetAndString and is
% already correctly localized in all 14 languages. Prints just "." when
% none of the three were set.
%
% The "with .. for sans/mono/math" glue words themselves are deliberately
% left in English across all languages, same as the "sans"/"mono"/"math"
% labels they attach to (see \FontPrettyNameSans and friends above): they
% are typographic jargon, not document prose, and translating a sentence
% frame (as opposed to the single "and" word \FormatNamesAsList already
% localizes for us) was judged out of proportion to what was asked.
\newcommand*{\@nt@colophonfontdetails}{%
  \begingroup
    \def\@nt@cfd@list{}%
    \def\@nt@cfd@sep{}%
    \IfStrEq{\FontPrettyNameSans}{}{}{%
      \edef\@nt@cfd@list{\@nt@cfd@list\@nt@cfd@sep\FontPrettyNameSans{} for sans}%
      \def\@nt@cfd@sep{,}%
    }%
    \IfStrEq{\FontPrettyNameMono}{}{}{%
      \edef\@nt@cfd@list{\@nt@cfd@list\@nt@cfd@sep\FontPrettyNameMono{} for mono}%
      \def\@nt@cfd@sep{,}%
    }%
    \IfStrEq{\FontPrettyNameMath}{}{}{%
      \edef\@nt@cfd@list{\@nt@cfd@list\@nt@cfd@sep\FontPrettyNameMath{} for math}%
    }%
    \IfStrEq{\@nt@cfd@list}{}{.}{,~with~\FormatNamesAsList{\@nt@cfd@list}.}%
  \endgroup
}

\newcommand*{\ntprintcolophon}{%
  \ifmaindoc{%
    \iftoggle{/novathesis/print/colophon}{%
      \chapter*{\GetBkmString(colophon,\@LANG@MAIN)}%
      \NTRunHook{colophon/pre}%
      \begin{center}
        \IfFileExists{\DIRIMAGES/novathesis-insignia-outline.pdf}
          {\includegraphics[width=12mm]{\DIRIMAGES/novathesis-insignia-outline}}
          {}%
      \end{center}
      \bigskip
      \noindent
      \GetColophonString(\@LANG@MAIN)%
      \@nt@colophonfontdetails%
      \par\bigskip
        \fullcite{novathesis-manual} %
      \ifdocfinal{%
        \iftoggle{/novathesis/pdfa}%
          {\GetColophonPdfaString(\@LANG@MAIN)}%
          {\GetColophonNotPdfaString(\@LANG@MAIN)}%
      }{%
        \GetColophonNotPdfaString(\@LANG@MAIN)%
      }%
      \par\bigskip
      {\scriptsize \GetColophonBuildString(\@LANG@MAIN)}%
      \NTRunHook{colophon/post}%
    }{}%
  }{}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Quoting
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\hugequote}{%
  \bgroup%
    \color{black!30}%
    \fontsize{75}{80}\selectfont%
    \fontfamily{ptm}\selectfont%
    \textit{\glqq}%
  \egroup%
}

\newcommand{\ntquoteauthor}[1]{{\\[1.25ex]\footnotesize\bfseries---~#1}}
\newcommand{\ntquotewhere}[1]{{\footnotesize, #1}}
\newcommand{\ntquoteprofession}[1]{{\footnotesize\\[-0.75ex](#1)}}
\newcommand{\ntquotetext}[1]{{\itshape#1}}
\providecommand{\ntchapskip}{%
  \vphantom{%
    \begin{minipage}{\textwidth}%
      \chapter*{ASDF}%
      % \vspace*{\midchapskip}%
    \end{minipage}%
  }%
}

\NewDocumentEnvironment{ntquoting}{sO{10cm}O{}O{}O{}O{\@LANG@MAIN}+b}{%
% 1 [*] => print big quote
% 2 [o] => max width of citation box
% 3 [o] => author name
% 4 [o] => where/source
% 5 [o] => profession
% 6 [o] => lang
% 7 [b] => quotation
  % \typeout{
  %          1=[#1]
  %          2=[#2]
  %          3=[#3]
  %          4=[#4]
  %          5=[#5]
  %          6=[#6]
  %          7=[#7]
  % }\asdfg
  \thispagestyle{novathesis@frontmatter}%
  \defifundef{\ntqbox}{\newsavebox}%
  \savebox{\ntqbox}{#7}
  \ifdim\wd\ntqbox<\ifblank{#2}{\textwidth}{#2}\relax
  \@tempdima=\wd\ntqbox
  \else
  \@tempdima=#2
  \fi
  \ntchapskip
  \begin{flushright}
    \NTRunHook{ntquote/pre}%
    \IfBooleanTF{#1}{\hugequote}{}
    \begin{minipage}[t]{\@tempdima}
      \raggedleft\normalsize
      % \options{/@nt/lang/shorttolong=#6}%
      \IfBooleanTF{#1}{%
        \foreigntextquote{\GetLangLong(#6)}{\ntquotetext{#7}}%
      }{\ntquotetext{#7}}
      \ifblank{#3}{}{\ntquoteauthor{#3}}%
      \ifblank{#4}{}{\ntquotewhere{#4}}%
      \ifblank{#5}{}{\ntquoteprofession{#5}}%
    \end{minipage}%
    \NTRunHook{ntquote/post}%
  \end{flushright}
}{}

\ExplSyntaxOn
\cs_if_exist:NF \endntquoting { \cs_new_protected:Nn \endntquoting { } }
\cs_new:cpn { ntquote } { \ntquoting* }
\cs_new_eq:cN { endntquote } \endntquoting
\cs_new:cpn { ntdedication } { \ntquoting }
\cs_new_eq:cN { endntdedication } \endntquoting
\ExplSyntaxOff

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Acknowledgements
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{ntacknowledgements}{%
  \chapter*{\GetBkmString(acknowledgements,\@LANG@MAIN)}%
}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Abstract
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Font names below use "~" instead of literal spaces: under ExplSyntaxOn,
% space (catcode 9) is ignored, but "~" is catcode 10 (an ordinary
% interword space), so multi-word font names survive untouched. This is
% safe here because IfFontExistsTF/newfontfamily only query the OS font
% backend with a string -- unlike \InputIfFileExists/\input/\RequirePackage
% elsewhere in this file, nothing here reads and tokenizes an external
% file's contents under these catcodes.
\ExplSyntaxOn
\IfSubStr{,\@LANG@LOADED,}{,zhs,}{%
  \iftutex
    \ifcsname nt@cjkfontSC\endcsname\else
      \IfFontExistsTF{PingFang~SC}
        {\newfontfamily\nt@cjkfontSC{PingFang~SC}}
        {\IfFontExistsTF{Noto~Sans~CJK~SC}
          {\newfontfamily\nt@cjkfontSC{Noto~Sans~CJK~SC}}
          {\IfFontExistsTF{Heiti~SC}
            {\newfontfamily\nt@cjkfontSC{Heiti~SC}}
            {\newfontfamily\nt@cjkfontSC{Latin~Modern~Roman}}%
          }%
        }%
    \fi
  \fi
}{}
\IfSubStr{,\@LANG@LOADED,}{,zht,}{%
  \iftutex
    \ifcsname nt@cjkfontTC\endcsname\else
      \IfFontExistsTF{PingFang~TC}
        {\newfontfamily\nt@cjkfontTC{PingFang~TC}}
        {\IfFontExistsTF{Noto~Sans~CJK~TC}
          {\newfontfamily\nt@cjkfontTC{Noto~Sans~CJK~TC}}
          {\IfFontExistsTF{Heiti~TC}
            {\newfontfamily\nt@cjkfontTC{Heiti~TC}}
            {\newfontfamily\nt@cjkfontTC{Latin~Modern~Roman}}%
          }%
        }%
    \fi
  \fi
}{}
\ExplSyntaxOff

\newcommand*{\printabstracttitle}[1]{%
  \begingroup
    \def\nt@abstitlefont{}%
    \iftutex
      \IfStrEq{#1}{zhs}{\def\nt@abstitlefont{\nt@cjkfontSC}}{}%
      \IfStrEq{#1}{zht}{\def\nt@abstitlefont{\nt@cjkfontTC}}{}%
    \fi
    \ifoptionequal{/novathesis/abstract/title}{before}{%
      \preto\chapterheadstart{%
        \begingroup
          \noindent
          \ifoptionequal{/novathesis/abstract/title/align}{left}{\raggedright}{}%
          \ifoptionequal{/novathesis/abstract/title/align}{right}{\raggedleft}{}%
          \ifoptionequal{/novathesis/abstract/title/align}{center}{\centering}{}%
          \NTRunHook{abstract/title/before}%
        \endgroup
      }%
    }{}%
    \chapter{\texorpdfstring{{\nt@abstitlefont\GetBkmString(abstract,#1)}}{\GetBkmString(abstract,#1)}}%
  \endgroup
}

% Default work-title block used by \ntsetup{abstract/title=before|after}.
% Schools needing custom styling should populate the hook directly instead.
\newcommand{\ntprintabstracttitledefault}{%
  \large\bfseries
  \IfMemVoidTF{DocTitle}{\@LSHORT,abstract}
    {\GetDocTitle(\@LSHORT,main)}%
    {\GetDocTitle(\@LSHORT,abstract)}%
  \par
  \smallskip
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Keywords
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\providecommand{\keywords}[1]{%
    \NTRunHook{keywords/pre}%
    % \NTRunHook{keywords/#1/pre}%
    \def\and{\unskip\MemGetDefault{KeywordsSep}[]{\@LSHORT}}%
    % \def\and{{\textperiodcentered} }%
    % Store comma-separated plain-text copy for PDF metadata.
    % \MemSet* is global (prop_gput), so storage survives the \begingroup.
    \begingroup
      \def\and{, }%
      \MemSet*{DocKeywords}{\@LSHORT}{#1}%
    \endgroup
    % Set the PDF info dict Keywords entry here (outside any group) so the
    % expanded string \@nt@pdfkwds is available at macro expansion time.
    %
    % WHY NOT \hypersetup: hyperref writes /Keywords via \PDF@FinishDoc, which
    % runs at the FIRST-PAGE shipout (hook shipout/firstpage). In novathesis the
    % abstract/keywords pages come after the first page, so \hypersetup called
    % here would be silently ignored (the option is disabled by then).
    %
    % Both \pdfextension info (LuaLaTeX) and \pdfinfo (pdfLaTeX) accumulate
    % info-dict entries; the last value for each key wins when the PDF is
    % finalised. Calling here (body time, after the first shipout) correctly
    % overwrites the empty /Keywords entry written by hyperref at first-page
    % shipout. XeLaTeX would need a \special{pdf:info ...} equivalent.
    \IfStrEq{\@LSHORT}{\@LANG@MAIN}{%
      \begingroup
        \def\and{, }%
        \protected@edef\@nt@pdfkwds{\MemGet{DocKeywords}{\@LANG@MAIN}}%
        \ifluatex
          \pdfextension info{/Keywords(\@nt@pdfkwds)}%
        \else\ifpdftex
          \pdfinfo{/Keywords(\@nt@pdfkwds)}%
        \fi\fi
      \endgroup
    }{}%
    \par\addvspace\baselineskip\noindent%
    \begingroup%
    \MemGetDefault{KeywordsStringPrefix}[]{\@LSHORT}%
    \MemGetDefault{KeywordsStringFont}[]{\@LSHORT}%
    \GetKeywordsString(\@LSHORT)%
    \MemGetDefault{KeywordsStringSuffix}[]{\@LSHORT}%
    \endgroup%
    % \enspace
    \ignorespaces#1%
    % \NTRunHook{keywords/#1/post}%
    \NTRunHook{keywords/post}%
}%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LABELS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\providecommand\thepresentationtext{%
  \GetDissertationStringFont()%
  \IfMemVoidTF{DissertationString}{\@DOCTYPE,\@LANG@COVER}{%
    \GetDissertationString(\@DOCCLASS,\@LANG@COVER)%
  }{%
    \GetDissertationString(\@DOCTYPE,\@LANG@COVER)%
  }%
}

\providecommand\thecopyrightstr{%
     {\thispagestyle{novathesis@frontmatter}}%
     % {\thispagestyle{empty}}%
    \noindent%
    Copyright \textcopyright\ \GetDocAuthor(name), %
    \GetSchool(\@LANG@COPYRIGHT), \GetUniversity(\@LANG@COPYRIGHT).\\%
    \GetCopyrightTextString(\@LANG@COPYRIGHT)%
    % \ntselectlang{\@LANG@MAIN}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Table of contents
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\setcounter{tocdepth}{2}

% ---------------------------------------------------------------------
% ToC Spacing Hack: Tighter spacing for Front Matter, normal for Main
% ---------------------------------------------------------------------

% 1. When Front Matter starts, write a command to the .toc file to REDUCE spacing
\appto\frontmatter{%
  \let\cftbeforechapterskipORIG\cftbeforechapterskip
  \addtocontents{toc}{\protect\setlength{\cftbeforechapterskip}{3pt}}%
}

% 2. When Main Matter starts, write a command to the .toc file to RESTORE spacing
% (Memoir default is usually 1.0em plus 1pt)
\appto\mainmatter{%
  \addtocontents{toc}{\protect\setlength{\cftbeforechapterskip}{1.0em plus 1pt}}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Sectioning
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\maxsecnumdepth{subsubsection}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% FINAL HOOKS & FIXES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fixes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Fix spacing for pages with roman numerals in ToC
\renewcommand*{\@pnumwidth}{2.5em}% default is 1.55em

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Text style
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% First try to load a local chapter-style definition
\InputIfFileExists{\DIRCHAPSTYLES/\option{/novathesis/style/chapter}.sty}
  {}{% otherwise, try a standard 'memoir' style
      \ifcsdef{chs@\option{/novathesis/style/chapter}}
          {\chapterstyle{\option{/novathesis/style/chapter}}}
          {\ClassError{novathesis}{Invalid Chapter Style: \option{/novathesis/style/chapter}}{}}%
}

\IfFileExistsCase
  {
    {\DIRFONTSTYLES/\option{/novathesis/style/font}.sty}
          {\input{\ntfoundpath}},
    {\option{/novathesis/style/font}.sty}
          {\RequirePackage{\ntfoundpathnoext}},        
  }
  {
    \ifcsname IfFontExistsTF\endcsname
      \IfFontExistsTF{\option{/novathesis/style/font}}
        {\setmainfont{\option{/novathesis/style/font}}
                     [ \option{/novathesis/style/font/attributes} ]}
        {\ClassError{novathesis}{Could not find the font: \option{/novathesis/style/font}}{}}
    \else
      \ClassError{novathesis}
                 {Cannot load the font: \option{/novathesis/style/font}. Are you using XeLaTeX or LuaLaTeX?}{}
    \fi
  }

% Safety net for the mandatory main-font name: a raw font name typed
% directly in style/font (the \setmainfont branch above) or a school's
% own FontStyles-less custom font never sets \FontPrettyName, so fall
% back to the raw option value itself.
\IfStrEq{\FontPrettyName}{}
  {\renewcommand*{\FontPrettyName}{\option{/novathesis/style/font}}}
  {}


% force page color to white in xelatex and lualatex
\ifxeorlua{\definecolor{nearlywhite}{gray}{0.999999}\colorlet{white}{nearlywhite}}{}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% LISTINGS + MINTED UNIFICATION
%% Goal: one counter, one list, regardless of which package(s) are loaded.
%%
%% IMPORTANT: listings.sty defers \newcounter{lstlisting} to \AtBeginDocument,
%% so counter aliasing must run AFTER that hook completes.
%% \AddToHook{begindocument/end} fires after ALL \AtBeginDocument hooks.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\AddToHook{begindocument/end}{%
  \makeatletter
    % 1. Helper for TOC/Bookmark injection
    \newcommand{\update@lol}[1]{%
      % \cleardoublepage
      % \phantomsection
      % \addcontentsline{toc}{chapter}{\listoflistingscaption}%
      #1%
    }

    % 2. Helper to set numbering to Chapter.Sequence
    % This defines \thelisting as \thechapter.\arabic{listing} 
    % and ensures the counter resets when \chapter is called.
    \newcommand{\setup@listing@numbering}[1]{%
      \@addtoreset{#1}{chapter}%
      \expandafter\renewcommand\csname the#1\endcsname{\thechapter.\arabic{#1}}%
    }

    \@ifpackageloaded{listings}{%
      \@ifpackageloaded{minted}{%
        % --- CASE: BOTH LOADED ---
        \let\old@listoflistings\listoflistings
        \renewcommand{\listoflistings}{\update@lol{\old@listoflistings}}
        \let\lstlistoflistings\listoflistings
      
        % Setup numbering for 'listing' (minted's counter)
        \setup@listing@numbering{listing}
        % Unify 'listings' to follow 'minted'
        \let\c@lstlisting\c@listing
        \let\thelstlisting\thelisting
      }{%
        % --- CASE: ONLY LISTINGS LOADED ---
        \ifx\listoflistingscaption\@undefined
          \let\listoflistingscaption\lstlistlistingname
        \fi
        \let\old@lstlistoflistings\lstlistoflistings
        \renewcommand{\lstlistoflistings}{\update@lol{\old@lstlistoflistings}}
        \let\listoflistings\lstlistoflistings
      
        % Setup numbering for 'lstlisting'
        \setup@listing@numbering{lstlisting}
      }%
    }{%
      \@ifpackageloaded{minted}{%
        % --- CASE: ONLY MINTED LOADED ---
        \let\old@listoflistings\listoflistings
        \renewcommand{\listoflistings}{\update@lol{\old@listoflistings}}
      
        % Setup numbering for 'listing'
        \setup@listing@numbering{listing}
      }{%
        % NEITHER LOADED
      }%
    }%
  \makeatother
}

\NTAddToHook{mainmatter/post}{%
  \ifoptionequal{/novathesis/numbering/floats}{sequential}{%
    \counterwithout{figure}{chapter}%
    \counterwithout{table}{chapter}%
    \counterwithout{footnote}{chapter}%
    \counterwithout{equation}{chapter}%
    \@ifundefined{c@algorithm}{}{\counterwithout{algorithm}{chapter}}%
    %% Counter unification (listings+minted) is handled at \AtBeginDocument above.
    %% Here we only need the \counterwithout calls.
    \@ifpackageloaded{minted}{%
      \@ifundefined{c@listing}{}{\counterwithout{listing}{chapter}}%
    }{%
      \@ifpackageloaded{listings}{%
        \@ifundefined{c@lstlisting}{}{\counterwithout{lstlisting}{chapter}}%
      }{}%
    }%
  }{}%
}


\AtBeginDocument{%
  \makeatletter%
  % \typeout{XXX [\GetDocAuthor(name)]}
  % \typeout{XXX [\GetDocTitle(\@LANG@MAIN,main,plain)]}
  % \typeout{XXX [\GetDocTitle(\@LANG@MAIN,sub,plain)]}
  \hypersetup{%
    pdfauthor={\GetDocAuthor(name)},
    pdftitle={\GetDocTitle(\@LANG@MAIN,main,plain)},
    pdfsubject={\GetDocTitle(\@LANG@MAIN,sub,plain)},
  }

  %--------------------------------
  % If subcaption is loaded, let it handle subfigures
  % otherwise use memoir subfigures
  \@ifpackageloaded{subcaption}{}{\newsubfloat{figure}}
  \newcommand{\@ntcite}[1]{\ifdef{\parencite}{\parencite{#1}}{\cite{#1}}}
  \makeatother  
}

% PDF Keywords are set inside \keywords{} via \pdfextension info (LuaLaTeX)
% or \pdfinfo (pdfLaTeX) during body processing, which overwrites the empty
% /Keywords entry that hyperref's \PDF@FinishDoc writes at first-page shipout.
