#+TITLE: Setup file
#+PROPERTY: header-args:latex :tangle config.tex

* Babel
#+begin_src latex
\usepackage[french,english]{babel}
#+end_src

* Figures
#+begin_src latex
\usepackage{graphicx}
\graphicspath{{figs/}}
#+end_src

* Captions
#+begin_src latex
\usepackage{subcaption}
\captionsetup{subrefformat=parens}
#+end_src

* Glossary
#+begin_src latex
\usepackage[      %
    acronym,      % Separate acronyms and glossary
    toc,          % appear in ToC
    automake,     % auto-use the makeglossaries command (requires shell-escape)
    nonumberlist, % don't back reference pages
    nogroupskip,  % don't group by letter
    nopostdot     % don't add a dot at the end of each element
]{glossaries}

\usepackage[stylemods=longextra]{glossaries-extra}

\setabbreviationstyle[acronym]{long-short}
\setglossarystyle{long-name-desc}
#+end_src

#+begin_src latex :tangle config_extra.tex
\makeindex
\makeglossaries
#+end_src

* Lists
Do not modify itemize/enumerate environments by default
#+begin_src latex
\usepackage[olditem,oldenum]{paralist}
#+end_src

* Spacing
#+begin_src latex
\RequirePackage{setspace}
\onehalfspacing
#+end_src

* Tables
#+begin_src latex
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{tabularx}
#+end_src

I reduce the size of tables so that longer tables can still fit into an A4 (reduce font and make sans serif).
#+begin_src latex
\usepackage{floatrow}
\floatsetup[table]{font={footnotesize,sf},capposition=top}
#+end_src

* Units
#+begin_src latex
\usepackage[binary-units=true]{siunitx}

\sisetup{%
  detect-all           = true,
  detect-family        = true,
  detect-mode          = true,
  detect-shape         = true,
  detect-weight        = true,
  detect-inline-weight = math,
}

\DeclareSIUnit\px{px}
\DeclareSIUnit\rms{rms}
#+end_src

* Mathematics
#+begin_src latex
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{dsfont}
#+end_src

Fix the spacing of =\left= and =\right=.
Use these with the proper bracket in order to ensure that they scale automatically.
#+begin_src latex
\let\originalleft\left
\let\originalright\right
\renewcommand{\left}{\mathopen{}\mathclose\bgroup\originalleft}
\renewcommand{\right}{\aftergroup\egroup\originalright}
#+end_src

* Geometry

# \usepackage[paperheight=24.41cm,paperwidth=17.21cm,bottom=3cm,left=1.4cm,right=2cm,heightrounded]{geometry}

* Penalties
#+begin_src latex
\clubpenalty         = 10000
\widowpenalty        = 10000
\displaywidowpenalty = 10000
#+end_src

* Headers
#+begin_src latex
\usepackage{scrlayer-scrpage}

\pagestyle{scrheadings}
#+end_src

* Section/Figure format
#+begin_src latex
\renewcommand{\partformat}{\huge\partname~\thepart\autodot}
\renewcommand{\raggedpart}{\flushleft}

\setkomafont{part}{\normalfont\huge\scshape}

\setkomafont{sectioning}{\normalfont\scshape}
\setkomafont{descriptionlabel}{\normalfont\bfseries}

\setkomafont{caption}{\small}
\setkomafont{captionlabel}{\usekomafont{caption}}

\setcounter{secnumdepth}{\subsubsectionnumdepth}
#+end_src

