%%% File: PhDThesis.cls
%%% Brief: Document class template for ED182 thesis manuscript
%%% Use: \documentclass[showNotes, showLineNumbers, showWatermark]{PhDThesis}
%%% Author: Alexandre BIGOT, alexandre.bigot@iphc.cnrs.fr
%%% Tweaks: Thibault BARNOUIN, thibault.barnouin@astro.unistra.fr

%% Identification
%% The class identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{PhDThesis}[Minimal class for PhD thesis manuscript]

%% Load the article class file
\LoadClass[a4paper,twoside,12pt]{report}

%% Load packages
\RequirePackage{hyperref} % Extensive support for hypertext
\PassOptionsToPackage{a4paper,inner=30mm,outer=20mm,top=25mm,bottom=30mm}{geometry}\RequirePackage{geometry} % Flexible and complete interface to document dimensions
\PassOptionsToPackage{main=english,french}{babel}\RequirePackage{babel} % Multilingual support
\PassOptionsToPackage{T1}{fontenc}\RequirePackage{fontenc} % Standard package for selecting font encodings
\RequirePackage{pdfpages} % Insert pdf as pages
\RequirePackage{lmodern} % Make sure thesis is ready for printing
\RequirePackage{graphicx} % Enhanced support for graphics
\RequirePackage{xcolor} % Driver-independent color extensions
\RequirePackage{amsmath} % AMS mathematical facilities
\RequirePackage{amssymb} % AMS mathematical symbols
\RequirePackage{amsfonts} % TeX fonts from the American Mathematical Society
\RequirePackage{fancyhdr} % Extensive control of page headers and footers
\RequirePackage{caption} % Better captions for tables and figures
\RequirePackage{listings} % For code citing
\RequirePackage{xparse} % For better parameter handling
\PassOptionsToPackage{export}{adjustbox}\RequirePackage{adjustbox} % Framing
% \PassOptionsToPackage{lang-warn=false}{datatool-base}\RequirePackage{datatool-base} % Remove missing datatool warning for french
\PassOptionsToPackage{acronym,toc,nopostdot}{glossaries}\RequirePackage{glossaries} % Create acronym and glossary tables
\PassOptionsToPackage{most}{tcolorbox}\RequirePackage{tcolorbox} % Coloured boxes
\RequirePackage{colortbl} % Coloured row and columns in tables
\PassOptionsToPackage{nottoc}{tocbibind}\RequirePackage{tocbibind}  % Add bibliography/index/contents to Table of Contents
\PassOptionsToPackage{nohints,tight}{minitoc}\RequirePackage{minitoc} % Produce a table of contents for each chapter, part or section
\PassOptionsToPackage{title}{appendix}\RequirePackage{appendix} % Extra control of appendices
\let\oldappendices=\appendices
\renewcommand\appendices{\oldappendices\adjustmtc}
\PassOptionsToPackage{modulo}{lineno}\RequirePackage{lineno} % Line numbers on paragraphs
\RequirePackage{macros} % Personal macros for bibliography

%% Switches
\newif\ifshowNotes

\newif\ifshowLineNumbers
%
%% Class options
\DeclareOption{showNotes}{\showNotestrue}
\DeclareOption{showLineNumbers}{\linenumbers} % show line numbers if switched on
\ProcessOptions\relax % to process options

