\documentclass\VAR{documentclass_options|default('')}{\VAR{documentclass|default('memoir')}}

\usepackage{amssymb} % Additional math symbols
\usepackage{nicefrac} % For support of: ½, ⅔...
\usepackage{xurl} % For better URL line breaking reduce overfull hbox

\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\VAR{extra_packages}

\usepackage{fancyhdr}

\usepackage{graphicx} % For including images
\usepackage{multicol}
\usepackage{booktabs}
\usepackage{csquotes}
\usepackage{epigraph}
\usepackage{capt-of}
\usepackage{enumitem}
\usepackage{etoolbox}
\usepackage{tocloft}
\usepackage{caption}
\usepackage[colorlinks=true, linkcolor=DarkBlue, urlcolor=DarkBlue, citecolor=DarkBlue]{hyperref}
\usepackage[\VAR{language|default('english')}]{babel}
\usepackage{emptypage}
\usepackage{fixtoc} % Custom package to fix TOC number widths

\graphicspath{{assets/}}

%
% Heading style
%
\chapterstyle{default} % ou ton style préféré (vehttp, ell, etc.)

\renewcommand*{\partnamefont}{\normalfont\sffamily\Huge\bfseries\scshape}
\renewcommand*{\partnumfont}{\normalfont\sffamily\Huge\bfseries}
\renewcommand*{\parttitlefont}{\normalfont\sffamily\Huge\bfseries}

\renewcommand*{\chapnamefont}{\normalfont\sffamily\Large\scshape}
\renewcommand*{\chapnumfont}{\normalfont\sffamily\Huge\bfseries}
\renewcommand*{\chaptitlefont}{\normalfont\sffamily\Huge\bfseries}
\setsecheadstyle{\sffamily\Large\bfseries}
\setsubsecheadstyle{\sffamily\large\bfseries}

%
% Table of contents style
%
\renewcommand{\cftpartfont}{\sffamily\Large\bfseries}
\renewcommand{\cftpartpagefont}{\sffamily\Large\bfseries}
\renewcommand{\cftpartpresnum}{\partname~}

%
% Adjust spacing
%

% Prevent too many overful \hbox (with some fonts)
\tolerance=2000
\emergencystretch=10pt
\hfuzz=1pt
\hbadness=2000

% Give more spaces on Table of content (between numbers and text)
\setlength{\cftpartnumwidth}{4em}
\setlength{\cftsectionnumwidth}{3em}
\setlength{\cftsubsectionnumwidth}{3.5em}
\setlength{\cftfigurenumwidth}{3em}
\setlength{\cfttablenumwidth}{3em}

% Make quotations italic and smaller
\AtBeginEnvironment{displayquote}{\small\itshape}

% Fit table to page width while keeping tabular optional args (alignment)
\let\oldtabular\tabular
\let\endoldtabular\endtabular

\newsavebox{\mytablebox}
\renewenvironment{tabular}[2][c]
  {\begin{lrbox}{\mytablebox}\small\oldtabular[#1]{#2}} % Temporary box to store table
  {\endoldtabular\end{lrbox}%
   \ifdim\wd\mytablebox>\linewidth
     \resizebox{\linewidth}{!}{\usebox{\mytablebox}} % Adjust to fit line width
   \else
     \usebox{\mytablebox} % Otherwise, use the table as is
   \fi
  }

\newif\ifgeometryduplex
\BLOCK{ if geometry_duplex|default(false) }\geometryduplextrue\BLOCK{ else }\geometryduplexfalse\BLOCK{ endif }

%
% Headers and footers
%

% No line on the footer
\renewcommand{\footrulewidth}{0pt}

% Part head pages
\fancypagestyle{part}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
}

% Chapter head pages
\fancypagestyle{plain}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0pt}
  \ifgeometryduplex
    \fancyfoot[LE,RO]{\thepage}
  \else
    \fancyfoot[RO]{\thepage}
  \fi
}