Improve chapter font colors and font size.
The following commands make chapter numbers BrickRed.
#+begin_src latex
\makeatletter
\renewcommand*{\chapterformat}{  \mbox{\chapappifchapterprefix{\nobreakspace}{\color{BrickRed}\fontsize{40}{45}\selectfont\thechapter}\autodot\enskip}}
% Section with color
% \renewcommand\@seccntformat[1]{\color{BrickRed} {\csname the#1\endcsname}\hspace{0.3em}}
\renewcommand\@seccntformat[1]{{\csname the#1\endcsname}\hspace{0.3em}}
\makeatother
#+end_src


* Floating images configuration
By default, if a figure consumes 60% of the page it will get its own float-page.
To change that we have to adjust the value of the =floatpagefraction= derivative.
#+begin_src latex
\renewcommand{\floatpagefraction}{.8}%
#+end_src

See more information [[https://tex.stackexchange.com/questions/68516/avoid-that-figure-gets-its-own-page][here]].

* Etoolbox package
#+begin_src latex
\usepackage{etoolbox}
#+end_src

* Hyperref and Bookmarks
#+begin_src latex
\usepackage[            %
    colorlinks=true,    %
    citecolor=BrickRed, %
    linkcolor=BrickRed, %
    urlcolor=BrickRed,  %
    unicode             %
    ]{hyperref}
#+end_src

The bookmark package implements a new bookmark (outline) organisation for package hyperref.
This lets us change the "tree-navigation" associated with the generated pdf and constrain the menu only to H:2.

#+begin_src latex
\usepackage{bookmark}

\bookmarksetup{depth=2}
#+end_src

* Bibliography

#+begin_src latex
\usepackage{xpatch} % Recommanded for biblatex
\usepackage[        % use biblatex for bibliography
    backend=biber,  % use biber backend (bibtex replacement) or bibtex
    style=ieee,     % bib style
    hyperref=true,  % activate hyperref support
    backref=true,   % activate backrefs
    isbn=false,     % don't show isbn tags
    url=false,      % don't show url tags
    doi=false,      % don't show doi tags
    urldate=long,   % display type for dates
    maxnames=3,     %
    minnames=1,     %
    maxbibnames=5,  %
    minbibnames=3,  %
    maxcitenames=2, %
    mincitenames=1  %
    ]{biblatex}
#+end_src

#+begin_src latex
\defbibenvironment{mypubs}
 {\list
     {}
     {\setlength{\leftmargin}{\bibhang}%
      \setlength{\itemindent}{-\leftmargin}%
      \setlength{\itemsep}{\bibitemsep}%
      \setlength{\parsep}{\bibparsep}}}
  {\endlist}
  {\item}
#+end_src

Remove ISSN, DOI and URL to shorten the bibliography.
#+begin_src latex
\AtEveryBibitem{%
  \clearfield{urlyear}
  \clearfield{urlmonth}
  \clearfield{note}
 \clearfield{issn} % Remove issn
 \clearfield{doi} % Remove doi
\ifentrytype{online}{}{% Remove url except for @online
  \clearfield{url}
}
}
#+end_src

And increase the spacing between the entries, as per default they are too small.
#+begin_src latex
\setlength\bibitemsep{1.1\itemsep}
#+end_src

Also reduce the font-size
#+begin_src latex
\renewcommand*{\bibfont}{\footnotesize}
#+end_src

* Table of Contents for each chapter
Note that this is marked as deprecated for koma-script.
#+begin_src latex
\usepackage{minitoc}
% Change section font
\renewcommand{\mtcSfont}{\small}
% Change indentation
\setlength{\mtcindent}{0pt}
% \usepackage[nottoc]{tocbibind}
#+end_src

* Fonts

#+begin_src latex
\ifxetexorluatex
  \usepackage{unicode-math}
  \setmainfont{EB Garamond}
  \setmathfont{Garamond Math}

  % Load some missing symbols from another font.
  \setmathfont{STIX Two Math}[%
    range = {
      \sharp,
      \natural,
      \flat,
      \clubsuit,
      \spadesuit,
      \checkmark
    }
  ]
  \setmonofont[Scale=MatchLowercase]{Source Code Pro}
\else
  \usepackage[lf]{ebgaramond} % https://tug.org/FontCatalogue/quattrocento/
  \usepackage[oldstyle,scale=0.7]{sourcecodepro} % https://tug.org/FontCatalogue/sourcecodepro/
  \singlespacing
\fi
#+end_src

* Colors
#+begin_src latex
\usepackage[usenames,dvipsnames]{xcolor}
#+end_src

* CS Quotes
The [[https://ctan.org/pkg/csquotes][csquotes]] package offers context sensitive quotation facilities, improving the typesetting of inline quotes.

#+begin_src latex
\usepackage{csquotes}
#+end_src

To enclose quote environments with quotes from csquotes, see [[https://tex.stackexchange.com/questions/365231/enclose-a-custom-quote-environment-in-quotes-from-csquotes][the following TeX SE thread]].

#+begin_src latex
\def\signed #1{{\leavevmode\unskip\nobreak\hfil\penalty50\hskip1em
    \hbox{}\nobreak\hfill #1%
    \parfillskip=0pt \finalhyphendemerits=0 \endgraf}}

\newsavebox\mybox
\newenvironment{aquote}[1]
{\savebox\mybox{#1}\begin{quote}\openautoquote\hspace*{-.7ex}}
  {\unskip\closeautoquote\vspace*{1mm}\signed{\usebox\mybox}\end{quote}}
#+end_src

And then use quotes as:
#+begin_example
# The options derivative adds text after the environment. We use it to add the author.
#+ATTR_LATEX: :options {\cite{Frahm1994}}
#+begin_aquote
/Current (fMRI) applications often rely on "effects" or "statistically significant differences", rather than on a proper analysis of the relationship between neuronal activity, haemodynamic consequences, and MRI physics./
#+end_aquote
#+end_example

Note that org-ref links won't work here because the attr latex will be pasted as-is in the .tex file.

* Setspace for controlling line spacing

#+begin_src latex
\usepackage{setspace}
\setstretch{1.25}
#+end_src

* Parskip

Fine tuning of spacing between paragraphs. See [[https://tex.stackexchange.com/questions/161254/smaller-parskip-than-half-for-koma-script][thread here]].
#+begin_src latex
\setparsizes{0em}{0.1\baselineskip plus .1\baselineskip}{1em plus 1fil}
#+end_src

* Possible Equation improvements

Make the equation numbers follow the chapter, not the whole thesis.
#+begin_src latex
\numberwithin{equation}{chapter}
#+end_src

* Minted

The minted package is used for highlighting source code.
#+begin_src latex
\usepackage{listings}
\usepackage{minted}
\setminted{autogobble=true,fontsize=\small,baselinestretch=0.8}
\setminted[python]{python3=true,tabsize=4}
\usemintedstyle{trac}
\lstset{abovecaptionskip=0}
\renewcommand{\listingscaption}{Code Snippet}
#+end_src

And reduce the distance between a minted listing and its caption.
#+begin_src latex
\AtEndEnvironment{listing}{\vspace{-16pt}}
#+end_src

* Scrhack

This package fixes some incompatibility errors between KOMAScript and other packages (namely minted). It has to be loaded at the end.
#+begin_src latex
\usepackage{scrhack}
#+end_src

* Background cover page

Add the cover image as background to the first page. Only do so when outputting a final version, because it significantly slows down the compilation times.
#+begin_src latex :tangle no
\usepackage[pages=some]{background}
\backgroundsetup{
  scale=1,
  color=black,
  opacity=0.9,
  angle=0,
  contents={%
    \includegraphics[width=\paperwidth,height=\paperheight]{/Users/user/Desktop/TectonicPlatesXVI.png}
  }%
}
#+end_src