%%%%%%%%%%%%%%%% Commands and default values for Covers %%%%%%%%%%%%%%%%
% Author name
\makeatletter
\newcommand{\Firstname}[1]{\gdef\@Firstname{#1}}
\Firstname{Firstname}
\newcommand{\Lastname}[1]{\gdef\@Lastname{#1}}
\Lastname{Lastname}
\renewcommand{\author}[2]{\Firstname{#1}\Lastname{#2}\hypersetup{pdfauthor={#1 #2},pdfcreator={#1 #2}}}
% University name and logo
\newcommand{\UniversityName}[1]{\gdef\@UniversityName{#1}}
\UniversityName{Universit\'e de Strasbourg}
\newcommand{\UniversityLogo}[1]{\gdef\@UniversityLogo{#1}}
\UniversityLogo{Images/Covers/Logo\_Unistra.pdf}
\newcommand{\University}[2]{\UniversityName{#1}\UniversityLogo{#2}}
% Labo name and logo
\newcommand{\LaboName}[1]{\gdef\@LaboName{#1}}
\LaboName{My Laboratory}
\newcommand*{\LaboLogo}[1]{\gdef\@LaboLogo{#1}}
\LaboLogo{}
\newcommand{\Labo}[2]{\LaboName{#1}\LaboLogo{#2}}
% Doctoral school name and logo
\newcommand{\DoctoralSchoolName}[1]{\gdef\@DoctoralSchoolName{#1}}
\DoctoralSchoolName{\'Ecole Doctorale de physique et chimie-physique}
\newcommand{\DoctoralSchoolLogo}[1]{\gdef\@DoctoralSchoolLogo{#1}}
\DoctoralSchoolLogo{Images/Covers/logo\_ED182}
\newcommand{\DoctoralSchool}[2]{\DoctoralSchoolName{#1}\DoctoralSchoolLogo{#2}}
% Thesis title and subtitle in French and English
\newcommand{\ThesisDate}[1]{\gdef\@ThesisDate{#1}}
\ThesisDate{XX mois 20XX}
\newcommand{\TitreThese}[1]{\gdef\@TitreThese{#1}}
\TitreThese{Le titre de la thèse en français.}
\newcommand*{\SousTitreThese}[1]{\gdef\@SousTitreThese{#1}}
\SousTitreThese{}
\newcommand{\resume}[1]{\gdef\@resume{#1}}
\resume{1000 caract\`eres maximum}
\newcommand{\motscles}[1]{\gdef\@motscles{#1}}
\motscles{...}
\newcommand{\ThesisTitle}[1]{\gdef\@ThesisTitle{#1}\hypersetup{pdftitle={#1}}}
\ThesisTitle{Title of the thesis in english.}
\newcommand*{\ThesisSubTitle}[1]{\gdef\@ThesisSubTitle{#1}}
\ThesisSubTitle{}
\renewcommand{\abstract}[1]{\gdef\@abstract{#1}}
\abstract{1000 letters max}
\newcommand{\keywords}[1]{\gdef\@keywords{#1}\hypersetup{pdfkeywords={#1}}}
\keywords{...}
\newcommand{\PhDThesis@Abstractmaxchars}[1]{\edef\PhDThesis@Abstractmaxchars{#1}}
\PhDThesis@Abstractmaxchars{1000}
% Discipline
\newcommand{\Discipline}[1]{\gdef\@Discipline{#1}}
\Discipline{Physique}
\newcommand*{\Speciality}[1]{\gdef\@Speciality{#1}}
\Speciality{}
%%% Jury members title, name, position and affiliation
\newcommand{\person}[5]{\hspace{2mm}\bfseries{#1\, \MakeUppercase{#2}\, #3} & {#4, #5}\\}
% Directeurs
\newcommand{\directeurs}[1]{\gdef\@directeurs{#1}}
\directeurs{
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
}
% Rapporteurs
\newcommand{\rapporteurs}[1]{\gdef\@rapporteurs{#1}}
\rapporteurs{
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
}
% Jury
\newcommand{\jury}[1]{\gdef\@jury{#1}}
\jury{
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
    \person{Civ.}{Lastname}{Firstname}{Pos.}{Affiliation}
}
\makeatother

%% Commands to hide parts from TOC
\newcommand{\nocontentsline}[3]{}
\let\origcontentsline\addcontentsline
\newcommand\stoptoc{\let\addcontentsline\nocontentsline}
\newcommand\resumetoc{\let\addcontentsline\origcontentsline}

\NewDocumentEnvironment{notes}{+b}{\ifshowNotes\color{gray}#1\color{black}\fi}{}

%% Define my colours
\definecolor{DarkBlue}{rgb}{0.0, 0.0, 0.55}
\definecolor{DarkRed}{rgb}{0.55, 0.0, 0.0}
\definecolor{DarkGreen}{rgb}{0.0, 0.2, 0.13}
\definecolor{DodgerBlue}{rgb}{0.12, 0.56, 1.0}
\colorlet{DodgerBlue2}{DodgerBlue!80}

%% Color commands
\newcommand{\mycolor}[2]{\color{#1}#2\color{black}\xspace}
\newcommand{\red}[1]{\color{red}#1\color{black}\xspace}

%% Page formating
\newcommand{\clearemptydoublepage}{%
\newpage{\pagestyle{empty}\cleardoublepage}}

%% Change hyperlinks style
\hypersetup
{
    colorlinks=true,
    linkcolor=black,
    linktocpage=false, % to have the link on the page number in toc
    filecolor=magenta,
    urlcolor=DodgerBlue,
    citecolor=DodgerBlue,
}

\makeatother
\let\oldtoc=\tableofcontents
\renewcommand\tableofcontents{\subpdfbookmark{Table of Contents}{name}{\hypersetup{hidelinks}\oldtoc}}
%
\let\oldlof=\listoffigures
\renewcommand\listoffigures{{\hypersetup{hidelinks}\oldlof\mtcaddchapter}}
%
\let\oldlot=\listoftables
\renewcommand\listoftables{{\hypersetup{hidelinks}\oldlot\mtcaddchapter}}
%
\let\oldpgls=\printglossaries
\renewcommand\printglossaries{
    \printglossary[type=\glsdefaulttype,style=altlist,nonumberlist=true]\adjustmtc%
    \printglossary[type=\acronymtype]\adjustmtc%
}

%% Fonts substitutions for non existing bold small caps in lmodern
\rmfamily
\DeclareFontShape{T1}{lmr}{b}{sc}{<->ssub*cmr/b/sc}{}
\DeclareFontShape{T1}{lmr}{bx}{sc}{<->ssub*cmr/bx/sc}{}

\sffamily
\DeclareFontShape{T1}{lmss}{m}{sc} { <-> ssub * phv/m/sc }{}
\DeclareFontShape{T1}{lmss}{bx}{sc} { <-> ssub * phv/bx/sc }{}

%% Table of contents configuration
\setcounter{minitocdepth}{3}
\setlength{\mtcindent}{24pt}
\renewcommand{\mtcfont}{\small}
\renewcommand{\mtifont}{\large\bf}
\renewcommand{\mtcSfont}{\small\bf}
\renewcommand{\mtcSSfont}{\small}
%\renewcommand{\mtctitle}{Contents}

%% Section configuration | Change the numerotations of sections
    % \renewcommand{\thesection}{\Roman{section})}
    % \renewcommand{\thesubsection}{\Roman{section}.\arabic{subsection})}
    % \renewcommand{\thesubsubsection}{\Roman{section}.\arabic{subsection}.\alph{subsubsection})}

%% Command to create a glossary entry with correspondent acronym.
%Syntax: \newdualentry[acronym options]{label}{abbrv}{long}{description}
\DeclareDocumentCommand{\newdualentry}{ O{} m m m m } {
  \newglossaryentry{#2_gls}{name={#3},
      type={\glsdefaulttype},
      long={#4},
      description={#5},
      #1
  }
  \newacronym[description={#4, \textit{Glossary:} \glslink{#2_gls}{#3} ;},#1]{#2}{#3}{#4\glsadd{#2_gls}}
  % \newglossaryentry{#2}{name={#3},
  %     type={\acronymtype},
  %     short={#3},
  %     long={#4},
  %     description={#4, \textit{Glossary:} \glslink{#2_gls}{#3}},
  %     first={#3 (\capitalisewords{#4})\glsadd{#2_gls}},
  %     #1
  % }
}

%% A simple box
\newenvironment{mybox}[0]{%
    \tcolorbox[opacityback=0., standard jigsaw,breakable]}%
    {\endtcolorbox}

%% Change left and right margins (used in front and back covers)
\newenvironment{changemargin}[2]{%
    \begin{list}{}{%
        \setlength{\topsep}{0pt}%
        \setlength{\leftmargin}{#1}%
        \setlength{\rightmargin}{#2}%
        \setlength{\listparindent}{\parindent}%
        \setlength{\itemindent}{\parindent}%
        \setlength{\itemsep}{\parskip}%
        \setlength{\parsep}{\parskip}%
    }%
    \item[]
}{\end{list}}

%% Save deault margin lengths
\newlength{\defaultOddsidemargin}
\setlength{\defaultOddsidemargin}{\oddsidemargin}
\newlength{\defaultEvensidemargin}
\setlength{\defaultEvensidemargin}{\evensidemargin}

% Warning on character count in resume/abstract
\makeatletter
\def\gobblechar{\let\char= }
\newcount\charcount
\def\countunlessnil{%
  \ifx\char\nil \let\next=\relax%
  \else%
    \let\next=\auxcountchar%
    \advance\charcount by 1%
  \fi\next
}%

\def\auxcountchar{%
  \afterassignment\countunlessnil\gobblechar%
}
\def\countchar#1{\edef\xx{#1}\charcount=0 \expandafter\auxcountchar\xx\nil}

\def\shownumchar#1{%
  \countchar{#1}%
  There are \the\charcount\ characters in [#1].%
}
\newcommand{\PhDThesis@checkResume}{%
    \countchar{\@resume}%
    \ifnum\charcount>\PhDThesis@Abstractmaxchars
        \PhDThesis@warningAbstract{resume}{\PhDThesis@Abstractmaxchars}%
    \fi
}
\newcommand{\PhDThesis@checkAbstract}{%
    \countchar{\@abstract}%
    \ifnum\charcount>\PhDThesis@Abstractmaxchars
        \PhDThesis@warningAbstract{abstract}{\PhDThesis@Abstractmaxchars}%
    \fi
}
\newcommand{\PhDThesis@warningAbstract}[2]{%
  \@latex@warning@no@line{your #1 is longer than #2 characters}%
}
