masters-thesis

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

kao.sty (52609B)


      1 \ProvidesPackage{kao}
      2 
      3 %----------------------------------------------------------------------------------------
      4 %	DECLARE KAO
      5 %----------------------------------------------------------------------------------------
      6 
      7 \DefineFamily{kao}% Define the family name
      8 \DefineFamilyMember{kao}% Add a member to the family
      9 \DefineFamilyKey{kao}{secnumdepth}[1]{\setcounter{secnumdepth}{#1}\FamilyKeyStateProcessed}% Define a key and a default value
     10 \FamilyProcessOptions{kao}% Process the options
     11 
     12 %----------------------------------------------------------------------------------------
     13 %	USEFUL PACKAGES AND COMMANDS
     14 %----------------------------------------------------------------------------------------
     15 
     16 \RequirePackage{kvoptions} % Handle package options
     17 \RequirePackage{etoolbox} % Easy programming to modify TeX stuff
     18 \RequirePackage{calc} % Make calculations
     19 \RequirePackage[usenames,dvipsnames,table]{xcolor} % Colours
     20 \RequirePackage{iftex} % Check whether XeTeX is being used
     21 \RequirePackage{xifthen} % Easy conditionals
     22 \RequirePackage{options} % Manage class options
     23 \RequirePackage{xparse} % Parse arguments for macros
     24 \RequirePackage{xpatch} % Patch LaTeX code in external packages
     25 \RequirePackage{xstring} % Parse strings
     26 \RequirePackage{afterpage} % Run commands after specific pages
     27 \RequirePackage{imakeidx} % For the index; must be loaded before the 'caption' and 'hyperref' packages
     28 \RequirePackage{varioref} % For the cross-references; must be loaded before the 'hyperref' and 'cleveref' packages
     29 \AtEndPreamble{\RequirePackage{scrhack}} % Make some packages compatible with KOMAScript
     30 
     31 % Define \Ifthispageodd (with a capital 'i') to make kaobook compatible with older KOMAScript versions
     32 \@ifpackagelater{scrbase}{2019/12/22}{%
     33 }{%
     34 	\let\Ifthispageodd\ifthispageodd%
     35 }
     36 
     37 %----------------------------------------------------------------------------------------
     38 %	KAO-SPECIFIC OPTIONS
     39 %----------------------------------------------------------------------------------------
     40 
     41 % Set up the package options
     42 \SetupKeyvalOptions{
     43 	family = kao,
     44 	prefix = kao@
     45 }
     46 
     47 % https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation
     48 % Introduce a command to find out whether the compiler is XeTeX or LuaTeX
     49 \newif\ifxetexorluatex
     50 \ifxetex
     51 	\xetexorluatextrue
     52 \else
     53 	\ifluatex
     54 		\xetexorluatextrue
     55 	\else
     56 		\xetexorluatexfalse
     57 	\fi
     58 \fi
     59 
     60 \ifxetexorluatex
     61 	\newcommand{\kao@defaultfontmethod}{modern}
     62 \else
     63 	\newcommand{\kao@defaultfontmethod}{tex}
     64 \fi
     65 
     66 % Set default based on rendering engine
     67 \DeclareStringOption[\kao@defaultfontmethod]{fontmethod}
     68 
     69 \ProcessKeyvalOptions{kao}
     70 
     71 %----------------------------------------------------------------------------------------
     72 %	TITLE AND AUTHOR MACROS
     73 %----------------------------------------------------------------------------------------
     74 
     75 % Provide an optional argument to the \title command in which to store a plain text title, without any formatting
     76 % Usage: \title[Plain Title]{Actual Title}
     77 \newcommand{\@plaintitle}{}
     78 \renewcommand{\title}[2][]{%
     79 	\gdef\@title{#2} % Store the full title in @title
     80 	\ifthenelse{\isempty{#1}}{ % If there is no plain title
     81 		\renewcommand{\@plaintitle}{\@title} % Use full title
     82 	}{ % If there is a plain title
     83 		\renewcommand{\@plaintitle}{#1} % Use provided plain-text title
     84 	}%
     85 	\hypersetup{pdftitle={\@plaintitle}} % Set the PDF metadata title
     86 }
     87 
     88 % Provide an optional argument to the \author command in which to store a plain text author, without any formatting
     89 % Usage: \author[Plain Author]{Actual Author}
     90 \newcommand{\@plainauthor}{}
     91 \renewcommand{\author}[2][]{%
     92 	\gdef\@author{#2} % Store the full author in @author
     93 	\ifthenelse{\isempty{#1}}{ % If there is no plain author
     94 		\renewcommand{\@plainauthor}{\@author}% Use full author
     95 	}{ % If there is a plain author
     96 		\renewcommand{\@plainauthor}{#1}% Use provided plain-text author
     97 	}%
     98 	\hypersetup{pdfauthor={\@plainauthor}} % Set the PDF metadata author
     99 }
    100 
    101 % Make a bookmark to the title page
    102 \pretocmd{\maketitle}{\pdfbookmark[1]{\@plaintitle}{title}}{}{}%
    103 
    104 %----------------------------------------------------------------------------------------
    105 %	PAGE LAYOUT
    106 %----------------------------------------------------------------------------------------
    107 
    108 % Define lengths to set the scale of the document. Changing these 
    109 % lengths should affect all the other pagesize-dependent elements in the 
    110 % layout, such as the geometry of the page, the spacing between 
    111 % paragraphs, and so on. (As of now, not all the elements rely on hscale 
    112 % and vscale; future work will address this shortcoming.)
    113 \newlength{\hscale}
    114 \newlength{\vscale}
    115 
    116 % By default, the scales are set to work for a4paper
    117 \setlength{\hscale}{1mm}
    118 \setlength{\vscale}{1mm}
    119 
    120 % Define hscale and vscale for all types of paper
    121 \@ifclasswith{\@baseclass}{a0paper}{\setlength{\hscale}{4mm}\setlength{\vscale}{4mm}}{}
    122 \@ifclasswith{\@baseclass}{a1paper}{\setlength{\hscale}{2.828mm}\setlength{\vscale}{2.828mm}}{}
    123 \@ifclasswith{\@baseclass}{a2paper}{\setlength{\hscale}{2mm}\setlength{\vscale}{2mm}}{}
    124 \@ifclasswith{\@baseclass}{a3paper}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{1.414mm}}{}
    125 \@ifclasswith{\@baseclass}{a4paper}{\setlength{\hscale}{1mm}\setlength{\vscale}{1mm}}{}
    126 \@ifclasswith{\@baseclass}{a5paper}{\setlength{\hscale}{0.704mm}\setlength{\vscale}{0.704mm}}{}
    127 \@ifclasswith{\@baseclass}{a6paper}{\setlength{\hscale}{0.5mm}\setlength{\vscale}{0.5mm}}{}
    128 \@ifclasswith{\@baseclass}{b0paper}{\setlength{\hscale}{4.761mm}\setlength{\vscale}{4.761mm}}{}
    129 \@ifclasswith{\@baseclass}{b1paper}{\setlength{\hscale}{3.367mm}\setlength{\vscale}{3.367mm}}{}
    130 \@ifclasswith{\@baseclass}{b2paper}{\setlength{\hscale}{2.380mm}\setlength{\vscale}{2.380mm}}{}
    131 \@ifclasswith{\@baseclass}{b3paper}{\setlength{\hscale}{1.681mm}\setlength{\vscale}{1.681mm}}{}
    132 \@ifclasswith{\@baseclass}{b4paper}{\setlength{\hscale}{1.190mm}\setlength{\vscale}{1.190mm}}{}
    133 \@ifclasswith{\@baseclass}{b5paper}{\setlength{\hscale}{0.837mm}\setlength{\vscale}{0.837mm}}{}
    134 \@ifclasswith{\@baseclass}{b6paper}{\setlength{\hscale}{0.570mm}\setlength{\vscale}{0.570mm}}{}
    135 \@ifclasswith{\@baseclass}{c0paper}{\setlength{\hscale}{4.367mm}\setlength{\vscale}{4.367mm}}{}
    136 \@ifclasswith{\@baseclass}{c1paper}{\setlength{\hscale}{3.085mm}\setlength{\vscale}{3.085mm}}{}
    137 \@ifclasswith{\@baseclass}{c2paper}{\setlength{\hscale}{2.180mm}\setlength{\vscale}{2.180mm}}{}
    138 \@ifclasswith{\@baseclass}{c3paper}{\setlength{\hscale}{1.542mm}\setlength{\vscale}{1.542mm}}{}
    139 \@ifclasswith{\@baseclass}{c4paper}{\setlength{\hscale}{1.090mm}\setlength{\vscale}{1.090mm}}{}
    140 \@ifclasswith{\@baseclass}{c5paper}{\setlength{\hscale}{0.771mm}\setlength{\vscale}{0.771mm}}{}
    141 \@ifclasswith{\@baseclass}{c6paper}{\setlength{\hscale}{0.542mm}\setlength{\vscale}{0.542mm}}{}
    142 \@ifclasswith{\@baseclass}{b0j}{\setlength{\hscale}{4.904mm}\setlength{\vscale}{4.904mm}}{}
    143 \@ifclasswith{\@baseclass}{b1j}{\setlength{\hscale}{3.467mm}\setlength{\vscale}{3.467mm}}{}
    144 \@ifclasswith{\@baseclass}{b2j}{\setlength{\hscale}{2.452mm}\setlength{\vscale}{2.452mm}}{}
    145 \@ifclasswith{\@baseclass}{b3j}{\setlength{\hscale}{1.733mm}\setlength{\vscale}{1.733mm}}{}
    146 \@ifclasswith{\@baseclass}{b4j}{\setlength{\hscale}{1.226mm}\setlength{\vscale}{1.226mm}}{}
    147 \@ifclasswith{\@baseclass}{b5j}{\setlength{\hscale}{0.867mm}\setlength{\vscale}{0.867mm}}{}
    148 \@ifclasswith{\@baseclass}{b6j}{\setlength{\hscale}{0.613mm}\setlength{\vscale}{0.613mm}}{}
    149 \@ifclasswith{\@baseclass}{ansiapaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
    150 \@ifclasswith{\@baseclass}{ansibpaper}{\setlength{\hscale}{1.328mm}\setlength{\vscale}{1.454mm}}{}
    151 \@ifclasswith{\@baseclass}{ansicpaper}{\setlength{\hscale}{2.057mm}\setlength{\vscale}{1.882mm}}{}
    152 \@ifclasswith{\@baseclass}{ansidpaper}{\setlength{\hscale}{2.662mm}\setlength{\vscale}{2.909mm}}{}
    153 \@ifclasswith{\@baseclass}{ansiepaper}{\setlength{\hscale}{4.114mm}\setlength{\vscale}{3.764mm}}{}
    154 \@ifclasswith{\@baseclass}{letterpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
    155 \@ifclasswith{\@baseclass}{executivepaper}{\setlength{\hscale}{0.876mm}\setlength{\vscale}{0.898mm}}{}
    156 \@ifclasswith{\@baseclass}{legalpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{1.198mm}}{}
    157 \@ifclasswith{\@baseclass}{smallpocketpaper}{\setlength{\hscale}{0.571mm}\setlength{\vscale}{0.639mm}}{}
    158 \@ifclasswith{\@baseclass}{pocketpaper}{\setlength{\hscale}{0.642mm}\setlength{\vscale}{0.723mm}}{}
    159 \@ifclasswith{\@baseclass}{juvenilepaper}{\setlength{\hscale}{0.738mm}\setlength{\vscale}{0.740mm}}{}
    160 \@ifclasswith{\@baseclass}{smallphotopaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.572mm}}{}
    161 \@ifclasswith{\@baseclass}{photopaper}{\setlength{\hscale}{1.00mm}\setlength{\vscale}{0.707mm}}{}
    162 \@ifclasswith{\@baseclass}{appendixpaper}{\setlength{\hscale}{1.000mm}\setlength{\vscale}{0.505mm}}{}
    163 \@ifclasswith{\@baseclass}{cookpaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.740mm}}{}
    164 \@ifclasswith{\@baseclass}{illustratedpaper}{\setlength{\hscale}{0.905mm}\setlength{\vscale}{0.909mm}}{}
    165 \@ifclasswith{\@baseclass}{f24paper}{\setlength{\hscale}{0.762mm}\setlength{\vscale}{0.808mm}}{}
    166 \@ifclasswith{\@baseclass}{a4paperlandscape}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{0.707mm}}{}
    167 \@ifclasswith{\@baseclass}{169paperlandscape}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{0.562mm}}{}
    168 
    169 % Set the default page layout
    170 \RequirePackage[
    171 	paperwidth=210\hscale,
    172 	paperheight=297\vscale,
    173 ]{geometry}
    174 
    175 % Command to choose among the three possible layouts
    176 \DeclareDocumentCommand{\pagelayout}{m}{%
    177 	\ifthenelse{\equal{margin}{#1}}{\marginlayout\marginfloatsetup}{}%
    178 	\ifthenelse{\equal{wide}{#1}}{\widelayout\widefloatsetup}{}%
    179 	\ifthenelse{\equal{fullwidth}{#1}}{\fullwidthlayout\widefloatsetup}{}%
    180 }
    181 
    182 \newif\ifwidelayout%
    183 \def\IfWideLayout{%
    184 	\ifwidelayout%
    185 		\expandafter\@firstoftwo%
    186 	\else%
    187 		\expandafter\@secondoftwo%
    188 	\fi%
    189 }
    190 
    191 % Layout #1: large margins
    192 \newcommand{\marginlayout}{%
    193 	\newgeometry{
    194 		top=27.4\vscale,
    195 		bottom=27.4\vscale,
    196 		inner=24.8\hscale,
    197 		textwidth=107\hscale,
    198 		marginparsep=6.2\hscale,
    199 		marginparwidth=47.7\hscale,
    200 	}%
    201 	\recalchead%
    202 	\widelayoutfalse%
    203 }
    204 
    205 % Layout #2: small, symmetric margins
    206 \newcommand{\widelayout}{%
    207 	\newgeometry{
    208 		top=27.4\vscale,
    209 		bottom=27.4\vscale,
    210 		inner=24.8\hscale,
    211 		outer=24.8\hscale,
    212 		marginparsep=0mm,
    213 		marginparwidth=0mm,
    214 	}%
    215 	\recalchead%
    216 	\widelayouttrue%
    217 }
    218 
    219 % Layout #3: no margins and no space above or below the body
    220 \newcommand{\fullwidthpage}{%
    221 	\newgeometry{
    222 		top=0mm,
    223 		bottom=0mm,
    224 		inner=0mm,
    225 		outer=0mm,
    226 		marginparwidth=0mm,
    227 		marginparsep=0mm,
    228 	}%
    229 	\recalchead%
    230 	\widelayouttrue%
    231 }
    232 
    233 % Set the default page layout
    234 \AtBeginDocument{\pagelayout{margin}}
    235 
    236 %----------------------------------------------------------------------------------------
    237 %	HEADERS AND FOOTERS
    238 %----------------------------------------------------------------------------------------
    239 
    240 \RequirePackage{scrlayer-scrpage}		% Customise head and foot regions
    241 
    242 % Set the header height to prevent a warning
    243 %\setlength{\headheight}{27.4\vscale}
    244 % Increase the space between header and text
    245 \setlength{\headsep}{11\vscale}
    246 
    247 % Define some LaTeX lengths used in the page headers
    248 \newlength{\headtextwidth} % This is the width of the text
    249 \newlength{\headmarginparsep} % This is the width of the whitespace between text and margin
    250 \newlength{\headmarginparwidth} % This is the width of the margin
    251 \newlength{\headtotal} % This is the total width of the header
    252 \newlength{\contentwidth} % An alias for \headtotal
    253 \newcommand{\recalchead}{% Command to recalculate the header-related length when needed
    254 	\setlength{\headtextwidth}{\textwidth}%
    255 	\setlength{\headmarginparsep}{\marginparsep}%
    256 	\setlength{\headmarginparwidth}{\marginparwidth}%
    257 	\setlength{\headtotal}{\headtextwidth+\headmarginparsep+\headmarginparwidth}%
    258 	\setlength{\contentwidth}{\headtotal}%
    259 }
    260 
    261 \AtBeginDocument{% Recalculate the header-related lengths
    262 	\recalchead%
    263 }
    264 
    265 % Header style with chapter number, chapter title, and page in the header (used throughout the document)
    266 \renewpagestyle{scrheadings}{%
    267 	{\smash{\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{%
    268 		\makebox[7\hscale][r]{\thepage}%
    269 		\makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
    270 		\makebox[\headtextwidth][l]{\leftmark}}}}%
    271 	{\smash{\makebox[0pt][l]{\makebox[\headtotal][r]{%
    272 		\makebox[\headtextwidth][r]{\hfill\rightmark}%
    273 		\makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
    274 		\makebox[7\hscale][l]{\thepage}}}}}%
    275 	{\smash{\makebox[0pt][l]{\makebox[\headtotal][r]{%
    276 		\makebox[\headtextwidth][r]{\hfill\rightmark}%
    277 		\makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
    278 		\makebox[7\hscale][l]{\thepage}}}}}%
    279 }{%
    280 	{}%
    281 	{}%
    282 	{}%
    283 }
    284 
    285 % Header style with neither header nor footer (used for special pages)
    286 \renewpagestyle{plain.scrheadings}{%
    287 	{}%
    288 	{}%
    289 	{}%
    290 }{%
    291 	{}%
    292 	{}%
    293 	{}%
    294 }
    295 
    296 % Header style with an empty header and the page number in the footer
    297 \newpagestyle{pagenum.scrheadings}{%
    298 	{}%
    299 	{}%
    300 	{}%
    301 }{%
    302 	{\makebox[\textwidth][r]{\thepage}}%
    303 	{\makebox[\textwidth][l]{\thepage}}%
    304 	{\makebox[\textwidth][l]{\thepage}}%
    305 }
    306 
    307 % Header style with an empty header and the page number in the footer
    308 \newpagestyle{centeredpagenum.scrheadings}{%
    309 	{}%
    310 	{}%
    311 	{}%
    312 }{%
    313 	{\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{\hfill\thepage\hfill}}
    314 	{\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
    315 	{\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
    316 }
    317 
    318 % Command to print a blank page
    319 \newcommand\blankpage{%
    320 	\null%
    321 	\thispagestyle{empty}%
    322 	\newpage%
    323 }
    324 
    325 % Set the default page style
    326 \pagestyle{plain.scrheadings}
    327 
    328 %----------------------------------------------------------------------------------------
    329 %	PARAGRAPH FORMATTING
    330 %----------------------------------------------------------------------------------------
    331 
    332 \RequirePackage{ragged2e} % Required to achieve better ragged paragraphs
    333 \RequirePackage{setspace} % Required to easily set the space between lines
    334 \RequirePackage{hyphenat} % Hyphenation for special fonts
    335 \RequirePackage{microtype} % Improves character and word spacing
    336 \RequirePackage{nowidow} % Prevent widowing
    337 \RequirePackage{needspace} % Required to prevent page break right after a sectioning command
    338 \RequirePackage{xspace} % Better print trailing whitespace
    339 
    340 % TODO: recognize space/indent justified/raggedright class options
    341 
    342 % Settings for a normal paragraph
    343 \newcommand{\@body@par}{%
    344 	\justifying% Justify text
    345 	\singlespacing% Set the interline space to a single line
    346 	\frenchspacing% No additional space after periods
    347 	\normalfont% Use the default font
    348 	\normalsize% Use the default size
    349 }
    350 
    351 % Settings for paragraphs in the margins
    352 \newcommand{\@margin@par}{%
    353 	\justifying% justify text
    354 	\setlength{\RaggedRightParindent}{0em}% Suppress indentation
    355 	\setlength{\parindent}{0em}% Suppress indentation
    356 	\setlength{\parskip}{0.5pc}% Set the space between paragraphs
    357 	%\singlespacing% Set the space between lines
    358 	\frenchspacing% No additional space after periods
    359 	\normalfont% Use the default font
    360 	\footnotesize% Use a smaller size
    361 }
    362 
    363 % By default, use @body@par settings
    364 \@body@par
    365 
    366 %----------------------------------------------------------------------------------------
    367 %	WIDE PARAGRAPHS
    368 %----------------------------------------------------------------------------------------
    369 
    370 % Environment for a wide paragraph
    371 \NewDocumentEnvironment{widepar}{}{%
    372 	\if@twoside%
    373 	\Ifthispageodd{%
    374 		\begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
    375 	}{%
    376 		\begin{addmargin}[-\marginparwidth-\marginparsep]{0cm}%
    377 	}%
    378 	\else%
    379 	\begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
    380 	\fi%
    381 }{%
    382 	\end{addmargin}%
    383 }
    384 
    385 % Environment for a full width paragraph
    386 \NewDocumentEnvironment{fullwidthpar}{}{%
    387 	\if@twoside%
    388 	\Ifthispageodd{%
    389 		\begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
    390 	}{%
    391 		\begin{addmargin}[-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth]{-\paperwidth+1in+\hoffset+\oddsidemargin+\marginparsep+\marginparwidth+\textwidth}%
    392 	}%
    393 	\else%
    394 	\begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
    395 	\fi% 
    396 }{%
    397 	\end{addmargin}%
    398 }
    399 
    400 % Environment for a wide equation
    401 \NewDocumentEnvironment{wideequation}{}{%
    402 	\begin{widepar}%
    403 	\begin{equation}%
    404 }{%
    405 	\end{equation}%
    406 	\end{widepar}%
    407 }
    408 
    409 %----------------------------------------------------------------------------------------
    410 %	FOOTNOTES, MARGINNOTES AND SIDENOTES
    411 %----------------------------------------------------------------------------------------
    412 
    413 \RequirePackage[section]{placeins} % Prevent floats to cross sections
    414 \extrafloats{100} % Require more floats
    415 
    416 \RequirePackage{marginnote} % Provides options for margin notes
    417 \RequirePackage{marginfix} % Make marginpars float freely; it should not be loaded as it prevents the marginnotes to stay aligned with the main text
    418 \RequirePackage{sidenotes} % Use sidenotes
    419 \RequirePackage{chngcntr} % Reset counters at sections
    420 
    421 % TODO: see also page 440 of the KOMA-script guide
    422 \RequirePackage[
    423 	bottom,
    424 	symbol*,
    425 	hang,
    426 	flushmargin,
    427 	% perpage,
    428 	stable,
    429 ]{footmisc} % Required to set up the footnotes
    430 \RequirePackage{footnotebackref} % Creates back references from footnotes to text
    431 
    432 % Fix the color of the footnote marker when back-referenced
    433 \patchcmd{\footref}{\ref}{\hypersetup{colorlinks=black}\ref}{}{}%
    434 % Workaround to fix back references
    435 \edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
    436 
    437 % FIXME: I am not able to choose the paragraph layout of footnotes, probably the footnotes package conflicts with scrbook.
    438 %\renewcommand{\footnotelayout}{\@margin@par}
    439 
    440 %----------------------------------------------------------------------------------------
    441 
    442 % Justify and format margin notes
    443 \renewcommand*{\raggedleftmarginnote}{} % Suppress left margin
    444 \renewcommand*{\raggedrightmarginnote}{} % Suppress right margin
    445 \renewcommand*{\marginfont}{\@margin@par} % Format marginnotes according to \@marginpar (see above)
    446 \renewcommand{\marginnotevadjust}{0.8\vscale} % Bring all marginnotes downwards a bit
    447 %\marginposadjustment=1\vscale % Bring downwards also the marginpars
    448 %\marginheightadjustment=10cm % Adjust the height of the margins for each page
    449 %\RequirePackage[marginnote=true]{snotez} % Provides options for sidenotes
    450 
    451 % Copied from snotez's \sidenote
    452 \def\kao@if@nblskip#1{%
    453 	\expandafter\ifx\@car#1\@nil*%
    454 		\expandafter\@firstoftwo%
    455 	\else%
    456 		\expandafter\@secondoftwo%
    457 	\fi%
    458 }
    459 
    460 % Command to detect whether we are inside a tcolorbox environment
    461 \newif\ifinfloat % First, set this flag whenever we are in a tcolorbox environment...
    462 \AtBeginEnvironment{tcolorbox}{\infloattrue\renewcommand{\marginnotevadjust}{-1.4\vscale}}
    463 \AtBeginEnvironment{minipage}{\infloattrue}
    464 
    465 \def\IfInFloatingEnvir{% ...Then, detect the flag
    466 	\ifinfloat%
    467 		\expandafter\@firstoftwo%
    468 	\else%
    469 		\expandafter\@secondoftwo%
    470 	\fi%
    471 }
    472 
    473 
    474 % Redefine the command to print marginnotes:
    475 % (a) the optional offset argument goes at the first position
    476 % (b) the offset can also be a multiple of baselineskip, like for snotez's \sidenote
    477 % Usage: \marginnote[<offset>]{Text of the note.}
    478 \let\oldmarginnote\marginnote%
    479 \RenewDocumentCommand\marginnote{ o m }{%
    480 	\IfNoValueOrEmptyTF{#1}{%
    481 		\IfInFloatingEnvir{%
    482 			\oldmarginnote{#2}%
    483 		}{%
    484 			\marginpar{\@margin@par#2}%
    485 		}%
    486 	}{%
    487 		\oldmarginnote{#2}[\kao@if@nblskip{#1}{\@cdr#1\@nil\baselineskip}{#1}]%
    488 	}%
    489 }
    490 
    491 % Initially set the sidenote counter to zero instead of 1, and update it before printing the sidenote.
    492 \setcounter{sidenote}{0}%
    493 \RenewDocumentCommand\sidenote{ o o +m }{%
    494 	\IfNoValueOrEmptyTF{#1}{%
    495 		\refstepcounter{sidenote}% This command has been moved here
    496 	}{%
    497 	}%
    498 	\sidenotemark[#1]%
    499 	\sidenotetext[#1][#2]{#3}%
    500 	\@sidenotes@multimarker%
    501 }
    502 
    503 % Formatting sidenote markers
    504 \RenewDocumentCommand\@sidenotes@thesidenotemark{ m }{% Format the marker
    505 	\leavevmode%
    506 	\ifhmode%
    507 		\edef\@x@sf{\the\spacefactor}%
    508 		\nobreak%
    509 	\fi%
    510 	\hbox{\@textsuperscript{\normalfont#1}}%
    511 	\ifhmode%
    512 		\spacefactor\@x@sf%
    513 	\fi%
    514 	\relax%
    515 }%
    516 
    517 % Formatting the sidenote text
    518 \RenewDocumentCommand\sidenotetext{ o o +m }{% number, offset, text
    519 	\IfNoValueOrEmptyTF{#1}{%
    520 		\marginnote[#2]{\thesidenote:\enskip#3}%
    521 	}{%
    522 		\marginnote[#2]{#1:\enskip#3}%
    523 	}%
    524 }
    525 
    526 %----------------------------------------------------------------------------------------
    527 %	FIGURES, TABLES, LISTINGS AND CAPTIONS
    528 %----------------------------------------------------------------------------------------
    529 
    530 \RequirePackage{graphicx} % Include figures
    531 \setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio} % Improves figure scaling
    532 \RequirePackage{tikz} % Allows to draw custom shapes
    533 \RequirePackage{tikzpagenodes} % Allows to anchor tikz nodes to page elements
    534 \RequirePackage{booktabs} % Nicer tables
    535 \RequirePackage{multirow} % Cells occupying multiple rows in tables
    536 \RequirePackage{multicol} % Multiple columns in dictionary
    537 \RequirePackage{rotating} % Allows tables and figures to be rotated
    538 \RequirePackage{listings} % Print code listings
    539 %\RequirePackage{minted}
    540 \RequirePackage[hypcap=true]{caption} % Correctly placed anchors for hyperlinks
    541 % \RequirePackage{atbegshi}
    542 % \RequirePackage{perpage}
    543 \let\c@abspage\relax
    544 % \newcommand{\pp@g@sidenote}{}
    545 \RequirePackage{floatrow} % Set up captions of floats
    546 %\RequirePackage{dblfloatfix} % Better positioning of wide figures and tables
    547 
    548 % Improve the figure placing (see https://www.overleaf.com/learn/latex/Tips)
    549 \def\topfraction{.9}%
    550 \def\textfraction{0.35}%
    551 \def\floatpagefraction{0.8}%
    552 
    553 % Set the space between floats and main text
    554 \renewcommand\FBaskip{.4\topskip}%
    555 \renewcommand\FBbskip{\FBaskip}%
    556 
    557 % Tighten up space between displays (e.g., equations) and make symmetric (from tufte-latex)
    558 \setlength\abovedisplayskip{6pt plus 2pt minus 4pt}%
    559 \setlength\belowdisplayskip{6pt plus 2pt minus 4pt}%
    560 \abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@%
    561 \abovedisplayshortskip \z@ \@plus3\p@%
    562 \belowdisplayskip \abovedisplayskip%
    563 \belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@%
    564 
    565 \setlength\columnseprule{.4pt} % Set the width of vertical rules in tables
    566 
    567 % % The marginfix package prevents the margin notes to stay aligned with the main text, so it cannot be used. However, we define the \marginskip command, which is the only command we need from that package.
    568 % \newcommand\marginskip[1]{%
    569 % 	\marginpar{\@margin@par\vspace{#1 - \baselineskip}}% We subtract the \baselineskip that we have in margin pars.
    570 % }
    571 
    572 \newlength{\kaomarginskipabove} % Specify the space above a marginfigure, margintable or marginlisting
    573 \newlength{\kaomarginskipbelow} % Specify the space below a marginfigure, margintable or marginlisting
    574 \setlength{\kaomarginskipabove}{3mm plus 2pt minus 2pt}
    575 \setlength{\kaomarginskipbelow}{3mm plus 2pt minus 2pt}
    576 
    577 % Environment to hold a margin figure (from the sidenotes package)
    578 % We redefine it here because we want to use our own caption formatting.
    579 \RenewDocumentEnvironment{marginfigure}{o}{%
    580 	\FloatBarrier%
    581 	\marginskip{\kaomarginskipabove}%
    582 	\begin{lrbox}{\@sidenotes@marginfigurebox}%
    583 	\begin{minipage}{\marginparwidth}%
    584 		\captionsetup{type=figure}%
    585 }{%
    586 	\end{minipage}%
    587 	\end{lrbox}%
    588 	\marginnote[#1]{\usebox{\@sidenotes@marginfigurebox}}%
    589 	\marginskip{\kaomarginskipbelow}%
    590 }
    591 
    592 % Environment to hold a margin table (from the sidenotes package)
    593 \RenewDocumentEnvironment{margintable}{o}{%
    594 	\FloatBarrier%
    595 	\marginskip{\kaomarginskipabove}%
    596 	\begin{lrbox}{\@sidenotes@margintablebox}%
    597 	\begin{minipage}{\marginparwidth}%
    598 		\captionsetup{type=table}%
    599 }{%
    600 	\end{minipage}%
    601 	\end{lrbox}%
    602 	\marginnote[#1]{\usebox{\@sidenotes@margintablebox}}%
    603 	\marginskip{\kaomarginskipbelow}%
    604 }
    605 
    606 % Environment to hold a margin listing
    607 \newsavebox{\@sidenotes@marginlistingbox}%
    608 \NewDocumentEnvironment{marginlisting}{o}{% The optional parameter is the vertical offset
    609 	\FloatBarrier%
    610 	\marginskip{\kaomarginskipabove}%
    611 	\begin{lrbox}{\@sidenotes@marginlistingbox}%
    612 	\begin{minipage}{\marginparwidth}%
    613 		\captionsetup{type=lstlisting}%
    614 }{%
    615 	\end{minipage}%
    616 	\end{lrbox}%
    617 	\marginnote[#1]{\usebox{\@sidenotes@marginlistingbox}}%
    618 	\marginskip{\kaomarginskipbelow}%
    619 }
    620 
    621 % Change the position of the captions
    622 \DeclareFloatSeparators{marginparsep}{\hskip\marginparsep}%
    623 
    624 % Detect whether there is a caption in the current environment by switching the kaocaption toggle when \caption is called. If there is no caption, reset the floatsetup. Without this fix, the floatrow package will align the environment to the main text if there is a caption, but to the margin if there is no caption.
    625 \newtoggle{kaocaption}
    626 \AtBeginEnvironment{figure}{%
    627 	\let\oldcaption\caption%
    628 	\RenewDocumentCommand{\caption}{s o m}{%
    629 		\IfBooleanTF{#1}{%
    630 			\oldcaption*{#3}%
    631 		}{%
    632 			\IfValueTF{#2}{%
    633 				\oldcaption[#2]{#3}%
    634 			}{%
    635 				\oldcaption{#3}%
    636 			}%
    637 		}%
    638 		\toggletrue{kaocaption}%
    639 	}%
    640 }
    641 \AtEndEnvironment{figure}{%
    642 	\iftoggle{kaocaption}{%
    643 	}{%
    644 		\RawFloats%
    645 		\centering%
    646 	}%
    647 	\togglefalse{kaocaption}%
    648 }
    649 \AtBeginEnvironment{table}{%
    650 	\let\oldcaption\caption%
    651 	\RenewDocumentCommand{\caption}{s o m}{%
    652 		\IfBooleanTF{#1}{%
    653 			\oldcaption*{#3}%
    654 		}{%
    655 			\IfValueTF{#2}{%
    656 				\oldcaption[#2]{#3}%
    657 			}{%
    658 				\oldcaption{#3}%
    659 			}%
    660 		}%
    661 		\toggletrue{kaocaption}%
    662 	}%
    663 }
    664 \AtEndEnvironment{table}{%
    665 	\iftoggle{kaocaption}{%
    666 	}{%
    667 		\RawFloats%
    668 		\centering%
    669 	}%
    670 	\togglefalse{kaocaption}%
    671 }
    672 
    673 % Change the formatting of the captions
    674 \addtokomafont{captionlabel}{\bfseries} % Bold font for the figure label
    675 % Declare a new style to format the caption according to \@margin@par (see above)
    676 \DeclareCaptionFormat{margin}{\@margin@par #1#2#3}
    677 % Declare a new caption style for lstlistings
    678 \newsavebox\mycap
    679 \DeclareCaptionFormat{llap}{%
    680 	\begin{lrbox}{\mycap}%
    681 	\begin{minipage}{\marginparwidth}%
    682 	\@margin@par #1:#2#3%
    683 	\end{minipage}%
    684 	\end{lrbox}%
    685 	\marginnote[0.2cm]{\usebox\mycap}%
    686 }
    687 % Set the global caption style
    688 \captionsetup{
    689 	format=margin, % Use the style previously declared
    690 	strut=no,%
    691 	%hypcap=true, % Links point to the top of the figure
    692 	singlelinecheck=false,%
    693 	%width=\marginparwidth,
    694 	indention=0pt, % Suppress indentation
    695 	parindent=0pt, % Suppress space between paragraphs
    696 	aboveskip=6pt, % Increase the space between the figure and the caption
    697 	belowskip=6pt, % Increase the space between the caption and the table
    698 }
    699 
    700 % Needed to have continued figures and tables (https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures_in_multiple_parts)
    701 \DeclareCaptionLabelFormat{cont}{#1~#2\alph{ContinuedFloat}}
    702 \captionsetup[ContinuedFloat]{labelformat=cont}
    703 
    704 % Captions for the 'margin' layout
    705 \NewDocumentCommand{\marginfloatsetup}{}{%
    706 \if@twoside%
    707 	\floatsetup[figure]{% Captions for figures
    708 		margins=hangoutside,% Put captions in the margins
    709 		facing=yes,%
    710 		capposition=beside,%
    711 		capbesideposition={bottom,outside},%
    712 		capbesideframe=yes,%
    713 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    714 		capbesidesep=marginparsep,%
    715 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    716 	}%
    717 	\floatsetup[widefigure]{% Captions for wide figures
    718 		margins=hangoutside,% Put captions below the figure
    719 		facing=yes,%
    720 		capposition=bottom%
    721 	}%
    722 	\floatsetup[table]{% Captions for tables
    723 		margins=hangoutside,% Put captions in the margin
    724 		facing=yes,%
    725 		capposition=beside,%
    726 		capbesideposition={top,outside},%
    727 		%capbesideposition=outside,
    728 		capbesideframe=yes,%
    729 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    730 		capbesidesep=marginparsep,%
    731 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    732 	}%
    733 	\floatsetup[widetable]{% Captions for wide tables
    734 		margins=hangoutside,% Put captions above the table
    735 		facing=yes,%
    736 		capposition=above%
    737 	}%
    738 	\floatsetup[longtable]{% Captions for longtables
    739 		margins=raggedright,% Overwrite the hangright setting from the `table' environment
    740 		%LTcapwidth=table,% Set the width of the caption equal to the table's
    741 	}%
    742 	\floatsetup[lstlisting]{% Captions for lstlistings
    743 		margins=hangoutside,% Put captions in the margin
    744 		facing=yes,%
    745 		capposition=beside,%
    746 		capbesideposition={top,outside},%
    747 		%capbesideposition=outside,
    748 		capbesideframe=yes,%
    749 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    750 		capbesidesep=marginparsep,%
    751 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    752 	}%
    753 	\floatsetup[listing]{% Captions for listings (minted package)
    754 		margins=hangoutside,% Put captions in the margin
    755 		facing=yes,%
    756 		capposition=beside,%
    757 		capbesideposition={top,outside},%
    758 		%capbesideposition=outside,
    759 		capbesideframe=yes,%
    760 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    761 		capbesidesep=marginparsep,%
    762 		floatwidth=\textwidth,%Width of the figure equal to the width of the text
    763 	}%
    764 	\captionsetup*[lstlisting]{%
    765 		format=llap,%
    766 		labelsep=space,%
    767 		singlelinecheck=no,%
    768 		belowskip=-0.6cm,%
    769 	}%
    770 \else%
    771 	\floatsetup[figure]{% Captions for figures
    772 		margins=hangright,% Put captions in the margins
    773 		facing=yes,%
    774 		capposition=beside,%
    775 		capbesideposition={bottom,right},%
    776 		capbesideframe=yes,%
    777 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    778 		capbesidesep=marginparsep,%
    779 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    780 	}%
    781 	\floatsetup[widefigure]{% Captions for wide figures
    782 		margins=hangright,% Put captions below the figure
    783 		facing=no,%
    784 		capposition=bottom%
    785 	}%
    786 	\floatsetup[table]{% Captions for tables
    787 		margins=hangright,% Put captions in the margin
    788 		facing=yes,%
    789 		capposition=beside,%
    790 		capbesideposition={top,right},%
    791 		%capbesideposition=outside,
    792 		capbesideframe=yes,%
    793 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    794 		capbesidesep=marginparsep,%
    795 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    796 	}%
    797 	\floatsetup[widetable]{% Captions for wide tables
    798 		margins=hangright,% Put captions above the table
    799 		facing=no,%
    800 		capposition=above%
    801 	}%
    802 	\floatsetup[longtable]{% Captions for longtables
    803 		margins=raggedright,% Overwrite the hangright setting from the `table' environment
    804 		%LTcapwidth=table,% Set the width of the caption equal to the table's
    805 	}%
    806 	\floatsetup[lstlisting]{% Captions for lstlisting
    807 		margins=hangright,% Put captions in the margin
    808 		facing=yes,%
    809 		capposition=beside,%
    810 		capbesideposition={top,right},%
    811 		%capbesideposition=outside,
    812 		capbesideframe=yes,%
    813 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    814 		capbesidesep=marginparsep,%
    815 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    816 	}%
    817 	\floatsetup[listing]{% Captions for listing (minted package)
    818 		margins=hangright,% Put captions in the margin
    819 		facing=yes,%
    820 		capposition=beside,%
    821 		capbesideposition={top,right},%
    822 		%capbesideposition=outside,
    823 		capbesideframe=yes,%
    824 		capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
    825 		capbesidesep=marginparsep,%
    826 		floatwidth=\textwidth,% Width of the figure equal to the width of the text
    827 	}%
    828 	\captionsetup*[lstlisting]{%
    829 		format=llap,%
    830 		labelsep=space,%
    831 		singlelinecheck=no,%
    832 		belowskip=-0.6cm,%
    833 	}%
    834 \fi%
    835 }
    836 
    837 % Captions for the 'wide' layout
    838 \NewDocumentCommand{\widefloatsetup}{}{%
    839 	\floatsetup[figure]{ % Captions for figures
    840 		capposition=bottom,%
    841 		margins=centering,%
    842 		floatwidth=\textwidth%
    843 	}
    844 	\floatsetup[widefigure]{ % Captions for wide figures
    845 		margins=hangoutside, % Put captions below the figure
    846 		facing=yes,%
    847 		capposition=bottom%
    848 	}
    849 	\floatsetup[table]{ % Captions for tables
    850 		capposition=above,%
    851 		margins=centering,%
    852 		floatwidth=\textwidth%
    853 	}
    854 	\floatsetup[widetable]{ % Captions for wide tables
    855 		margins=hangoutside, % Put captions above the table
    856 		facing=yes,%
    857 		capposition=above%
    858 	}
    859 	\floatsetup[lstlisting]{ % Captions for lstlistings
    860 		capposition=above,%
    861 		margins=centering,%
    862 		floatwidth=\textwidth%
    863 	}
    864 	\floatsetup[listing]{ % Captions for listings (minted package)
    865 		capposition=above,%
    866 		margins=centering,%
    867 		floatwidth=\textwidth%
    868 	}
    869 	\captionsetup*[lstlisting]{% Captions style for lstlistings
    870 		%format=margin,%
    871 		labelsep=colon,%
    872 		strut=no,%
    873 		singlelinecheck=false,%
    874 		indention=0pt,%
    875 		parindent=0pt,%
    876 		aboveskip=6pt,%
    877 		belowskip=6pt,%
    878 		belowskip=-0.1cm%
    879 	}
    880 }
    881 
    882 \RequirePackage[ruled, vlined]{algorithm2e}
    883 
    884 %----------------------------------------------------------------------------------------
    885 %	TOC, LOF & LOT
    886 %----------------------------------------------------------------------------------------
    887 
    888 \RequirePackage{tocbasic} % Required to customise the TOC
    889 
    890 % Show an entry for the TOC in the TOC
    891 \setuptoc{toc}{totoc}
    892 
    893 % Choose the levels in table of contents
    894 \setcounter{tocdepth}{\subsectiontocdepth}
    895 
    896 % Customise the list of listings
    897 \renewcommand{\lstlistlistingname}{List of Listings}% Change the title
    898 \newcommand{\lstlistingtocdepth}{\sectiontocdepth}% Set the depth
    899 \newcommand{\listoflistings}{\lstlistoflistings}% Provide the alias \listoflistings
    900 \newcommand{\listoflstlistings}{\lstlistoflistings}% Provide the alias \listoflstlistings
    901 
    902 % Define the style for toc entries
    903 \@ifpackagelater{scrbase}{2019/10/11}{%
    904 	\DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes,pagenumberwidth=2.1em]{tocline}{figure,table,lstlisting}%
    905 	\DeclareTOCStyleEntries[beforeskip=-6pt,level:=figure,indent=0em,numwidth=2em,dynnumwidth=yes,pagenumberwidth=2.1em]{tocline}{algocf}%
    906 	\DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
    907 	\DeclareTOCStyleEntries[pagenumberwidth=2.5em]{tocline}{chapter,part}%
    908 	\DeclareTOCStyleEntries[pagenumberwidth=2.1em]{tocline}{subsubsection,subsection,section}%
    909 }{%
    910 	\DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes]{tocline}{figure,table,lstlisting}%
    911 	\DeclareTOCStyleEntries[beforeskip=-6pt,level:=figure,indent=0em,numwidth=2em,dynnumwidth=yes,pagenumberwidth=2.1em]{tocline}{algocf}%
    912 	\DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
    913 }
    914 
    915 % Define the names for the headings
    916 % \renewcaptionname{english}{\contentsname}{Detailed Contents}
    917 % \renewcaptionname{english}{\listfigurename}{Figures}
    918 % \renewcaptionname{english}{\listtablename}{Tables}
    919 % \newcaptionname{english}{\listtheoremname}{Theorems}
    920 
    921 %----------------------------------------------------------------------------------------
    922 %	MARGIN TOC
    923 %----------------------------------------------------------------------------------------
    924 
    925 \RequirePackage{etoc} % Required to insert local tables of contents
    926 
    927 \newcounter{margintocdepth} % Set the depth of the margintoc
    928 \setcounter{margintocdepth}{\subsectiontocdepth} % By default to subsections
    929 
    930 \newlength{\mtocshift} % Length of the vertical offset used for margin tocs
    931 \setlength{\mtocshift}{1\vscale}% \mtocshift is overridden by \setchapterstyle
    932 
    933 % Optional title for margintoc (by hCarlFelix, 
    934 % https://github.com/fmarotta/kaobook/issues/101)
    935 % We want to create an additional entries in the toc which is to be used for the margintoc
    936 % We define these as mtocsection and mtocsubsection for section and subsection`
    937 \newcommand{\mtocsection}[1]{
    938 	\etoctoccontentsline{mtocsection}{%
    939 		\ifnum\value{secnumdepth}>0 \protect\numberline{\thesection}%
    940 		\else \protect\nonumberline%
    941 		\fi #1}%
    942 }
    943 \newcommand{\mtocsubsection}[1]{
    944 	\etoctoccontentsline{mtocsubsection}{%
    945 		\ifnum\value{secnumdepth}>1 \protect\numberline{\thesubsection}%
    946 		\else \protect\nonumberline%
    947 		\fi #1}%
    948 }
    949 
    950 % Next, we redefine \section and \subsection so that they accept an additional parameter
    951 % \section[alternative-title-for-toc]{title-as-written-in-text}[alternative-title-for-margintoc]
    952 
    953 % Adapted from Frank Mittelbach's answer at Stackexchange
    954 % https://tex.stackexchange.com/a/109560/226693
    955 \let\oldsection\section	% save the old command
    956 \let\oldsubsection\subsection	% save the old command
    957 
    958 \RenewDocumentCommand\section{s o m o}{%
    959 	\IfBooleanTF{#1}{%
    960 		\oldsection*{#3}
    961 		\IfNoValueF{#2}{% if TOC arg is given create a TOC entry
    962 			\addxcontentsline{toc}{section}[\thesection]{#2}%
    963 		}%
    964 	}{% no star given
    965 		\IfNoValueTF{#2}{%
    966 			\oldsection{#3}%
    967 		}{% no TOC arg
    968 			\oldsection[#2]{#3}%
    969 		}%
    970 		\IfNoValueTF{#4}{% optional label given, if not we do nothing
    971 			% \mtocsection{#3}%
    972 			\IfNoValueTF{#2}{%
    973 				\mtocsection{#3}% when no optional toc title, use main title
    974 			}{% no TOC arg
    975 				\mtocsection{#2}% when toc optional title is given, use it
    976 			}%
    977 		}{%
    978 			\mtocsection{#4}%
    979 		}%
    980 	}%
    981 }
    982 
    983 \RenewDocumentCommand\subsection{s o m o}{%
    984 	\IfBooleanTF{#1}{%
    985 		\oldsubsection*{#3}%
    986 		\IfNoValueF{#2}{% if TOC arg is given create a TOC entry
    987 			\addxcontentsline{toc}{subsection}[\thesubsection]{#2}%
    988 		}%
    989 	}{% no star given
    990 		\IfNoValueTF{#2}{%
    991 			\oldsubsection{#3}%
    992 		}{% no TOC arg
    993 			\oldsubsection[#2]{#3}%
    994 		}%
    995 		\IfNoValueTF{#4}{% optional label given, if not we do nothing
    996 			\IfNoValueTF{#2}{%
    997 				\mtocsubsection{#3}% when no optional toc title, use main title
    998 			}{% no TOC arg
    999 				\mtocsubsection{#2}% when toc optional title is given, use it
   1000 			}%
   1001 		}{%
   1002 			\mtocsubsection{#4}%
   1003 		}%
   1004 	}%
   1005 }
   1006 
   1007 \etocsetlevel{mtocsection}{6}% dummy sectioning level
   1008 \etocsetlevel{mtocsubsection}{6}% dummy sectioning level
   1009 
   1010 % Command to print a table of contents in the margin
   1011 \NewDocumentCommand{\margintoc}{O{\mtocshift}}{ % The first parameter is the vertical offset; by default it is \mtocshift
   1012 	\begingroup%
   1013 		% Move regular section and subsection to level 6 so that they won't be included and instead set let the mtoc versions take their place.
   1014 		% Adapted from https://tex.stackexchange.com/a/133559/226693
   1015 		\etocsetlevel{section}{6}
   1016 		\etocsetlevel{subsection}{6}
   1017 		\etocsetlevel{mtocsection}{1}
   1018 		\etocsetlevel{mtocsubsection}{2}
   1019 
   1020 		% Define default widths
   1021 		\def\margintocnumwidth{-.8mm}%
   1022 		\def\margintocpagenumwidth{8pt}%
   1023 		\setlength{\RaggedRightParfillskip}{0pt}
   1024 
   1025 		% Dry run to get the needed widths
   1026 		\etocsetstyle{mtocsection}%
   1027 			{}%
   1028 			{\setbox0\hbox{{\usekomafont{section}\small\etocthenumber\kern.8mm}}%%
   1029 			 \setbox1\hbox{{\usekomafont{section}\small\etocthepage}}}%
   1030 			{\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%%
   1031 			 \ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
   1032 			{}%
   1033 		\etocsetstyle{mtocsubsection}%
   1034 			{}%
   1035 			{\setbox0\hbox{{\usekomafont{section}\small\mdseries\etocthenumber\kern.8mm}}%
   1036 			 \setbox1\hbox{{\usekomafont{section}\small\mdseries\etocthepage}}}%
   1037 			{\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%
   1038 			 \ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
   1039 			{}%
   1040 		\etocsetstyle{subsubsection}%
   1041 			{}%
   1042 			{}%
   1043 			{}%
   1044 			{}%
   1045 		\etocsetstyle{paragraph}%
   1046 			{}%
   1047 			{}%
   1048 			{}%
   1049 			{}%
   1050 		\etocsettocstyle{}{%
   1051 			\global\let\margintocnumwidth\margintocnumwidth%
   1052 			\global\let\margintocpagenumwidth\margintocpagenumwidth%
   1053 		}%
   1054 		\localtableofcontents%
   1055 
   1056 		% Set the style for section entries
   1057 		\etocsetstyle{mtocsection}%
   1058 		{\parindent 0pt \parskip 2.5pt \parfillskip 0pt \RaggedRight}%
   1059 		{\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth}%
   1060 		{\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\etocnumber}}\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\etocpage}}\par}%
   1061 		{}%
   1062 		% Set the style for subsection entries
   1063 		\etocsetstyle{mtocsubsection}%
   1064 		{\parindent 0pt \parskip 0pt \parfillskip 0pt \RaggedRight}%
   1065 		{\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth}%
   1066 		{\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\mdseries\etocnumber}}{\mdseries\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\mdseries\etocpage}}}\par}%
   1067 		{\parskip 2.5pt}%
   1068 		% Set the global style of the toc
   1069 		\etocsettocstyle{\usekomafont{section}\small}{}%
   1070 		\etocsetnexttocdepth{\themargintocdepth}%
   1071 		% Print the table of contents in the margin
   1072 		\marginnote[#1]{\localtableofcontents}%
   1073 		\FloatBarrier%
   1074 	\endgroup%
   1075 }
   1076 
   1077 %----------------------------------------------------------------------------------------
   1078 %	ENCODING AND FONTS
   1079 %----------------------------------------------------------------------------------------
   1080 
   1081 \newcommand{\kao@fontmethodModern}{%
   1082 	\RequirePackage{amssymb} % Must be loaded before unicode-math
   1083 	\RequirePackage[force]{filehook} % Fixes an error
   1084 	\RequirePackage{unicode-math} % Math fonts in xetexorluatex
   1085 	\setromanfont[ % Libertinus Serif font
   1086 		Scale=1.04
   1087 	]{Libertinus Serif}
   1088 	\setsansfont[ % Libertinus Sans font
   1089 		Scale=1
   1090 	]{Libertinus Sans}
   1091 	\setmonofont[ % Libertinus Mono font
   1092 		Scale=.89
   1093 	]{Liberation Mono}
   1094 	\setmathfont{Libertinus Math} % Libertinus Math font
   1095 	\ifluatex
   1096 	\else
   1097 		\RequirePackage{morewrites} % Fix some errors related to floats (not necessary with LuaLaTeX
   1098 	\fi%
   1099 }
   1100 \newcommand{\kao@fontmethodTex}{%
   1101 	\RequirePackage[utf8]{inputenc} % utf8 encoding in the input (.tex) file
   1102 	\RequirePackage[T1]{fontenc} % utf8 encoding in the output (.pdf) file
   1103 
   1104 	\RequirePackage{amssymb} % Math symbols, including \blacktriangleright, used for bullets
   1105 	\RequirePackage[scaled=.97,helvratio=.93,p,theoremfont]{newpxtext} % Serif palatino font
   1106 	\RequirePackage[vvarbb,smallerops,bigdelims]{newpxmath} % Math palatino font
   1107 	\RequirePackage[scaled=.85]{beramono} % Monospace font
   1108 	% \RequirePackage[scr=rsfso,cal=boondoxo]{mathalfa} % Mathcal from STIX, unslanted a bit
   1109 	\RequirePackage{morewrites} % Fix some errors related to floats
   1110 }
   1111 
   1112 \ifthenelse%
   1113 	{\equal{modern}{\kao@fontmethod}}{\kao@fontmethodModern}%
   1114 {\ifthenelse%
   1115 	{\equal{tex}{\kao@fontmethod}}{\kao@fontmethodTex}%
   1116 {
   1117 	\PackageError{kao}{%
   1118 		Invalid fontmethod choice \kao@fontmethod.%
   1119 	}{%
   1120 		Should be one of "modern" or "tex"%
   1121 	}
   1122 }}
   1123 
   1124 % When using the Palatino (newpxtext) font, it is better to use a 
   1125 % slightly larger stretch.
   1126 %\setstretch{1.10}
   1127 \linespread{1.07} % Give Palatino more leading (space between lines)
   1128 
   1129 % Fix for the subequation environment (https://tex.stackexchange.com/questions/27053/too-much-space-between-full-paragraph-and-subequations-env)
   1130 \preto\subequations{\ifhmode\unskip\fi}
   1131 
   1132 %----------------------------------------------------------------------------------------
   1133 %	HYPERREFERENCES
   1134 %----------------------------------------------------------------------------------------
   1135 
   1136 \RequirePackage{hyperref} % Required for hyperlinks
   1137 \RequirePackage{bookmark} % Required for pdf bookmarks
   1138 
   1139 \PassOptionsToPackage{hyphens}{url} % Break long URLs and use hyphens to separate the pieces
   1140 
   1141 % Color settings should be done in the next \hypersetup
   1142 \hypersetup{ % Set up hyperref options
   1143 	unicode, % Use unicode for links
   1144 	pdfborder={0 0 0}, % Suppress border around pdf
   1145 	%xetex,
   1146 	%pagebackref=true,
   1147 	%hyperfootnotes=false, % We already use footmisc
   1148 	bookmarksdepth=section,
   1149 	bookmarksopen=true, % Expand the bookmarks as soon as the pdf file is opened
   1150 	%bookmarksopenlevel=4,
   1151 	linktoc=all, % Toc entries and numbers links to pages
   1152 	breaklinks=true,
   1153 	colorlinks=true,
   1154 }
   1155 
   1156 % Define a new color for the footnote marks
   1157 \def\@footnotecolor{black}
   1158 \define@key{Hyp}{footnotecolor}{%
   1159 	\HyColor@HyperrefColor{#1}\@footnotecolor%
   1160 }
   1161 \def\@footnotemark{%
   1162 	\leavevmode
   1163 	\ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
   1164 	\stepcounter{Hfootnote}%
   1165 	\global\let\Hy@saved@currentHref\@currentHref
   1166 	\hyper@makecurrent{Hfootnote}%
   1167 	\global\let\Hy@footnote@currentHref\@currentHref
   1168 	\global\let\@currentHref\Hy@saved@currentHref
   1169 	\hyper@linkstart{footnote}{\Hy@footnote@currentHref}%
   1170 	\@makefnmark
   1171 	\hyper@linkend
   1172 	\ifhmode\spacefactor\@x@sf\fi
   1173 	\relax
   1174 }
   1175 
   1176 % Redefine the \thanks command to allow users to use \label within \thanks without getting warnings
   1177 \let\oldthanks\thanks
   1178 \renewcommand\thanks[1]{%
   1179 	\label{bhfn:0}%
   1180 	\oldthanks{#1}%
   1181 }
   1182 
   1183 % Adjust the colour of the footnotes marks using the colour defined above
   1184 \renewcommand\@makefntext[1]{%
   1185 	\renewcommand\@makefnmark{%
   1186 		\mbox{\textsuperscript{\normalfont%
   1187 			\hyperref[\BackrefFootnoteTag]{%
   1188 				\color{\@footnotecolor}{\@thefnmark}%
   1189 			}}\,%
   1190 		}%
   1191 	}%
   1192 	\BHFN@OldMakefntext{#1}%
   1193 }
   1194 
   1195 %----------------------------------------------------------------------------------------
   1196 %	COLOURS
   1197 %----------------------------------------------------------------------------------------
   1198 
   1199 % Uncomment to have coloured headings
   1200 %\addtokomafont{title}{\color{Maroon}}
   1201 %\addtokomafont{part}{\color{Maroon}}
   1202 %\addtokomafont{chapter}{\color{Maroon}}
   1203 %\addtokomafont{section}{\color{Maroon}}
   1204 %\addtokomafont{subsection}{\color{Maroon}}
   1205 %\addtokomafont{subsubsection}{\color{Maroon}}
   1206 %\addtokomafont{paragraph}{\color{Maroon}}
   1207 %\addtokomafont{captionlabel}{\color{Blue}}
   1208 %\addtokomafont{pagenumber}{\color{Maroon}}
   1209 
   1210 % Choose the default colors
   1211 \hypersetup{
   1212 	%allcolors=DarkGreen,
   1213 	%anchorcolor=Red,
   1214 	%citecolor=DarkOrange!70!black,
   1215 	citecolor=OliveGreen,
   1216 	filecolor=OliveGreen,
   1217 	%linkcolor=Blue,
   1218 	linkcolor=Black,
   1219 	%pagecolor = Blue,
   1220 	%menucolor=Red,
   1221 	%runcolor=Red,
   1222 	urlcolor=OliveGreen,
   1223 }
   1224 
   1225 %----------------------------------------------------------------------------------------
   1226 %	ITEMS
   1227 %----------------------------------------------------------------------------------------
   1228 
   1229 \renewcommand{\labelitemi}{\small$\blacktriangleright$} % Use a black triangle for the first level of \item's
   1230 \renewcommand{\labelitemii}{\textbullet} % Use a bullet for the second level of \item's
   1231 \RequirePackage[inline]{enumitem} % Used to customise lists (in particular, we don't want to put whitespace between items)
   1232 \setlist[itemize]{noitemsep}
   1233 \setlist[enumerate]{noitemsep}
   1234 \setlist[description]{noitemsep}
   1235 
   1236 %----------------------------------------------------------------------------------------
   1237 %	SIMPLE BOXED ENVIRONMENT
   1238 %----------------------------------------------------------------------------------------
   1239 
   1240 % kaobox
   1241 \RequirePackage[most]{tcolorbox}
   1242 
   1243 % Define a new environment using the style created above
   1244 \newtcolorbox{kaobox}[1][]{
   1245 	breakable,
   1246 	before skip=1.5\topskip,
   1247 	after skip=1.5\topskip,
   1248 	left skip=0pt,
   1249 	right skip=0pt,
   1250 	left=4pt,
   1251 	right=4pt,
   1252 	top=2pt,
   1253 	bottom=2pt,
   1254 	lefttitle=4pt,
   1255 	righttitle=4pt,
   1256 	toptitle=2pt,
   1257 	bottomtitle=2pt,
   1258 	sharp corners,
   1259 	boxrule=0pt,
   1260 	titlerule=.4pt,
   1261 	colback=RoyalBlue!25!White,
   1262 	colbacktitle=RoyalBlue!25!White,
   1263 	coltitle=black,
   1264 	colframe=black,
   1265 	fonttitle=\bfseries,
   1266 	#1
   1267 }
   1268 
   1269 %----------------------------------------------------------------------------------------
   1270 %	ENVIRONMENT WITH A COUNTER
   1271 %----------------------------------------------------------------------------------------
   1272 
   1273 % Define an environment titled 'Comment' and numbered incrementally
   1274 \newenvironment{kaocounter}{
   1275 	\refstepcounter{kaocounter}
   1276 	\begin{kaobox}[title=Comment~\thekaocounter\autodot]
   1277 }{
   1278 	\end{kaobox}
   1279 }
   1280 
   1281 % Define the commands to manage the counter for the 'kaocounter' environment
   1282 \newcounter{kaocounter}
   1283 \counterwithin{kaocounter}{section}
   1284 \newcommand*{\kaocounterformat}{% Format for the caption
   1285 	Comment~\thekaocounter\csname autodot\endcsname}
   1286 \newcommand*{\fnum@kaocounter}{\kaocounterformat}
   1287 
   1288 
   1289 %----------------------------------------------------------------------------------------
   1290 %	FLOATING ENVIRONMENT WITH TOC ENTRIES
   1291 %----------------------------------------------------------------------------------------
   1292 
   1293 % Define a floating environment
   1294 \newenvironment{kaofloating}{%
   1295 	\@float{kaofloating}%
   1296 }{%
   1297 	\end@float%
   1298 }
   1299 
   1300 % Configure the 'kaofloating' environment
   1301 \newcommand*{\fps@floatingbox}{tbph}% Allowed positions for the environment (top, bottom, own page, here)
   1302 \newcommand*{\ftype@floatingbox}{5}% Set the type of float (floats of the same type cannot change their order; figures and tables are type 1 and 2 respectively)
   1303 \newcommand*{\floatingboxformat}{% Set a title of the environment
   1304 	Insight~\thefloatingbox\csname autodot\endcsname}
   1305 \newcommand*{\fnum@floatingbox}{\floatingboxformat}% Use the environment title
   1306 \newcommand*{\ext@floatingbox}{loi}% Choose the extension of the auxiliary file for this environment
   1307 
   1308 \addtotoclist[float]{loi}% Keep track of 'kaofloating' environments for a "List of Insights"
   1309 \newcommand*{\listofloiname}{List of Insights}% Choose the title for the "List of Insights"
   1310 \newcommand*{\l@floatingbox}{\l@figure}% Format the LOI
   1311 \newcommand*{\listofinsights}{\listoftoc{loi}}% User-friendly command to print the LOI
   1312 
   1313 %----------------------------------------------------------------------------------------
   1314 %	ADDITIONAL PACKAGES
   1315 %----------------------------------------------------------------------------------------
   1316 
   1317 % Productivity
   1318 \RequirePackage{pdfpages} % Insert PDF pages
   1319 \RequirePackage{subfiles} % Adopt a modular structure
   1320 \RequirePackage{todonotes} % Add useful notes in the margins
   1321 
   1322 % Listings code
   1323 \RequirePackage{listings} % Code
   1324 %\RequirePackage{minted} (must be loaded before scrhack)
   1325 
   1326 % Configure the listings
   1327 \definecolor{listingkeywords}{rgb}{0.0, 0.5, 0.0}
   1328 \definecolor{listingidentifiers}{rgb}{0, 0, 0}
   1329 \definecolor{listingcomments}{rgb}{0.25, 0.5, 0.5}
   1330 \definecolor{listingstrings}{rgb}{0.73, 0.13, 0.13}
   1331 \definecolor{listingnumbers}{rgb}{0.25, 0.25, 0.25}
   1332 % Define a fancy style
   1333 \lstdefinestyle{kaolst}{
   1334 	aboveskip=0.7\topskip,
   1335 	belowskip=0.1\topskip,
   1336 	basicstyle=\small\ttfamily,
   1337 	commentstyle=\color{listingcomments}\itshape,
   1338 	keywordstyle=\color{listingkeywords}\bfseries,
   1339 	numberstyle=\scriptsize\color{listingnumbers}\ttfamily,
   1340 	stringstyle=\color{listingstrings},
   1341 	identifierstyle=\color{listingidentifiers},
   1342 	backgroundcolor=\color{White},
   1343 	breakatwhitespace=false,
   1344 	breaklines=true,
   1345 	captionpos=t,
   1346 	keepspaces=true,
   1347 	showspaces=false,
   1348 	showstringspaces=false,
   1349 	showtabs=false,
   1350 	numbers=left,
   1351 	numbersep=1em,
   1352 	%frame=lines,
   1353 	frame=l,
   1354 	framerule=.7pt,
   1355 	tabsize=4,
   1356 	defaultdialect=[LaTeX]Tex,
   1357 }
   1358 % Define a plain style as well
   1359 \lstdefinestyle{kaolstplain}{
   1360 	aboveskip=0.6\topskip,
   1361 	belowskip=-0.1\topskip,
   1362 	basicstyle=\small\ttfamily,
   1363 	commentstyle=\color{listingcomments}\itshape,
   1364 	keywordstyle=\color{listingkeywords}\bfseries,
   1365 	numberstyle=\scriptsize\color{listingnumbers}\ttfamily,
   1366 	stringstyle=\color{listingstrings},
   1367 	identifierstyle=\color{listingidentifiers},
   1368 	backgroundcolor=\color{White},
   1369 	breakatwhitespace=false,
   1370 	breaklines=true,
   1371 	captionpos=b,
   1372 	keepspaces=true,
   1373 	showspaces=false,
   1374 	showstringspaces=false,
   1375 	showtabs=false,
   1376 	numbers=none,
   1377 	frame=none,
   1378 	tabsize=4,
   1379 	defaultdialect=[LaTeX]Tex,
   1380 }
   1381 \lstset{style=kaolst}% Use the fancy style
   1382 
   1383 % Verbatim
   1384 %\RequirePackage{fancyvrb} % Customization of verbatim environments
   1385 %\fvset{fontsize=\normalsize} % Change here the font size of all 
   1386 %verbatim \preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
   1387 
   1388 % Algorithms
   1389 % \RequirePackage[ruled, vlined]{algorithm2e} % Algorithms
   1390 % \RequirePackage[linesnumbered, ruled, vlined]{algorithm2e} % Algorithms
   1391 
   1392 % Special gliphs
   1393 \RequirePackage{ccicons} % Creative Commons icons
   1394 
   1395 % Index, glossary and nomenclature
   1396 \RequirePackage{imakeidx}
   1397 \RequirePackage[xindy,toc,numberedsection=nameref]{glossaries}
   1398 \RequirePackage[intoc]{nomencl}
   1399 
   1400 % Commands to print specific words always in the same way
   1401 % TODO: in \Command, automatically escape braces {} and replace backslashes with \textbackslash
   1402 \newcommand{\Class}[1]{\texttt{#1}}
   1403 \newcommand{\Package}[1]{\texttt{#1}}
   1404 \newcommand{\Option}[1]{\texttt{#1}}
   1405 \newcommand{\Command}[1]{\texttt{\textbackslash#1}}
   1406 \newcommand{\Environment}[1]{\texttt{#1}}
   1407 \newcommand{\Path}[1]{\texttt{#1}}
   1408 
   1409 % Print latin words in italics (The xspace package is required but we already loaded it in the class)
   1410 \newcommand{\hairsp}{\hspace{1pt}} % Command to print a very short space
   1411 \newcommand{\invitro}{\textit{in vitro}\xspace}
   1412 \newcommand{\invivo}{\textit{in vivo}\xspace}
   1413 \newcommand{\cis}{\textit{cis}\xspace}
   1414 \newcommand{\trans}{\textit{trans}\xspace}
   1415 \newcommand{\etal}{\textit{et al.}\xspace}
   1416 \newcommand{\denovo}{\textit{de novo}\xspace}
   1417 \newcommand{\adhoc}{\textit{ad hoc}\xspace}
   1418 \newcommand{\etcetera}{\textit{et cetera}\xspace}
   1419 \newcommand{\etc}{\textit{etc.}\xspace}
   1420 \newcommand{\ie}{\textit{i.\nobreak\hairsp{}e.}\xspace}
   1421 \newcommand{\eg}{\textit{e.\nobreak\hairsp{}g.}\xspace}
   1422 \newcommand{\vs}{\textit{vs}\xspace}
   1423 \newcommand{\cfr}{\textit{cfr.}\xspace}
   1424 
   1425 % Tables
   1426 \newcommand{\na}{\quad--} % Used in tables for N/A cells
   1427 \newcommand{\hangp}[1]{\makebox[0pt][r]{(}#1\makebox[0pt][l]{)}} % Create parentheses around text in tables which take up no horizontal space - this improves column spacing
   1428 \newcommand{\hangstar}{\makebox[0pt][l]{*}} % Create asterisks in tables which take up no horizontal space - this improves column spacing
   1429 
   1430 % A command to print the current month and year (from tufte-latex)
   1431 \newcommand{\monthyear}{\ifcase\month\or January\or February\or March\or 
   1432 April\or May\or June\or July\or August\or September\or October\or 
   1433 November\or December\fi\space\number\year}