% Mainmatter regular page style
\fancypagestyle{fancy}{%
  \fancyhf{}
  \renewcommand{\headrulewidth}{0.5pt}
  \ifgeometryduplex
    \fancyhead[LE]{\scshape \leftmark}
    \fancyhead[RO]{\scshape \rightmark}
    \fancyfoot[LE,RO]{\thepage}
  \else
    \fancyhead[RO]{\scshape \rightmark}
    \fancyhead[LO]{\scshape \leftmark}
    \fancyfoot[RO]{\thepage}
  \fi
}
\makeatletter
\nouppercaseheads
\renewcommand{\chaptermark}[1]{%
  \markboth{\thechapter\quad #1}{}%
}
\renewcommand{\sectionmark}[1]{%
  \markright{\thesection\quad #1}%
}
\makeatother

% Associate the part page style to the part
\patchcmd{\part}{\thispagestyle{plain}}{\thispagestyle{part}}{}{}

\newcommand{\changelocaltocdepth}[1]{%
  \addtocontents{toc}{\protect\setcounter{tocdepth}{#1}}%
  \setcounter{tocdepth}{#1}%
}

\apptocmd{\frontmatter}{%
  \pagestyle{plain}%
  \changelocaltocdepth{1}%
  \setcounter{secnumdepth}{0}
}{}{}

\apptocmd{\mainmatter}{%
  \pagestyle{fancy}%
  \changelocaltocdepth{2}%
  \setcounter{secnumdepth}{2}
}{}{}

%
% Tweaks
%

% Allow hyphenation after a hyphen
\providecommand{\allowhyphens}{\ifvmode\else\nobreak\hspace{0pt}\fi}

% To number paragraphs and subparagraphs
\setcounter{secnumdepth}{5}

% Paragraph indentation
% \setlength{\parindent}{0pt}
\setlength\parskip{\smallskipamount}

% Avoid orphans at the end of a page, encourage page breaks if there are
% a section with less than 3 lines
% https://tex.stackexchange.com/a/723704/85416
\makeatletter
\patchcmd\@afterheading{\clubpenalty \@M}{\clubpenalties 3 \@M\@M 5000 }{%
  \typeout{patched}}{\typeout{patch failed}
}
\makeatother

% Allow for short text below parts
% https://tex.stackexchange.com/a/336401/85416
% \makeatletter
% \newcommand{\extraPartText}[1]{\def\@extraPartText{#1}}
% \pretocmd{\@endpart}{\vspace{8ex}\begingroup\centering\@extraPartText\par\endgroup\let\@extraPartText\relax}{}{}
% \makeatother

% Description item and text are displayed by default on the same line
% this is not very readable. This command changes the style to display
% the description text on the next line.
% https://tex.stackexchange.com/a/430454/85416
% https://tex.stackexchange.com/a/726879/85416
\AddToHook{cmd/descriptionlabel/after}{\rule[-8pt]{0pt}{0pt}}
\setlist[description]{
  style=nextline,
  labelindent=0pt,
  leftmargin=1cm,
  itemindent=\dimexpr-5pt-\labelsep\relax
}


\newcommand{\booktitle}{\VAR{title}}
\newcommand{\booksubtitle}{\VAR{subtitle}}
\newcommand{\bookauthor}{\VAR{author|default('', true)}}
\newcommand{\bookemail}{\VAR{email}}
\newcommand{\bookpublisher}{\VAR{publisher}}
\newcommand{\bookedition}{\VAR{edition}}
\BLOCK{ set effective_date = date|default('')|trim }
\newcommand{\bookdate}{\BLOCK{ if effective_date }\VAR{date}\BLOCK{ else }\today\BLOCK{ endif }}

\title{\Huge\bfseries\booktitle}
\author{\bookauthor}
\makeatletter
\gdef\@subtitle{\booksubtitle}
\gdef\@authoremail{\bookemail}
\makeatother
\newcommand{\subtitletext}{\booksubtitle}
\newcommand{\authorname}{\bookauthor}
\newcommand{\authoremail}{\bookemail}
\providecommand{\texsmithEmoji}[1]{#1}

\definecolor{indigo(dye)}{rgb}{0.0, 0.25, 0.42}

\BLOCK{ if glossary }\input{glossary}\BLOCK{ endif }

\begin{document}
\hypersetup{pageanchor=false}
\frontmatter
\maketitle
\thispagestyle{empty}
\BLOCK{ if imprint_thanks|trim or imprint_license|trim or imprint_copyright|trim }
\cleardoublepage
\thispagestyle{plain}
\phantomsection
\BLOCK{ if imprint_thanks|trim }
\subsection*{Thanks}
\VAR{imprint_thanks}
\BLOCK{ endif }
\BLOCK{ if imprint_license|trim }
\subsection*{License}
\VAR{imprint_license}
\BLOCK{ endif }
\BLOCK{ if imprint_copyright|trim }
\subsection*{Copyright}
\VAR{imprint_copyright}
\BLOCK{ endif }
\BLOCK{ endif }

\BLOCK{ if dedication|trim }
\cleardoublepage
\thispagestyle{plain}
\phantomsection
\begin{center}
  \vspace*{3cm}
  {\Large\itshape \VAR{dedication}}
\end{center}
\BLOCK{ endif }

\thispagestyle{empty}

\raggedbottom


\BLOCK{ if preface|trim }
\cleardoublepage
\thispagestyle{plain}
\begingroup
\setsecnumdepth{none}
\phantomsection
\VAR{preface}
\endgroup
\BLOCK{ endif }

\VAR{frontmatter}

\clearpage
\tableofcontents*

\hypersetup{pageanchor=true}
\mainmatter

\VAR{mainmatter}

\BLOCK{ if appendix }
\appendix
\VAR{appendix}
\BLOCK{ endif }

\backmatter

\BLOCK{ if listoffigures }
\ifnum\value{figure}>0
\clearpage
\listoffigures
\fi
\BLOCK{ endif }
\BLOCK{ if listoftables }
\ifnum\value{table}>0
\clearpage
\listoftables
\fi
\BLOCK{ endif }

\VAR{backmatter}
\VAR{fragment_backmatter}

\BLOCK{ if colophon|trim }
\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{Colophon}
\chapter*{Colophon}
\VAR{colophon}
\BLOCK{ endif }

\end{document}
