commit 587719b6e8ea33feaaa65d67b49b278c01f5b270
Author: Silas Brack <s174433@student.dtu.dk>
Date: Mon, 2 Jan 2023 00:33:36 +0100
First commit.
Diffstat:
40 files changed, 6159 insertions(+), 0 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,303 @@
+## Core latex/pdflatex auxiliary files:
+*.aux
+*.lof
+*.log
+*.lot
+*.fls
+*.out
+*.toc
+*.fmt
+*.fot
+*.cb
+*.cb2
+.*.lb
+
+## Intermediate documents:
+*.dvi
+*.xdv
+*-converted-to.*
+# these rules might exclude image files for figures etc.
+# *.ps
+# *.eps
+# *.pdf
+
+## Generated if empty string is given at "Please type another file name for output:"
+.pdf
+
+## Bibliography auxiliary files (bibtex/biblatex/biber):
+*.bbl
+*.bcf
+*.blg
+*-blx.aux
+*-blx.bib
+*.run.xml
+
+## Build tool auxiliary files:
+*.fdb_latexmk
+*.synctex
+*.synctex(busy)
+*.synctex.gz
+*.synctex.gz(busy)
+*.pdfsync
+
+## Build tool directories for auxiliary files
+# latexrun
+latex.out/
+
+## Auxiliary and intermediate files from other packages:
+# algorithms
+*.alg
+*.loa
+
+# achemso
+acs-*.bib
+
+# amsthm
+*.thm
+
+# beamer
+*.nav
+*.pre
+*.snm
+*.vrb
+
+# changes
+*.soc
+
+# comment
+*.cut
+
+# cprotect
+*.cpt
+
+# elsarticle (documentclass of Elsevier journals)
+*.spl
+
+# endnotes
+*.ent
+
+# fixme
+*.lox
+
+# feynmf/feynmp
+*.mf
+*.mp
+*.t[1-9]
+*.t[1-9][0-9]
+*.tfm
+
+#(r)(e)ledmac/(r)(e)ledpar
+*.end
+*.?end
+*.[1-9]
+*.[1-9][0-9]
+*.[1-9][0-9][0-9]
+*.[1-9]R
+*.[1-9][0-9]R
+*.[1-9][0-9][0-9]R
+*.eledsec[1-9]
+*.eledsec[1-9]R
+*.eledsec[1-9][0-9]
+*.eledsec[1-9][0-9]R
+*.eledsec[1-9][0-9][0-9]
+*.eledsec[1-9][0-9][0-9]R
+
+# glossaries
+*.acn
+*.acr
+*.glg
+*.glo
+*.gls
+*.glsdefs
+*.lzo
+*.lzs
+*.slg
+*.slo
+*.sls
+
+# uncomment this for glossaries-extra (will ignore makeindex's style files!)
+# *.ist
+
+# gnuplot
+*.gnuplot
+*.table
+
+# gnuplottex
+*-gnuplottex-*
+
+# gregoriotex
+*.gaux
+*.glog
+*.gtex
+
+# htlatex
+*.4ct
+*.4tc
+*.idv
+*.lg
+*.trc
+*.xref
+
+# hyperref
+*.brf
+
+# knitr
+*-concordance.tex
+# TODO Uncomment the next line if you use knitr and want to ignore its generated tikz files
+# *.tikz
+*-tikzDictionary
+
+# listings
+*.lol
+
+# luatexja-ruby
+*.ltjruby
+
+# makeidx
+*.idx
+*.ilg
+*.ind
+
+# minitoc
+*.maf
+*.mlf
+*.mlt
+*.mtc[0-9]*
+*.slf[0-9]*
+*.slt[0-9]*
+*.stc[0-9]*
+
+# minted
+_minted*
+*.pyg
+
+# morewrites
+*.mw
+
+# newpax
+*.newpax
+
+# nomencl
+*.nlg
+*.nlo
+*.nls
+
+# pax
+*.pax
+
+# pdfpcnotes
+*.pdfpc
+
+# sagetex
+*.sagetex.sage
+*.sagetex.py
+*.sagetex.scmd
+
+# scrwfile
+*.wrt
+
+# svg
+svg-inkscape/
+
+# sympy
+*.sout
+*.sympy
+sympy-plots-for-*.tex/
+
+# pdfcomment
+*.upa
+*.upb
+
+# pythontex
+*.pytxcode
+pythontex-files-*/
+
+# tcolorbox
+*.listing
+
+# thmtools
+*.loe
+
+# TikZ & PGF
+*.dpth
+*.md5
+*.auxlock
+
+# titletoc
+*.ptc
+
+# todonotes
+*.tdo
+
+# vhistory
+*.hst
+*.ver
+
+# easy-todo
+*.lod
+
+# xcolor
+*.xcp
+
+# xmpincl
+*.xmpi
+
+# xindy
+*.xdy
+
+# xypic precompiled matrices and outlines
+*.xyc
+*.xyd
+
+# endfloat
+*.ttt
+*.fff
+
+# Latexian
+TSWLatexianTemp*
+
+## Editors:
+# WinEdt
+*.bak
+*.sav
+
+# Texpad
+.texpadtmp
+
+# LyX
+*.lyx~
+
+# Kile
+*.backup
+
+# gummi
+.*.swp
+
+# KBibTeX
+*~[0-9]*
+
+# TeXnicCenter
+*.tps
+
+# auto folder when using emacs and auctex
+./auto/*
+*.el
+
+# expex forward references with \gathertags
+*-tags.tex
+
+# standalone packages
+*.sta
+
+# Makeindex log files
+*.lpz
+
+# xwatermark package
+*.xwm
+
+# REVTeX puts footnotes in the bibliography by default, unless the nofootinbib
+# option is specified. Footnotes are the stored in a file with suffix Notes.bib.
+# Uncomment the next line to have this generated file ignored.
+#*Notes.bib
+
+.vscode/
diff --git a/MANIFEST.md b/MANIFEST.md
@@ -0,0 +1,85 @@
+---
+
+# First Work: The kaobook Class
+
+MANIFEST.md
+Copyright 2020-2021 Federico Marotta
+
+This work may be distributed and/or modified under the
+conditions of the LaTeX Project Public License, either version 1.3
+of this license or (at your option) any later version.
+The latest version of this license is in
+
+ http://www.latex-project.org/lppl.txt
+
+and version 1.3 or later is part of all distributions of LaTeX
+version 2005/12/01 or later.
+
+This work has the LPPL maintenance status `maintained'.
+
+The Current Maintainer of this work is Federico Marotta.
+
+This work consists of all files listed below.
+
+---
+
+```
+kaobook/
+|-- kaobook.cls - book-specific definitions
+|-- kaohandt.cls - handout-specific definitions
+|-- kao.sty - main definitions
+|-- kaobiblio.sty - style of the bibliography
+|-- kaotheorems.sty - colorful styling of theorems
+`-- kaorefs.sty - commands for referencing
+```
+
+---
+
+# Second Work: Templates and Examples
+
+This work is released into the public domain using the CC0 code. To the
+extent possible under law, I waive all copyright and related or
+neighbouring rights to this work. To view a copy of the CC0 code, visit:
+
+ http://creativecommons.org/publicdomain/zero/1.0
+
+This work consists of all files listed below as well as the products of
+their compilation.
+
+---
+
+```
+kaobook/
+`-- examples/
+ |-- minimal_book/
+ | `-- main.tex
+ |-- minimal_report/
+ | `-- main.tex
+ |-- documentation/
+ | |-- main.tex
+ | |-- main.bib
+ | |-- glossary.tex
+ | `-- chapters/
+ | |-- appendix.tex
+ | |-- figsntabs.tex
+ | |-- introduction.tex
+ | |-- layout.tex
+ | |-- mathematics.tex
+ | |-- options.tex
+ | |-- preface.tex
+ | |-- references.tex
+ | `-- textnotes.tex
+ `-- machine_learning_project/
+ |-- sections/
+ | |-- introduction.tex
+ | |-- data.tex
+ | |-- results.tex
+ | `-- discussion.tex
+ |-- main.tex
+ `-- main.bib
+```
+
+As regards the files in the `kaobook/examples/documentation/images`
+directory, they were downloaded from [Wikimedia
+Commons](https://commons.wikimedia.org/wiki/Main_Page) and are
+attributed inside the example book where they are used.
+\ No newline at end of file
diff --git a/Makefile b/Makefile
@@ -0,0 +1,25 @@
+root = main
+dist = pdflatex
+latexmkopts = -$(dist) -shell-escape
+
+all: $(root).pdf
+.PHONY: $(root).pdf
+
+$(root).pdf: $(root).tex
+ latexmk $(latexmkopts) $(root)
+
+clean:
+ rm -rf `biber --cache`
+ rm -rf _minted-$(root)
+ latexmk -bibtex -CA
+ find . -name \*.aux -type f -delete
+ rm -f $(root).run.xml $(root).nlg $(root).nlo $(root).nls $(root).glo $(root).loa $(root).mw $(root).xdy indent.log
+
+watch: clean
+ latexmk $(latexmkopts) -pvc $(root)
+
+quick: clean
+ $(dist) -shell-escape $(root)
+
+view:
+ open $(root).pdf
+\ No newline at end of file
diff --git a/README.md b/README.md
@@ -0,0 +1,239 @@
+# kaobook
+
+## Acknowledgements
+
+This class is based on the work of [Ken Arroyo
+Ohori](https://3d.bk.tudelft.nl/ken/en/) for his doctoral thesis.
+The main ideas behind the layout can be found in this [blog
+post](https://3d.bk.tudelft.nl/ken/en/2016/04/17/a-1.5-column-layout-in-latex.html).
+The [Tufte-LaTeX class](https://github.com/Tufte-LaTeX/tufte-latex) has also been a
+source of ideas about the layout.
+
+My gratitude goes also to [Vel](https://www.vel.nz/), for his patience
+and his invaluable suggestions about the design, and to all the people
+who have contributed either on
+[GitHub](https://github.com/fmarotta/kaobook/graphs/contributors) or by
+sending e-mails.
+
+## Description
+
+The salient features of the class are as follows.
+
+* Wide margin to house captions, small figures or tables, and textual
+ notes.
+
+* Mini table of contents in the margin at the start of each chapter.
+
+* Easily-customisable chapter headings.
+
+* Flexible citations with references both in the margin and in the
+ bibliography at the end.
+
+* Powered by KOMA-Script.
+
+* Many commands have been redefined to ease the life of the user.
+
+A better description can be found at [LaTeX
+Templates](http://www.latextemplates.com/template/kaobook). If you think
+that a PDF is worth a thousand words, have a look at [this](example_and_documentation.pdf).
+
+## Showcase
+
+In the [examples](examples) directory you can find some sample
+documents, but here are some more books or reports created with the
+kaobook class. If you want to add a link to your work, please send me an
+email or open a pull request!
+
+* [Growing Open Source Projects with a Stable
+Foundation](https://www.cyrius.com/foss-foundations/growing-open-source-projects.pdf)
+* [Cours De Physique - Mécanique Classique](https://femto-physique.fr/mecanique/pdf/book_meca.pdf)
+
+## Getting Started
+
+If you are not familiar with LaTeX, I recommend starting with [this
+tutorial](https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes),
+which will give you a basic understanding of the language. As you will
+read in the tutorial, all LaTeX documents, including books, start by
+defining the so-called *class* of the document, for example 'article',
+or 'book'. Thus, the first line of a LaTeX document will be something
+like
+```
+\documentclass{book}
+```
+
+Each class provides a unique style and set of commands that can be used
+throughout the document. `kaobook` is just another class. The following
+paragraphs will outline how to use it.
+
+### On Overleaf
+
+Browse to the [latextemplates.com page for
+kaobook](https://www.latextemplates.com/template/kaobook) and start
+editing the `main.tex` file to fill it with your own contents.
+
+### On your computer
+
+Download the latest release from GitHub. At the root of the repository,
+create a new `.tex` file and make sure that the first line reads
+`\documentclass{kaobook}` (for books) or `\documentclass{kaohandt}` (for
+reports). Then, fill the file with your LaTeX contents.
+
+Important: when we say `\documentclass{kaobook}`, LaTeX needs to know
+where the `kaobook` file is. This means that the `.tex` file should be
+in the same folder as the `kaobook.cls` file, *i.e.* at the root of the
+repository. (There are exceptions to this rule. For instance, the
+kaobook files can be placed in a folder that is automatically searched
+by LaTeX. This, way, the `.tex` file with your contents can be placed
+anywhere; check out the [instructions](instructions) directory for
+hints.)
+
+If you don't want to start from scratch, go to the `examples` directory,
+and find a template that you like; then, copy the corresponding
+`main.tex` at the root of the repository, and start editing it, filling
+it with your contents.
+
+### Other
+
+Check out the [instructions](instructions) directory for additional
+guidance. There, you can read about setting up your own sharelatex
+server or integrating kaobook with your local LaTeX installation.
+
+---
+
+The class is documented and exemplified in the
+[example\_and\_documentation.pdf](example_and_documentation.pdf) file.
+The easiest way to start using the class is to open one of the examples
+and start editing them.
+
+## Compiling the examples
+
+In the `examples` directory of this repository you can find several
+documents exemplifying the kaobook class. For the sake of simplicity
+I shall list the commands to compile only the documentation, but the
+compilation of the other examples would proceed in the same way. Also,
+you can replace pdflatex with your favorite engine, like *e.g.* xelatex.
+
+### From the command line (Unix-like operating systems)
+
+`cd` into the root of the repository, and run
+```
+pdflatex -output-directory examples/documentation main.tex
+biber -output-directory examples/documentation main
+pdflatex -output-directory examples/documentation main.tex
+pdflatex -output-directory examples/documentation main.tex
+pdflatex -output-directory examples/documentation main.tex
+```
+
+To compile the glossary and nomenclature as well, `cd` into the
+`examples/documentation` directory and run
+```
+makeindex main.nlo -s nomencl.ist -o main.nls
+makeglossaries main
+```
+Then, `cd` back into the root of the repository and re-run pdflatex.
+
+NOTE: sometimes LaTeX needs more than one run to get the correct
+position of each element; this is true in particular for the positioning
+of floating elements like figures, tables, and margin notes.
+Occasionally, LaTeX can need up to four re-runs, so If the alignment of
+margin elements looks odd, or if they bleed into ther main text, try
+running pdflatex one more time.
+
+## Updating kaobook
+
+To update kaobook you should download the whole repository (or one of
+the releases) again, and replace all of your old files with the newer
+ones, *except* for the main.tex and the files that you have created,
+like the chapters of the book. The crucial files that pertain to kaobook
+and that you should always update are:
+
+1. `kaobook.cls`;
+2. `kaohandt.cls`;
+3. `kao.sty`;
+3. `kaobiblio.sty` (optional);
+4. `kaorefs.sty` (optional);
+5. `kaotheorems.sty` (optional);
+
+These files should be in the same folder as your `main.tex`. Even if a
+file has not been modified, I would still suggest to replace everything
+because it is easier.
+
+In practice, I would do as follows. I would have a directory, called for
+example 'my\_book', with all the files necessary for the book:
+`kaobook.cls`, `kaohand.cls`, the `*.sty` files, and the `main.tex`. I
+like to have a separate file for each chapter, so I would also have a
+directory called `chapters`, with all the `.tex` files with the actual
+chapters. Then, when I want to update kaobook, I would download the
+GitHub repository (or one of the releases) into a directory called
+'kaobook', and finally copy the `kaobook.cls`, `kaohandt.cls`, and the
+whole `styles` directory from 'kaobook' to 'my\_book'. Once the update
+is completed, the whole 'my\_book' directory can be uploaded on Overleaf
+or on a personal ShareLaTeX server.
+
+Alternatively, advanced users can download the repository in their local
+texmf tree; see the [instructions](instructions) directory for hints.
+
+## Repository Schema
+
+There are two main class files: `kaobook.cls`, used for books, and
+`kaohandt.cls`, used for reports or handouts; both heavily rely on
+`kao.sty`, which contains the bulk of the definitions that are common to
+both classes. In the future there may be another class for theses.
+
+Some examples and templates are listed in the `examples` directory. The
+book that documents the class is an example itself, and the pdf has been
+copied to the root of the repository so that it will be found more
+easily.
+
+The `*.sty` files contain additional packages that are used by the
+class, but in principle they are independent of it (even though in
+practice it is still not so).
+
+If you want to do something more peculiar, the `instructions` directory
+may contain what you need.
+
+## Contributing
+
+Do you like the design, but you found a bug? Is there something you
+would have done differently? Any contribution is welcome! Moreover, even
+if you'd rather not tamper with the code it is *not* forbidden to send
+me the documents you compiled using the kaobook class.
+
+Since the content of this repository is published at [LaTeX
+Templates](http://www.latextemplates.com/), if you wish to contribute by
+changing the code you must follow the style guidelines of the site:
+extensive commenting and clear separation of the code into nicely
+formatted blocks.
+
+## Supporting
+
+I am always happy to help as much as I can, and I am glad if someone
+uses the `kaobook` class for their works, so there is no real need to do
+anything: the kaobook can be used just like any other LaTeX package (no
+need to add copyright statements). However, if you want to acknowledge
+kaobook, adding somewhere a sentence like 'This book was typeset with the
+kaobook class' would suffice.
+
+Coffee keeps me awake and helps me writing a better LaTeX template. As
+another way to contribute, you can buy me a coffee through PayPal:
+https://paypal.me/marofede.
+
+## License
+
+This repository contains two independent works. On the one hand, the
+kaobook class, consisting of `kaobook.cls`, `kaohandt.cls`, and
+`kao.def` files plus all of the files listed in the `styles` directory;
+on the other hand, the templates and the examples in the `examples`
+directory.
+
+The first work is licensed under the [LaTeX Project Public
+License](https://www.latex-project.org/lppl/), so if you want to modify
+and/or distribute the `*.cls` and `*.sty` files pertaining to this work
+you have to complain with the terms of the license. However, if you just
+want to use the class to compile your documents you need not worry about
+the license.
+
+The second work is released into the public domain with a Creative
+Commons Zero License.
+
+Read [MANIFEST.md](MANIFEST.md) for the details.
+\ No newline at end of file
diff --git a/chapters/abstract.tex b/chapters/abstract.tex
@@ -0,0 +1,9 @@
+\chapter*{Abstract}
+\addcontentsline{toc}{chapter}{Abstract}
+
+\lipsum[1-3]
+
+\chapter*{Resum\'e}
+\addcontentsline{toc}{chapter}{Resum\'e}
+
+\lipsum[1-3]
diff --git a/chapters/acknowledgments.tex b/chapters/acknowledgments.tex
@@ -0,0 +1,26 @@
+\chapter*{Acknowledgments}
+\addcontentsline{toc}{chapter}{Acknowledgments} % Add the ackn to the table of contents as a chapter
+
+\blackout
+{
+To S{\o}ren's group --- in particular, Marco --- for always making me feel like a part of the group, and like my contribution was valued.
+
+To Marco and Frederik for giving me the chance to dip my toes in ``real'' research in the rawest, realest way possible, with what I perceive to be all of the highs and the lows which constitute it.
+
+To S{\o}ren, for always being up for a great chat, for giving me the opportunity to work on difficult and novel problems (such as preconditioning), and for approaching everything with kindness.
+I don't think I could have chosen a better supervisor.
+
+To J\'ulia, Brandon and Julia for the constant laughs and endless opportunities to relax on the couch when working was too much (memes surrounding me working on the couch aside)
+And to Adam, with who I shared the experience of writing a thesis --- highs and lows both.
+
+To Jason and Kristi for always cultivating/imparting/instilling/nurturing in me a sense of curiosity and independence
+And for never forgetting me in the supermarket.
+
+Lorenza
+
+And above all else, \emph{to Steampunk}.
+}
+
+\begin{flushright}
+ \textit{Silas Brack}
+\end{flushright}
diff --git a/chapters/appendix.tex b/chapters/appendix.tex
@@ -0,0 +1,54 @@
+\setchapterstyle{lines}
+\labch{appendix}
+% \setchapterpreamble[u]{\margintoc}
+
+\begin{algorithm}
+\caption{Multi-Shift Minimum Residual (msMINRES)}\label{alg:msminres}
+\Input{$\bm K \succ 0$, $\bm b$, $\bm P \succ 0$, $t_1, \ldots, t_Q$, $J > 0$}
+\Output{$\bm c_1 = (\bm K + t_1)^{-1} \bm b, \ldots, \bm c_Q = (\bm K + t_Q)^{-1} \bm b$}
+$\norm{\bm b}_2 \gets \left( \bm b \cdot \bm b \right)^{1/2}$\;
+$\bm b \gets \bm b / \norm{\bm b}_2$\;
+$\bm p \gets \bm K \bm b$\;
+$\bm z_{-1} = \bm 0$\;%Like \bm p
+$\bm z_{0} = \bm b$\;
+$\bm q_{0} = \bm P \bm z_{0}$\;
+$\beta_{0} = \left(\bm z_{0} \cdot \bm q_{0} \right)$\;
+\For{$q \gets 1$ \KwTo $Q$}{
+ $\bm c^{(q)}_{0} \gets \bm 0$\;
+ $\bm d^{(q)}_{0}, \bm d^{(q)}_{-1} \gets \bm 0$\;%Like \bm b
+ $\cos^{(q)}_0, \cos^{(q)}_{-1} \gets 1$\;
+ $\sin^{(q)}_0, \sin^{(q)}_{-1} \gets 0$\;
+ $\varphi^{(q)}_0 \gets \beta_{0}$\;
+}
+\For{$j \gets 1$ \KwTo $J$}{
+ $\bm p \gets \bm K \bm q_{j-1}$\; % Removed negative because we input the "negative" K matrix
+ % $\bm p \gets - \bm K \bm q_{j-1}$\;
+ $\alpha_{j} \gets \bm p \cdot \bm q_{j-1}$\;
+ $\bm z_j \gets \bm p - \alpha_j \bm z_{j-1} - \beta_{j-1} \bm z_{j-2}$\;
+ $\bm q_j \gets \bm P \bm z_j$\;
+ $\beta_j \gets \left({\bm z_j \bm q_j}\right)^{1/2}$\;
+ % $\beta_j = \sqrt{\bm z_j \bm q_j}$\;
+ $\bm z_j \gets \bm z_j / \beta_{j}$\;
+ $\bm q_j \gets \bm q_j / \beta_{j}$\;
+ \For{$q \gets 1$ \KwTo $Q$}{
+ $\epsilon^{(q)}_{j} \gets \sin^{(q)}_{j-2} \beta_{j-1}$\;%\Comment{Start Givens rotation}
+ $\zeta^{(q)}_{j} \gets \cos^{(q)}_{j-2} \beta_{j-1}$\;
+ $\alpha^{(q)}_{j} \gets \alpha_{j} + t_q$\;%\Comment{Compute shifted alpha}
+ $\eta^{(q)}_j \gets \cos^{(q)}_{j-1} \alpha^{(q)}_{j} - \sin^{(q)}_{j-1} \zeta^{(q)}_{j}$\;%\Comment{Givens rotation from 1 step ago}
+ $\zeta^{(q)}_{j} \gets \cos^{(q)}_{j-1} \zeta^{(q)}_{j} + \sin^{(q)}_{j-1} \alpha^{(q)}_{j}$\;
+ $r^{(q)}_{j} \gets \left({\eta^{(q)2}_{j} + \beta^2_{j}}\right)^{1/2}$\;%\Comment{Compute next Givens terms}
+ % $r^{(q)}_{j} \gets \sqrt{\eta^{(q)2}_{j} + \beta^2_{j}}$\;%\Comment{Compute next Givens terms}
+ $\cos^{(q)}_{j} \gets \eta^{(q)}_{j} / r^{(q)}_{j}$\;
+ $\sin^{(q)}_{j} \gets \beta^{(q)}_{j} / r^{(q)}_{j}$\;
+ $\eta^{(q)}_{j} \gets \eta^{(q)}_{j} \cos^{(q)}_{j} + \sin^{(q)}_{j} \beta_{j}$\;%\Comment{Apply current Givens rotation}
+ $\varphi^{(q)}_{j} \gets - \varphi^{(q)}_{j-1} \sin^{(q)}_{j}$\;%\Comment{Apply the latest Givens rotation to the Lanczos-rhs, computing the scale terms for the search vectors}
+ $\varphi^{(q)}_{j-1} \gets \varphi^{(q)}_{j-1} \cos^{(q)}_{j}$\;
+ $\bm d^{(q)}_j \gets \left( \bm q_{j-1} - \zeta^{(q)}_{j} \bm d^{(q)}_{j-1} - \epsilon^{(q)}_{j} \bm d^{(q)}_{j-2} \right) / \eta^{(q)}_{j}$\;%\Comment{Get the new search vector}
+ $\Delta \bm c^{(q)}_{j} \gets \bm d^{(q)}_{j} \varphi^{(q)}_{j-1}$\;
+ $\bm c^{(q)}_{j} \gets \bm c^{(q)}_{j-1} + \Delta \bm c^{(q)}_{j}$\;%\Comment{Update the solution}
+ }
+}
+\For{$q \gets 1$ \KwTo $Q$}{
+ $\bm c_q \gets \bm c^{(q)}_J \norm{\bm b}_2$\;
+}
+\end{algorithm}
diff --git a/chapters/conclusion.tex b/chapters/conclusion.tex
@@ -0,0 +1,5 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Conclusion}
+\labch{conclusion}
+
+
diff --git a/chapters/experiments.tex b/chapters/experiments.tex
@@ -0,0 +1,75 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Experiments}
+\labch{experiments}
+
+\section{The Advantage of Working With Hessian-Vector Products}
+
+How much larger matrices can we work with if we don't need to instantiate the whole Hessian?
+How much faster is it to compute the GVP versus, say, the HVP or the explicit Hessian-vector product?
+
+describe experimental setup, how were results generated etc.
+Note that computational time includes \emph{computing} the Hessian, and then multiplying it by a vector.
+
+for values of 100k parameters or more, the manual computation failed, for both the inverse and the vector product (because it's the explicit \(N \times N\) matrix instantiation that fails)
+The largest number of parameters tested is 100M.
+Even the implicit methods failed for 1B parameters, although this failure occurred in the instantiation of a single random vector of that size (and so would fail on this hardware for any method).
+Notably, however, these implicit Hessian-vector product methods allowed for computation of matrix products and solves for problems up to 1000 times greater than the explicit method.
+
+\cref{fig:hessian-profile}
+
+\begin{marginfigure}
+ \centering
+ \includegraphics{hessian_profile.pdf}
+ \caption{Top: comparison of the performance for Hessian-vector, GGN-vector, and manual matrix-vector products. Bottom: comparison of the performance of inverse-vector products for the same methods. Implicit inverse performed via conjugate gradient for Hessian-vector and GGN-vector products.}
+ \label{fig:hessian-profile}
+\end{marginfigure}
+
+some analysis
+
+comment on performance of manual products
+comment on weird dip in inverse, possibly something to do with caching or something like that? like the size of your cache
+
+however, due to the scale of the figure, it isn't completely clear how the performance of the Hessian-vector product compares to the performance of the GGN-vector product.
+We can then plot the speedup from going from the HVP to the GVP as a function of the number of model parameters.
+This can be calculated as the ratio between the wall-clock time of the HVP to the wall-clock time of the GVP.
+Thus, larger values suggest greater performance gains for the GVP, while values closer to one suggest there is no performance gain.
+These results can be seen in \cref{fig:hessian-profile-ratios}.
+
+\begin{marginfigure}
+ \centering
+ \includegraphics{hessian_profile_ratios.pdf}
+ \caption{Comparison of the speedup from computing the Hessian-vector product (HVP) versus the GGN-vector product (GVP) (top) and inverse HVP versus inverse GVP using the conjugate gradient method (bottom). Speedup is calculated as the ratio of the wall-clock time of the HVP to the GVP. It can be seen that the two methods have the same time complexity (up to a linear factor).}
+ \label{fig:hessian-profile-ratios}
+\end{marginfigure}
+
+Notice that the number of parameters on the \(x\)-axis are exponentially increasing.
+This is in line with the theory, as noted in \cref{ssec:practical-ggn} (SHOULD IT BE MOVED HERE?)
+
+\section{Sampling Ablation}
+
+As we vary \(q \in [1, \ldots, 20]\), how does performance vary?
+What metrics can we use?
+
+
+
+\section{Sampling for the Sine Function}
+
+Note that the choice of \(\alpha\) is a non-trivial one
+
+\begin{marginfigure}
+ \centering
+ \includegraphics{sin.pdf}
+ \caption{Visualisation of the predicted output of multilayer perceptron (MLP) modelling a sine curve.}
+ \label{fig:sin}
+\end{marginfigure}
+
+\begin{figure}
+ \centering
+ \includegraphics{chisq_sin.pdf}
+ \caption{Chi-squared sample plot for testing normality of Laplace approximation samples for a neural network modelling \(f(x) = \sin(x)\). The sample Mahalanobis distances are Chi-squared distributed, then the samples themselves are multivariate normal with correct (fixed) covariance. As can be seen by looking at the histogram and the q-q plot, samples are multivariate normal.}
+ \label{fig:chisq-sin}
+\end{figure}
+
+\section{Sampling for MNIST}
+
+
diff --git a/chapters/introduction.tex b/chapters/introduction.tex
@@ -0,0 +1,63 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Introduction}
+\labch{intro}
+
+% \section{The Problem}
+\section{Bayesian Deep Learning}
+
+Bayesian methods allow for estimates of uncertainty which enable more
+efficient usage of data (i.e., via active learning) and avoid
+overfitting. Furthermore, these uncertainty estimates improve model
+interpretability and assessment of model predictive confidence.
+
+% \section{The State of Research in the Area}
+\section{Current Methods}
+
+Currently, approximate Bayesian methods are either expensive to compute
+(Markov Chain Monte Carlo), are significantly more difficult to implement (such as
+variational inference), or simply perform poorly and are limited in
+their Bayesian interpretation (MC dropout). As such, there is demand for
+a method which exhibits the same computational cost as the optimization
+algorithms for deterministic neural networks while providing accurate
+posterior approximations and working out-of-the-box for any given
+architecture.
+
+% Laplace approximations are simple, yet prin-
+% cipled, posterior approximations suitable for
+% Bayesian modeling. Sadly these approximations
+% rely on access to the Hessian matrix of the loss
+% with respect to the model parameters. For large
+% models, it is even intractable to store this matrix,
+% and crude approximations (e.g. only storing the
+% Hessian diagonal) are called upon. We investigate
+% storing the Hessian implicitly as means to avoid
+% the fuzz.
+
+% \section{My Solution}
+\section{Large-Scale Laplace}
+
+The ideas presented in this article essentially constitute a compilation of advice given by academics from multiple fields and institutions,~\cite{miani2022laplacian}
+
+% \section{Research Objectives}
+
+This should be discussed in a bit more detail in a thesis, since there
+are certain objectives discussed in the project plan.
+In an article, a quick sentence which summarises the rest of the
+introduction and states succinctly exactly which problem will be tackled
+and which approach will be made should suffice.
+
+In this project, we strive to develop an effortless Bayesian method.
+More specifically, this project investigates the connections between the
+Laplace approximation and the approximate second-order derivatives used
+in modern optimizers, such as Adam. We use a sampling-based training
+procedure, where a sample is first drawn from a Gaussian
+weight-posterior, a gradient step is performed on this sampled neural
+network, and the variance of the weight-posterior is updated with an
+approximate Hessian. Approximating the Hessian is the most time
+consuming and painful-to-engineer step of this training procedure. In
+this project, we tap into the potential of modern machine learning
+frameworks to efficiently approximate the Hessian.
+
+\section{JAX and XLA}
+
+\cite{jax2018github,sabne2020xla}
diff --git a/chapters/literature.tex b/chapters/literature.tex
@@ -0,0 +1,263 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Literature}
+\labch{literature}
+% http://saul.cpsc.ucalgary.ca/pmwiki.php/GradTips/GradTipsLiteratureReview
+
+\section{Adam}
+
+% How does adam work?
+
+\cite{sutskever2013importance}
+\cite{kingma2014adam}
+
+% Integrate with the previous section
+Note that Adam approximates the second moment of the gradient of the loss, that this corresponds to the variance of this gradient, and that this is the diagonal of the Fisher information matrix, as we defined in \cref{sec:fisher-information} (\cref{eq:gradient-covariance})!
+% This is a common approximation in practice, as the diagonal of the Fisher information matrix is often sufficient to approximate the curvature of the log-likelihood.
+
+% \section{K-FAC}
+
+% Another alternative to approximating the Fisher information matrix is by calculating the Kronecker-factored Approximate Curvature (K-FAC).
+% \cite{martens2015optimizing}
+
+% \section{Noisy K-FAC}
+% \cite{zhang2018noisy}
+
+% \section{Gauss-Newton Optimization}
+% \cite{botev2017practical}
+
+% \section{Variational Adaptive-Newton}
+
+% \cite{khan2017variational}
+
+% \section{Natural Gradient Variational Inference}
+
+% Natural gradient VI (VOGN)~\cite{osawa2019practical} approximates the posterior as
+% %
+% \begin{equation*}
+% p(\bm \theta) \approx \normal(\bm 0, \bm I / \delta)
+% \end{equation*}
+% %
+% with an update of the natural parameter
+% \begin{equation*}
+% \bm \lambda_{t+1} = (1 + \tau \rho) \bm \lambda_t - \rho \nabla_\mu \E{q}{\bar{\ell}(\bm \theta + 1 / 2 \tau \delta \bm\theta\T \bm\theta)}
+% \end{equation*}
+
+\section{Laplace Approximation}\label{sec:laplace}
+
+In the Laplace approximation (LA)~\cite{laplace1774memoire,bishop1995neural,mackay2003information,daxberger2021laplace}, the posterior is approximated by a Gaussian, similarly to mean-field VI.
+However, instead of finding the optimal Gaussian distribution locations and scales by maximising the ELBO, the LA finds the location by computing the MAP solution \(\bm \theta_{\textsc{map}}\) and the scale by approximating the log posterior with a second degree Taylor expansion around this solution (\(\bm \theta_0 = \bm \theta_{\textsc{map}}\)) and determining the curvature via its Hessian matrix \(\bm \Lambda = - \left.\nabla^2_{\bm \theta} \log p(\bm \theta \given \bm y) \right|_{\bm \theta_\mathrm{MAP}}\).
+Since the Taylor expansion is performed around the MAP solution, the first order derivative is zero, and the expansion is simply given by
+\begin{align*}
+ \ln p(\bm \theta \given \bm y)
+ \approx{} & \ln p(\bm \theta_{\textsc{map}} \given \bm y) +
+ \\ & + \frac{1}{2} (\bm \theta - \bm \theta_{\textsc{map}})\T \left( \left.\nabla^2_{\bm \theta} \ln p(\bm \theta \given \bm y) \right|_{\bm \theta_\mathrm{MAP}} \right) (\bm \theta - \bm \theta_{\textsc{map}}) %\label{eq:laplace-taylor}
+ \\ \Rightarrow \tilde{p}(\bm \theta \given \bm y) \approx{}& p(\bm \theta_{\textsc{map}} \given \bm y) \exp\left(- \frac{1}{2} (\bm \theta - \bm \theta_{\textsc{map}})\T \bm \Lambda (\bm \theta - \bm \theta_{\textsc{map}})\right) \propto p(\bm \theta \given \bm y) \nonumber
+\end{align*}
+where \(\tilde{p}(\bm \theta \given \bm y)\) corresponds to the unnormalised posterior.
+Normalising it yields
+\begin{align*}
+ p(\bm\theta \given \bm y) \approx{} & \sqrt{\frac{\det (\bm \Lambda)}{(2 \pi)^D}} \exp\left(- \frac{1}{2} (\bm \theta - \bm \theta_{\textsc{map}})\T \bm \Lambda (\bm \theta - \bm \theta_{\textsc{map}})\right) \nonumber
+ % \\={}& \sqrt{\frac{\det (- \bm H)}{(2 \pi)^D}} \exp\left(- \frac{1}{2} (\bm \theta - \bm \theta_0)\T (- \bm H) (\bm \theta - \bm \theta_0)\right) \nonumber
+ \\={}& \normal(\bm \theta_{\textsc{map}}, \bm \Lambda^{-1}).%\label{eq:laplace}
+\end{align*}
+
+We have now shown that approximating the log-posterior with a second degree Taylor expansion around the \(\bm\theta_{\textsc{map}}\) corresponds to approximating the posterior with a Gaussian distribution given by \(\normal(\bm \theta_{\textsc{map}}, \bm \Lambda^{-1})\) where \(\bm \Lambda = - \left.\nabla^2_{\bm \theta} \log p(\bm \theta \given \bm y) \right|_{\bm \theta_{\textsc{map}}}\).
+
+In practice, we can determine the Hessian of the loss with respect to the parameters, where the loss is given by the negative log-posterior.
+In this case, we get \(\bm \Lambda = \left. - \nabla^2_{\bm \theta} \log p(\bm \theta \given \bm y) \right|_{\bm \theta_{\textsc{map}}} = \left. \nabla^2_{\bm \theta} \mathcal{L} \right|_{\bm \theta_{\textsc{map}}}\) and the posterior is approximated by \(\normal(\bm \theta_{\textsc{map}}, \bm \Lambda^{-1})\).
+
+\cref{eq:negative-log-likelihood-ggn}
+
+% \begin{align}
+% \bm \Lambda = \sum_i^N \bm J_i\T \bm H_i \bm J_i
+% \end{align}
+
+% \section{Linear Response Variational Bayes}
+
+% % https://martiningram.github.io/vi-with-good-covariances/
+% % https://github.com/martiningram/jax_advi/blob/main/jax_advi/lrvb.py#L49
+
+% \cite{giordano2018covariances}
+
+% INLA: Laplace for nested models (hierarchical models)
+% \cite{rue2017bayesian}
+
+% \section{Online Laplace}
+
+% \cite{miani2022laplacian}
+
+% \section{Bayes’ Theorem}
+
+% Bayes' theorem describes the probability of an event occurring with respect to prior knowledge of that event.
+% Specifically for modelling, it can be used to relate latent variables with observed variables, allowing distributions on these latent variables to be inferred.
+% We therefore want to find the posterior \(p(\bm \theta \given \bm y)\) by
+% According to Bayes' theorem, the posterior is given by
+% \begin{align}\label{eq:bayes}
+% p(\bm \theta \given \bm y) = \frac{\lik p(\bm \theta)}{p(\bm y)}
+% \end{align}
+% where \(\lik\) is the likelihood, parameterised by the model parameters \(\bm \theta\), \(p(\bm \theta) = p(\bm \theta; \bm \alpha)\) is the prior, parameterised by its hyper-parameters \(\bm \alpha\), and \(p(\bm y) = p_{\bm \theta}(\bm y)\) is the marginal likelihood (or evidence).
+
+% Furthermore, the posterior predictive probability, i.e., the probability of observing some new data, is given by
+% \begin{align}
+% p(\bm y_{\mathrm{new}} \given \bm y) = \int p(\bm y_{\mathrm{new}} \given \bm \theta) p(\bm \theta \given \bm y)\,d\bm\theta.
+% \end{align}
+
+% \section{Variational Inference}
+
+% In variational inference (VI), we minimise the KL divergence between the posterior distribution \(p(\bm \theta \given \bm y)\) and a variational distribution \(q_{\bm \phi}(\bm \theta)\) parameterised by its variational parameters \(\bm \phi\) by optimising with respect to these parameters.
+% The KL divergence is defined as
+% \begin{align}
+% \kld{q(\bm \theta)}{\lik} \equiv{} & \E{q}{\log \frac{q(\bm \theta)}{\lik}} \label{eq:kld} \\
+% ={} & \E{q}{\log q(\bm \theta)} - \E{q}{\log \lik} \nonumber
+% \end{align}
+% However, the KL divergence contains the evidence term \(\log p(\bm y)\), which is intractable.
+% Instead, we define a lower bound on this marginal likelihood term that is surrogate to the KL divergence (known as the ELBO).
+% Since they are surrogate, ELBO fulfils the property \(\elbo = \log p(\bm y) - \kld{q(\bm \theta)}{\lik}\), minimising the KL divergence is equivalent to maximising the ELBO, which is defined as
+% \begin{align}
+% \elbo \equiv{} & \E{q}{\log p(\bm y, \bm \theta)} - \E{q}{\log q(\bm \theta)} \nonumber \\
+% ={} & \E{q}{\log \lik} + \E{q}{\log p(\bm \theta)} - \E{q}{\log q(\bm \theta)} \nonumber \\
+% ={} & \E{q}{\log \lik} - \underbrace{- \E{q}{\log p(\bm \theta)}}_{\text{Cross-entropy}} + \underbrace{- \E{q}{\log q(\bm \theta)}}_{\text{Entropy}} \label{eq:elbo}
+% \end{align}
+% where the first term \(\E{q}{\log \lik}\) is the data (or likelihood) term, \(-\E{q}{\log p(\bm \theta)}\) is the cross-entropy of the prior with respect to the variational approximation, and \(-\E{q}{\log q(\bm \theta)}\) is the entropy term of the variational approximation.
+% The last two terms can be interpreted as the regularising KL divergence (or relative entropy) from the prior to the variational approximation, \(\kld{q(\bm \theta)}{p(\bm \theta)} = \E{q}{\log q(\bm \theta)} - \E{q}{\log p(\bm \theta)}\).
+% Furthermore, the likelihood term in the ELBO can be decomposed further by assuming independence in the observations, calculated as the term given by \(\E{q}{\log \lik} = \frac{1}{N} \sum^N_i \E{q}{\log p(y_i \given \bm \theta)}\).
+% Overall, BNNs have been found to be relatively ineffective unless the number of observations is greater than the number of model parameters.
+
+% \section{Methods}\label{ssec:methods}
+
+% Multiple approximate inference methods were implemented.
+% Specifically, \emph{maximum a posteriori} estimation, Laplace approximation, VI with mean-field, full-rank, low-rank and radial variational families, deep ensembles, and MultiSWAG.
+% These methods will be described in the following sections.
+
+% \subsection{Maximum a Posteriori Estimation}
+
+% \emph{Maximum a posteriori} estimation finds a point estimate of the posterior given by its maximum.
+% It can therefore be interpreted as a Delta distribution estimate of the posterior \(p(\bm \theta \given \bm y) \approx \delta(\bm \theta_\mathrm{MAP})\), where \(\bm \theta_{\text{MAP}}\) is defined as
+% \begin{align}
+% \bm \theta_\mathrm{MAP}
+% ={} & \max_{\bm \theta} p(\bm \theta \given \bm y) \nonumber \\
+% ={} & \max_{\bm \theta} \log p(\bm \theta \given \bm y) \nonumber \\
+% ={} & \max_{\bm \theta} \left[ \log \lik + \log p(\bm \theta) \right] \nonumber \\
+% ={} & \max_{\bm \theta} \left[ \sum_{i=1}^N \log p(y_i \given \bm \theta) + \log p(\bm \theta) \right].
+% \end{align}
+% As an optimisation problem, maximising the posterior corresponds to minimising the regularised loss,
+% \begin{align}
+% \bm \theta_{\text{MAP}} ={} & \arg \min_{\bm \theta} \mathcal{L}(\bm y; \bm \theta) \\
+% ={} & \arg \min_{\bm \theta} \left[ -\sum_{i=1}^N \log p(y_i \given \bm \theta) - \log p(\bm \theta) \right].
+% \end{align}
+% In this formulation, the term \(-\sum_{i=1}^N \log p(y_i \given \bm \theta)\) is known as the empirical loss or reconstruction loss and the term \(\log p(\bm \theta)\) is the regulariser.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+% \subsection{Mean-Field Variational Approximation}\label{ssec:mfvi}
+
+% One common variational family used to approximate the real posterior is a product of independent Gaussian distributions (the mean-field approximation) such that each model parameter is sampled from a normal distribution and is independent of all other model parameters, yielding a Gaussian with a diagonal covariance matrix.
+% In mean-field VI, model weights are sampled from an approximate posterior \(q(\bm \theta) \approx p(\bm{\theta} \given \bm y)\) as
+% \begin{align}
+% q(\bm \theta) ={} & \normal(\bm \mu, \bm \sigma) \Leftrightarrow \nonumber \\
+% \bm{\theta} ={} & \bm{\mu} + \bm{\sigma} \circ \bm{\epsilon}\label{eq:meanfield}
+% \end{align}
+% where \(\bm{\epsilon} \sim \normal(\bm 0, \identity)\).
+% For this type of variational approximation, the entropy term is given by \(\entropy{q(\theta)} = - \sum_i \log \sigma_i\) and the cross-entropy of the prior relative to the variational approximation is calculated via Monte Carlo simulation by taking the prior log probability with respect to mean-field posterior samples as
+% \begin{align}
+% \crossentropy{q(\bm\theta)}{p(\bm\theta)} ={} & - \int q(\bm\theta) \log(p(\bm\theta))\,d\bm\theta \nonumber \\
+% \approx{} & \frac{1}{S} \sum^S_{s=1} \log p(\bm{\theta}^{(s)})
+% \end{align}
+% where \(\bm{\theta}^{(s)} \sim \normal(\bm{\mu}, \bm{\sigma})\).
+
+% The use of the mean-field approximation for BNNs has been found to be unreliable~\cite{wu2018deterministic} and, for increasingly wide neural networks, converges towards the prior~\cite{coker2021wide}.
+% However, for \emph{deep} BNNs, the mean-field assumption may be reasonable~\cite{farquhar2020liberty}.
+
+% \subsection{Full-Rank Variational Approximation}
+
+% As an alternative to the mean-field approximation, a full-rank approximation doesn't assume independence of the model parameters, instead being sampled as
+% \begin{align}\label{eq:fullrank}
+% q(\bm \theta) = \normal(\bm \mu, \bm \Sigma_{\mathrm{FR}}).
+% \end{align}
+% For \(D\) model parameters, while a mean-field approximation has scale parameters \(\bm \sigma^D\), the full-rank approximation has scale parameters \(\bm \Sigma_{\mathrm{FR}}^{D \times D}\).
+% The variational parameters scale quadratically with the number of model parameters, which constitutes a significant limitation of this type of approximation.
+% Specifically, since NNs are typically overparameterised, possessing from thousands to millions to billions of parameters, the quadratic number of variational parameters makes this type of VI infeasible for BNNs.
+% As such, full-rank VI is not applied in any of the experiments in this paper.
+
+% \subsection{Low-Rank Variational Approximation}
+
+% As a compromise between the last two methods, a low-rank variational approximation uses a low-rank approximation of the covariance matrix \(\bm \Sigma_{\mathrm{LR}}\).
+% \begin{align}\label{eq:lowrank}
+% q(\bm \theta) = \normal(\bm \mu, \bm \Sigma_{\mathrm{LR}}).
+% \end{align}
+% This approximation is obtained by reconstructing a covariance matrix paremeterised by the covariance factor \(\tilde{\bm \Sigma}\) and diagonal element vector \(\bm \sigma_{\text{diag}}\) as
+% \begin{align}
+% \hat{\bm \Sigma}_{\text{LR}} = \tilde{\bm \Sigma} \tilde{\bm \Sigma}\T + \bm \sigma_{\text{diag}} \nonumber \\
+% \bm \Sigma_{\text{LR}} = \min_{\hat{\bm \Sigma}_{\text{LR}}} \norm{\bm \Sigma - \hat{\bm \Sigma}_{\text{LR}}}_F \label{eq:lowrank-covariance}
+% \end{align}
+
+% \subsection{Radial Variational Approximation}
+
+% \textcite{farquhar2020radial} introduces the Radial approximate posterior,
+% For each NN layer, this posterior is sampled similarly to the mean-field (\cref{eq:meanfield}), but by normalising the \(\bm{\epsilon}\) term (projecting it onto a hypersphere) yielding a direction term \(\bm{\epsilon}/\norm{\bm{\epsilon}}\), and scaling it by the distance \(r\).
+% Therefore, for each layer, the radial posterior is sampled as
+% \begin{align}\label{eq:radial}
+% q(\bm \theta) ={} & \mathrm{Radial}(\bm{\mu}, \bm{\sigma}) \nonumber \\
+% \bm{\theta} ={} & \bm{\mu} + \bm{\sigma} \circ \frac{\bm{\epsilon}}{\norm{\bm{\epsilon}}} r
+% \end{align}
+% where \(\bm{\epsilon} \sim \normal(\bm{0}, \identity)\), \(r \sim \normal(0, 1)\).
+% The entropy term of the Radial approximate posterior is given
+% \(\entropy{q(\theta)} = - \sum_i \log \sigma_i + \mathrm{const}\) (and is therefore approximately equal to the entropy of the mean-field approximation up to a constant) and the cross-entropy term is calculated via Monte Carlo simulation as for mean-field VI (\cref{ssec:mfvi}), but by sampling from a radial posterior \(\bm{\theta}^{(s)} \sim \mathrm{Radial}(\bm{\mu}, \bm{\sigma})\) as in \cref{eq:radial}.
+
+% \subsection{Deep Ensembles}
+
+% In deep ensembles~\cite{lakshminarayanan2017simple}, \(M\) neural networks are trained with different initialisations.
+% In this way, multiple local MAP solutions \(\bm\theta^{(m)}\) are obtained and equally considered, such that \(p(\bm \theta = \bm \theta^{(m)} \given \bm y) = 1 / M\) for every \(\bm \theta^{(m)} \in \{\bm\theta^{(1)}, \ldots, \bm\theta^{(M)}\}\) (and 0 elsewhere).
+% To make predictions with a deep ensemble, we use the posterior predictive given by
+% \begin{align}
+% p(\bm y_{\mathrm{new}} \given \bm y)
+% ={} & \int p(\bm y_{\mathrm{new}} \given \bm \theta) p(\bm \theta \given \bm y)\,d\bm\theta \nonumber \\
+% ={} & \frac{1}{M} \sum_{m=1}^M p(\bm y_{\mathrm{new}} \given \bm\theta^{(m)})
+% \end{align}
+% where \(p(\bm y_{\mathrm{new}} \given \bm\theta^{(m)})\) are the normalised logits, or predicted probabilities, from model \(m\) in the ensemble.
+
+% \subsection{MultiSWAG}
+
+% Stochastic weight averaging estimates the final point estimate of the weights of a neural network \(\bm \theta_\mathrm{SWA}\) as the average of the model parameters \(\bm \theta\) at the end of each epoch obtained via gradient descent after convergence has been reached.
+
+% For SWAG~\cite{maddox2019simple}, the standard deviation of the model parameters \(\bm \sigma_\mathrm{SWA}\) is also calculated.
+% From the SWA estimates of the model parameter mean and standard deviations, the posterior is then estimated as
+% \begin{align}\label{eq:swag}
+% p(\bm \theta \given \bm y) \approx{} & \normal(\bm \theta_\mathrm{SWA}, \bm \sigma_\mathrm{SWA}).
+% \end{align}
+
+% For MultiSWAG~\cite{wilson2020bayesian}, \(M\) SWAG estimates are obtained from different initialisations, as in deep ensembles.
+% Then, a Gaussian mixture model (GMM) is formed as a combination of each Gaussian SWAG posterior estimate \(\normal(\bm \theta_\mathrm{SWA}^{(m)}, \bm \sigma_\mathrm{SWA}^{(m)})\) where each component is equally weighed, as
+% \begin{align}\label{eq:multiswag}
+% p(\bm \theta \given \bm y) \approx{} & \frac{1}{M} \sum_{m=1}^M \normal(\bm \theta_\mathrm{SWA}^{(m)}, \bm \sigma_\mathrm{SWA}^{(m)}).
+% \end{align}
+
+% % \begin{adjustwidth*}{0cm}{-0.4cm}
+% % \begin{lstlisting}[language=Python,caption=Fibonacci2,label=Fibonacci2]
+% % # This is a comment
+% % import easy
+% % str = "I am a string"
+% % def fib(n):
+% % if n == 0:
+% % return 0
+% % elif n == 1:
+% % return 1
+% % else:
+% % return fib(n-1) + fib(n-2)
+% % str5 = "It adjusts according to the spine"
+% % \end{lstlisting}
+% % \end{adjustwidth*}
diff --git a/chapters/mixture.tex b/chapters/mixture.tex
@@ -0,0 +1,31 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Future Work: Mixture of Laplace Approximations}
+\labch{mixture}
+
+We could then frame the Laplace posterior as a mixture of per-sample Laplace approximations as
+\begin{equation}
+ p(\bm \theta \given \bm x) = \sum^N_i w_i \normal(\bm \mu_i, \bm \Sigma_i)
+\end{equation}
+such that \(\sum^N_i w_i \bm \mu_i = \bm \mu_{\textsc{map}}\) and \(\sum_i^N w_i = 1\).
+Sampling from this model would then involve sampling an index
+\begin{align}
+ i \sim{}& \mathrm{Cat}(N, \bm w)
+\\ \bm \epsilon \sim{}& \normal(\bm \mu_i, \bm \Sigma_i)
+\end{align}
+where \(\bm \mu_i\) is computed by shifting the MAP solution based on ??? and \(\bm \Sigma_i\) is computed as the inverse precision matrix from the Laplace approximation, as per \cref{eq:shifted-map}.
+\begin{align}\label{eq:shifted-map}
+ \bm \mu_i ={}& \bm \mu_{\textsc{map}} - \nabla_{\bm\theta} F_n (\bm \theta) \nabla_{\bm\theta}^2 F_n (\bm \theta)
+\\ \bm \Sigma^{-1}_i ={}& - \nabla^2_{\bm \theta} \log p(\bm \theta \given x_i)
+\end{align}
+The reparameterisation trick can then be performed using only a single observation to determine the covariance matrix
+\begin{align}
+ \bm \epsilon ={}& \bm \mu_i + \bm \Sigma^{-1/2}_i \bm \epsilon_0,
+\end{align}
+where, since \(\bm \Sigma^{-1}_i = J_i\T H_i J_i + \alpha / N \identity\) is low-rank with rank \(o\), the Woodbury identity from \cref{sec:woodbury} can be used.
+This method depends on the conditioning number of the outer product \(J H J\T\), which is significantly lower than that of the inner product \(J\T H J\).
+
+However, this method poses some major issues when it comes to computing the evidence of the model.
+\begin{align}
+ p(\bm x) ={}& \int_{\bm \theta} p(\bm x \given \bm \theta) p(\bm \theta)\,d \bm \theta
+\\ ={}& \prod^N_n \exp F_n (\bm \theta_\textsc{map}) \int_{\bm \theta} \exp\left\{ -\frac{1}{2} (\bm\theta - \bm\theta_\textsc{map} )\T \Lambda_n (\bm\theta - \bm\theta_\textsc{map} )\,d\bm\theta \right\}
+\end{align}
diff --git a/chapters/preface.tex b/chapters/preface.tex
@@ -0,0 +1,15 @@
+\chapter*{Preface}
+\addcontentsline{toc}{chapter}{Preface} % Add the preface to the table of contents as a chapter
+
+\blackout
+{
+I've been studying for a pretty long time.
+I suppose that, because of this, there is some kind of expectation of anticipation, of the sensation that a culmination/apogee of a.
+I, not always being the biggest adherent to the idea of symbol-for-the-sake-of-symbol, wasn't sure what to expect of this experience
+
+The work in this thesis is the product of a five-month-long research project conducted in the Section for Cognitive Systems of DTU Compute, during which I helped develop some of the ideas in the paper ``Large-Scale Laplace''(cite).
+}
+
+\begin{flushright}
+ \textit{Silas Brack}
+\end{flushright}
diff --git a/chapters/references.tex b/chapters/references.tex
@@ -0,0 +1,322 @@
+\setchapterstyle{kao}
+%\setchapterpreamble[u]{\margintoc}
+\chapter{References}
+\labch{references}
+
+\section{Citations}
+
+\index{citations}
+To cite someone \sidecite{Visscher2008,James2013} is very simple: just
+use the \Command{sidecite}\index{\Command{sidecite}} command. It does
+not have an offset argument yet, but it probably will in the future.
+This command supports multiple entries, as you can see, and by default
+it prints the reference on the margin as well as adding it to the
+bibliography at the end of the document. Note that the citations have
+nothing to do with the text,\sidecite{James2013} but they are completely
+random as they only serve the purpose to illustrate the feature.
+
+For this setup I wrote a separate package, \Package{kaobiblio}, which
+you can find in the \Package{styles} directory and include in your main
+tex file. This package accepts all the options that you can pass to
+\Package{biblatex}, and actually it passes them to \Package{biblatex}
+under the hood. Moreover, it also defines some commands, like
+\Command{sidecite}, and environments that can be used within a
+\Class{kao} book.\sidenote[][-.9cm]{For this reason you should always
+use \Package{kaobiblio} instead of \Package{biblatex}, but the syntax
+and the options are exactly the same.}
+
+If you want to use \Package{bibtex} instead of \Package{biblatex},
+pass the option \Option{backend=bibtex} to \Package{kaobiblio}.
+\Package{kaobiblio} also supports two options that are not shared with
+\Package{biblatex}: \Option{addspace} and \Option{linkeverything},
+both of which are boolean options, meaning that they can take
+either \enquote{true} or \enquote{false} as a value. If you
+pass \Option{addspace=true} when loading \Package{kaobiblio},
+a space will be automatically added before the citation marks.
+If you pass \Option{linkeverything=true}, the author's name in
+the authoryear-* and authortitle-* styles will be a hyperlink
+like the year.\sidenote{The fact that the author name is not
+a hyperlink bothers more than one biblatex user. There are
+\href{https://github.com/plk/biblatex/issues/428}{strong arguments}
+\emph{against} hyperlinking the author name, but in my personal opinion,
+linking the author's name does not result in any problems in most
+practical cases.}
+
+As you have seen, the \Command{sidecite} command will print a citation
+in the margin. However, this command would be useless without a way to
+customise the format of the citation, so the \Class{kaobook} provides
+also the \Command{formatmargincitation} command. By \enquote{renewing}
+that command, you can choose which items will be printed in the margins.
+The best way to understand how it works is to see the actual definition
+of this command.
+
+\begin{lstlisting}[style=kaolstplain,linewidth=1.5\textwidth]
+\newcommand{\formatmargincitation}[1]{%
+ \parencite{#1}: \citeauthor*{#1} (\citeyear{#1}), \citetitle{#1}%
+}
+\end{lstlisting}
+
+Thus, the \Command{formatmargincitation} accepts one parameter, which is
+the citation key, and prints the parencite followed by a colon, then the
+author, then the year (in brackets), and finally the
+title.\sidecite{Battle2014} Now, suppose that you wish the margin
+citation to display the year and the author, followed by the title, and
+finally a fixed arbitrary string; you would add to your document:
+
+\begin{lstlisting}[style=kaolstplain,linewidth=1.5\textwidth]
+\renewcommand{\formatmargincitation}[1]{%
+ \citeyear{#1}, \citeauthor*{#1}: \citetitle{#1}; very interesting!%
+}
+\end{lstlisting}
+
+\renewcommand{\formatmargincitation}[1]{%
+ \citeyear{#1}, \citeauthor*{#1}: \citetitle{#1}; very interesting!%
+}
+
+The above code results in citations that look like the
+following.\sidecite{Zou2005} Of course, changing the format is most
+useful when you also change the default bibliography style. For
+instance, if you want to use the \enquote{philosophy-modern} style for
+your bibliography, you might have something like this in the preamble:
+
+\begin{lstlisting}[style=kaolstplain,linewidth=1.5\textwidth]
+\usepackage[style=philosophy-modern]{styles/kaobiblio}
+\renewcommand{\formatmargincitation}[1]{%
+ \sdcite{#1}%
+}
+\addbibresource{main.bib}
+\end{lstlisting}
+
+\renewcommand{\formatmargincitation}[1]{%
+ \parencite{#1}: \citeauthor*{#1} (\citeyear{#1}), \citetitle{#1}%
+}
+
+The commands like \Command{citeyear}, \Command{parencite}
+and \Command{sdcite} are just examples. A full
+reference of the available commands can be found in this
+\href{http://tug.ctan.org/info/biblatex-cheatsheet/biblatex-cheatsheet.pdf}{cheatsheet},
+under the \enquote{Citations} section.
+
+Finally, to compile a document containing citations, you need to use an
+external tool, which for this class is biber. You need to run the
+following (assuming that your tex file is called main.tex):
+
+\begin{lstlisting}[style=kaolstplain]
+$ pdflatex main
+$ biber main
+$ pdflatex main
+\end{lstlisting}
+
+\section{Glossaries and Indices}
+
+\index{glossary}
+The \Class{kaobook} class loads the packages \Package{glossaries} and
+\Package{imakeidx}, with which you can add glossaries and indices to
+your book. For instance, I previously defined some glossary entries and
+now I am going to use them, like this: \gls{computer}.
+\Package{glossaries} also allows you to use acronyms, like the
+following: this is the full version, \acrfull{fpsLabel}, and this is the
+short one \acrshort{fpsLabel}. These entries will appear in the glossary
+in the backmatter.
+
+Unless you use \href{https://www.overleaf.com}{Overleaf} or some other
+fancy IDE for \LaTeX, you need to run an external command from your
+terminal in order to compile a document with a glossary. In particular,
+the commands required are:\sidenote{These are the commands you would run
+in a UNIX system, but see also \nrefsec{compiling}; I have no idea about
+how it works in Windows.}
+
+\begin{lstlisting}[style=kaolstplain]
+$ pdflatex main
+$ makeglossaries main
+$ pdflatex main
+\end{lstlisting}
+
+Note that you need not run \texttt{makeglossaries} every time you
+compile your document, but only when you change the glossary entries.
+
+\index{index}
+To create an index, you need to insert the command
+\lstinline|\index{subject}| whenever you are talking about
+\enquote{subject} in the text. For instance, at the start of this
+paragraph I would write \lstinline|index{index}|, and an entry would be
+added to the Index in the backmatter. Check it out!
+
+\marginnote[2mm]{In theory, you would need to run an external command
+for the index as well, but luckily the package we suggested,
+ \Package{imakeidx}, can compile the index automatically.}
+
+\index{nomenclature}
+A nomenclature is just a special kind of index; you can find one at the end of
+this book. To insert a nomenclature, we use the package \Package{nomencl} and
+add the terms with the command \Command{nomenclature}. We put then a
+\Command{printnomenclature} where we want it to appear.
+
+Also with this package we need to run an external command to compile the
+document, otherwise the nomenclature will not appear:
+
+\begin{lstlisting}[style=kaolstplain]
+$ pdflatex main
+$ makeindex main.nlo -s nomencl.ist -o main.nls
+$ pdflatex main
+\end{lstlisting}
+
+These packages are all loaded in
+\href{style/packages.sty}{packages.sty}, one of the files that come with
+this class. However, the configuration of the elements is best done in
+the main.tex file, since each book will have different entries and
+styles.
+
+Note that the \Package{nomencl} package caused problems when the
+document was compiled, so, to make a long story short, I had to prevent
+\Package{scrhack} to load the hack-file for \Package{nomencl}. When
+compiling the document on Overleaf, however, this problem seem to
+vanish.
+
+\marginnote[-19mm]{This brief section was by no means a complete
+reference on the subject, therefore you should consult the documentation
+of the above package to gain a full understanding of how they work.}
+
+\section{Hyperreferences}
+\labsec{hyprefs}
+
+\index{hyperreferences}
+Together with this class we provide a handy package to help you
+referencing the same elements always in the same way, for consistency
+across the book. First, you can label each element with a specific
+command. For instance, should you want to label a chapter, you would put
+\lstinline|\labch{chapter-title}| right after the \Command{chapter}
+directive. This is just a convenience, because \Command{labch} is
+actually just an alias to \lstinline|\label{ch:chapter-title}|, so it
+spares you the writing of \enquote{ch:}. We defined similar commands for
+many typically labeled elements, including:
+
+\begin{multicols}{2}
+\setlength{\columnseprule}{0pt}
+\begin{itemize}
+ \item Page: \Command{labpage}
+ \item Part: \Command{labpart}
+ \item Chapter: \Command{labch}
+ \item Section: \Command{labsec}
+ \item Figure: \Command{labfig}
+ \item Table: \Command{labtab}
+ \item Definition: \Command{labdef}
+ \item Assumption: \Command{labassum}
+ \item Theorem: \Command{labthm}
+ \item Proposition: \Command{labprop}
+ \item Lemma: \Command{lablemma}
+ \item Remark: \Command{labremark}
+ \item Example: \Command{labexample}
+ \item Exercise: \Command{labexercise}
+\end{itemize}
+\end{multicols}
+
+Of course, we have similar commands for referencing those elements.
+However, since the style of the reference should depend on the context,
+we provide different commands to reference the same thing. For instance,
+in some occasions you may want to reference the chapter by name, but
+other times you want to reference it only by number. In general, there
+are four reference style, which we call plain, vario, name, and full.
+
+The plain style references only by number. It is accessed, for chapters,
+with \lstinline|\refch{chapter-title}| (for other elements, the syntax
+is analogous). Such a reference results in: \refch{references}.
+
+The vario and name styles rest upon the \Package{varioref} package.
+Their syntax is \lstinline|\vrefch{chapter-title}| and
+\lstinline|\nrefch{chapter-title}|, and they result in:
+\vrefch{references}, for the vario style, and: \nrefch{references}, for
+the name style. As you can see, the page is referenced in
+\Package{varioref} style.
+
+The full style references everything. You can use it with
+\lstinline|\frefch{chapter-title}| and it looks like this:
+\frefch{references}.
+
+Of course, all the other elements have similar commands (\eg for parts
+you would use \lstinline|\vrefpart{part-title}| or something like that).
+However, not all elements implement all the four styles. The commands
+provided should be enough, but if you want to see what is available or
+to add the missing ones, have a look at the
+\href{styles/kaorefs.sty}{attached package}.
+
+In order to have access to all these features, the \Package{kaorefs}
+should be loaded in the preamble of your document. It should be loaded
+last, or at least after \Package{babel} (or \Package{polyglossia}) and
+\Package{plaintheorems} (or \Package{mdftheorems}). Options can be
+passed to it like to any other package; in particular, it is possible to
+specify the language of the captions. For instance, if you specify
+\enquote{italian} as an option, instead of \enquote{Chapter} it will be
+printed \enquote{Capitolo}, the Italian analog. If you know other
+languages, you are welcome to contribute the translations of these
+captions! Feel free to contact the author of the class for further
+details.
+
+The \Package{kaorefs} package also include \Package{cleveref}, so it is
+possible to use \Command{cref} in addition to all the previously
+described referencing commands.
+
+\section{A Final Note on Compilation}
+\labsec{compiling}
+
+Probably the easiest way to compile a latex document is with the
+\Package{latexmk} script, as it can take care of everything, if properly
+configured, from the bibliography to the glossary. The command to issue,
+in general, is:
+
+\begin{lstlisting}
+latexmk [latexmk_options] [filename ...]
+\end{lstlisting}
+
+\Package{latexmk} can be extensively configured (see
+\url{https://mg.readthedocs.io/latexmk.html}). For convenience, I print
+here an example configuration that would cover all the steps described
+above.
+
+\begin{lstlisting}
+# By default compile only the file called 'main.tex'
+@default_files = ('main.tex');
+
+# Compile the glossary and acronyms list (package 'glossaries')
+add_cus_dep( 'acn', 'acr', 0, 'makeglossaries' );
+add_cus_dep( 'glo', 'gls', 0, 'makeglossaries' );
+$clean_ext .= " acr acn alg glo gls glg";
+sub makeglossaries {
+ my ($base_name, $path) = fileparse( $_[0] );
+ pushd $path;
+ my $return = system "makeglossaries", $base_name;
+ popd;
+ return $return;
+}
+
+# Compile the nomenclature (package 'nomencl')
+add_cus_dep( 'nlo', 'nls', 0, 'makenlo2nls' );
+sub makenlo2nls {
+ system( "makeindex -s nomencl.ist -o \"$_[0].nls\" \"$_[0].nlo\"" );
+}
+\end{lstlisting}
+
+However, if you'd rather not use an external package and want to do
+everything manually, here are some tips.\sidenote{As the author only
+uses Linux and compiles everything from the command line, he doesn't
+know how the compilation works in Windows or Mac. The tips, therefore,
+refer to the usage with Linux from the command line.}
+
+\minisec{Compiling the examples in the kaobook repository}
+To compile the examples, and in particular the documentation, that are
+in the \Path{examples} directory of the
+\href{https://github.com/fmarotta/kaobook}{kaobook repository} on
+GitHub, do as follows. \lstinline[language=bash]|cd| into the root
+directory of the repository, and run
+\lstinline|pdflatex -output-directory examples/documentation main.tex|.
+With this trick, you can compile the documentation using the class files
+pertaining to the repository (and not, say, those in your texmf tree).
+The \enquote{-output-directory} option works with the other
+\LaTeX-related commands such as biber and makeglossaries.
+
+A note of warning: sometimes \LaTeX\ needs more than one run to get the
+correct position of each element; this is true in particular for the
+positioning of floating elements like figures, tables, and margin notes.
+Occasionally, \LaTeX\ can need up to four re-runs, so If the alignment
+of margin elements looks odd, or if they bleed into ther main text, try
+runnign pdflatex one more time.
diff --git a/chapters/results.tex b/chapters/results.tex
@@ -0,0 +1,5 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Results}
+\labch{results}
+
+
diff --git a/chapters/sampling.tex b/chapters/sampling.tex
@@ -0,0 +1,382 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Sampling}
+\labch{sampling}
+
+\section{Introduction}
+
+In many methods, such as the Laplace approximation (\cref{sec:laplace}), we need to sample from a multivariate normal distribution parameterised by a mean vector \(\bm \mu\) and a covariance matrix \(\bm \Sigma\).
+To sample from a Gaussian distribution, you can apply the \emph{reparameterisation trick} to compute a vector of samples as
+\begin{align}\label{eq:sample-reparam}
+ \bm \epsilon ={}& \bm \mu + \bm \Sigma^{1/2} \bm \epsilon_0,
+\end{align}
+where \(\bm \epsilon_0\) are samples drawn from a standard normal distribution, i.e., \(\bm \epsilon_0 \sim \normal(\bm 0, \identity)\).
+
+Using the Laplace approximation, we estimate the precision matrix as the Hessian matrix of the loss with respect to the parameters, as discussed in \cref{sec:laplace}.
+Because of this, the covariance matrix is given by the inverse of this Hessian and we thus sample as per
+\begin{align}\label{eq:sample-laplace}
+ \bm \epsilon = \bm \mu + \bm \Lambda^{-1/2} \bm \epsilon_0,
+\end{align}
+where \(\Lambda = \nabla^2_{\bm\theta} \log p(\bm \theta \given \bm x)\).
+In \cref{eq:sample-reparam}, the square root of a matrix yields another matrix which, when multiplied by itself, is equal to the original matrix.
+However, since a Gaussian distribution is invariant to rotation\sidenote{That is, if we have a vector of standard normal samples \(\bm \epsilon \sim \normal(\bm 0, \identity)\), then, for an orthogonal matrix \(\bm A\) (a rotation matrix), \(\bm A\T \bm \epsilon \sim \normal(\bm 0, \identity)\). We then have that we can sample \emph{non} standard normal samples as \(\bm \Lambda^{-1/2} \bm A\T \bm \epsilon \sim \normal(\bm 0, \identity)\).}, in this special case, we simply need to obtain a matrix which returns the original matrix \emph{up to a rotation}.
+We thus need to find a method which computes the inverse square root of a high-dimensional matrix up to a rotation.
+
+\section{Contour Integral Quadrature}
+
+\textcite{pleiss2020fast} propose one such algorithm.
+Contour integral quadrature (CIQ) attempts to solve the problem of computing the inverse-square root of a matrix, up to a rotation, by approximating the expression \(f(\bm K) \bm \epsilon_0 = \bm K^{-1/2} \bm \epsilon_0\) where \(\bm K\) is a positive semi-definite matrix using Cauchy's integral formula.
+To do this, it exploits the Lanczos algorithm, Gaussian quadrature, and the msMINRES algorithm, methods which only require the computation of matrix-vector products \(\bm K \bm v\).
+There are then essentially three steps to CIQ, namely:
+\begin{enumerate}
+ \item Perform the Lanczos algorithm to compute a lower bound on the maximum eigenvalue and an upper bound on the minimum eigenvalue of \(\bm K\).
+ \item Sample \(Q\) quadrature points from a complex-plane circle to approximate the contour integral from Cauchy's integral formula, using these quadrature points to create \(Q\) shifted linear systems.
+ \item Solve these \(Q\) shifted systems using the msMINRES algorithm.
+\end{enumerate}
+
+\begin{algorithm}
+\caption{Contour Integral Quad (CIQ)}\label{alg:ciq}
+\Input{$\bm K \succ 0$, $\bm b$, $\bm P \succ 0$, $J > 0$, $Q > 0$}
+\Output{$\bm s = \bm K^{-1/2} b$}
+$\_, \bm\alpha, \bm\beta \gets \mathrm{CG}(\bm K, \bm b, \bm P, J)$\;
+$M_{\mathrm{Lanczos}} \gets \mathrm{TriDiag}(\bm\alpha, \bm\beta)$\;
+$\lambda_M \gets \lambda(M_{\mathrm{Lanczos}})$\;
+$\lambda_{\min} \gets \min{\lambda_M}$\;
+$\lambda_{\max} \gets \max{\lambda_M}$\;
+$w_1, \ldots, w_Q, t_1, \ldots, t_Q \gets \mathrm{Quadrature}(\lambda_{\min}, \lambda_{\max}, Q)$\;
+$\bm c_1, \ldots, \bm c_Q \gets \mathrm{msMINRES}(-\bm K, \bm b, t_q, \bm P, J)$ \Comment*[r]{$\left( t_q \identity - \bm K \right)^{-1} \bm b$.}
+% $s_q \gets \left( t_q \identity + H \right)^{-1} b$ \Comment*[r]{Computed via msMINRES(K, t, J).}
+$\bm s \gets \sum^Q_{q=1} w_q \bm c_q$\;
+% $\mathrm{samples} \gets \sum^Q_{q=1} w_q \left( t_q \identity + H \right)^{-1} b$
+\end{algorithm}
+
+This procedure (see \cref{alg:ciq}) computes the approximate inverse square root product \(\bm K\) by \(\bm \epsilon_0\) up to a rotation, i.e.,
+\begin{align}\label{eq:ciq-asdf}
+ \bm K^{-1/2} \bm \epsilon_0.
+\end{align}
+From here, it is then trivial to also compute the product of a matrix square root by multiplying \(\bm K\) onto this vector, as per
+\begin{align}
+ \bm K^{1/2} \bm \epsilon_0 = \bm K \cdot \underbrace{\bm K^{-1/2} \bm \epsilon_0}_{\text{\cref{eq:ciq-asdf}}}.
+\end{align}
+
+\def\ymax{2.0}
+\def\ymin{-2.0}
+\def\xmax{3.0}
+\def\xmin{-0.5}
+\def\R{1.3}
+\begin{marginfigure}[-7cm]
+\centering
+\input{figures/cauchy_integral.tex}
+\caption{Representation of integration circle \(\Gamma\) contained within the domain of \(f\) as defined according to the Cauchy integral formula.}
+\label{fig:cauchy-integral-formula}
+\end{marginfigure}
+
+% ------------------------------------------
+% CAUCHY INTEGRAL FORMULA
+% ------------------------------------------
+
+The first step therefore invokes Cauchy's integral formula.
+Cauchy's integral formula is a central theorem in complex analysis which states that, for a holomorphic function \(f\)\marginnote{Note that the square root function is holomorphic for all positive definite real matrices.}, for a closed circular contour \(\Gamma\) in the complex plane (see \cref{fig:cauchy-integral-formula}) which encloses the eigenvalues of \(\bm K\), \(f(\bm K)\) can be approximated as
+\begin{align}\label{eq:cauchy-integral-formula}
+ f(\bm K) ={}& \frac{1}{2 \pi i} \oint_\Gamma f(\tau) \left(\tau \identity - \bm K\right)^{-1}\,d\tau.
+\end{align}
+Since, in our case, the eigenvalues of \(\bm K\) will all be real-valued\marginnote{If a real-valued matrix is symmetric, its eigenvalues are real. In our case, \(\bm K\) will be the precision matrix obtained from the Laplace approximation.}, a circle centred on the real axis which encloses the minimum and maximum eigenvalues of \(\bm K\) will suffice.
+
+% ------------------------------------------
+% QUADRATURE
+% ------------------------------------------
+
+We can then apply this to the function \(f(\bm K) = \bm K^{-1/2}\), apply a change of variable, and then approximate the integral using the quadrature rule with \(Q\) quadrature points, as per
+\begin{align}\label{eq:ciq}
+ \bm K^{-1/2} ={}& \frac{1}{2 \pi i} \oint_\Gamma \tau^{-1/2} \left(\tau \identity - \bm K\right)^{-1}\,d\tau \nonumber
+\\ ={}& \frac{1}{\pi i} \oint_{\Gamma_\sigma} \left( \sigma^2 \identity - \bm K \right)^{-1}\,d\sigma \nonumber
+\\ \approx{}& \frac{1}{\pi i} \sum_{q=1}^Q w_q \left( \sigma^2_q \identity - \bm K \right)^{-1} \nonumber
+\\ \bm K^{-1/2} \bm v \approx{}& \frac{1}{\pi i} \sum_{q=1}^Q w_q \underbrace{\left( \sigma^2_q \identity - \bm K \right)^{-1} \bm v}_{Q\text{ system solves}},
+\end{align}
+where \(\sigma\) is obtained from \(\tau\) as a change of variable and \(w_1, \ldots, w_Q\) are the \(Q\) quadrature weights.
+We can then solve the \(Q\) systems of equations \(\left( \sigma^2_1 \identity - \bm K \right) = \bm v, \ldots, \left( \sigma^2_Q \identity - \bm K \right) = \bm v\) to obtain the solutions \(\bm c_q = \left(\sigma^2_q \identity - \bm K \right)^{-1} \bm v\).
+Note that, since the integrand is symmetric with respect to the real axis\sidenote{This is due to the fact that the circle with its centre on the \(x\)-axis is symmetric with respect to said \(x\)-axis.}, only the imaginary component of \(\Gamma_\sigma\) is required.
+Since \(\sigma\) is then imaginary (its real part is zero), \(\tau := \sigma^2\) is real such that \(\tau = - \Im(\sigma)^2\).
+As such, since \(\Im(\sigma)^2\) must be positive, \(\tau\) must then be real-valued and negative.
+And since, as mentioned before, the integrand is symmetric with respect to the real axis, we can ?????
+In practice, we incorporate the \(1 / (\pi i) \) term into the quadrature weights and, with the quadrature points \(\sigma^2_1, \ldots, \sigma^2_Q\), compute \(\bm K^{-1/2} \bm v \approx \sum^Q_{q=1} w_q \bm c_q\), where the solutions \(\bm c_1, \ldots \bm c_Q\) are computed using msMINRES (\cref{ssec:msminres}).
+
+Since we will be sampling a fixed number of quadrature points from the circle, to guarantee the closeness of the approximation of the integral, it would be beneficial to choose a closed circle which intersects the real axis \emph{at} the minimum and maximum eigenvalues, since this will avoid sampling quadrature points which lie outside of the spectrum of \(\bm K\).
+
+\def\ymax{2.0}
+\def\ymin{-2.0}
+\def\xmax{3.0}
+\def\xmin{-0.5}
+\def\R{1.3}
+\begin{marginfigure}
+\centering
+\input{figures/cauchy_quadrature.tex}
+\caption{The shifts \(\tau_1, \ldots, \tau_Q\) are sampled from a circle with its centre on the real axis which intersects the real axis at the points \(\lambda_{\mathrm{min}}\) and \(\lambda_{\mathrm{max}}\). Since we use Jacobi elliptic functions, we over-sample points that are closer to the minimum eigenvalue \(\lambda_{\mathrm{min}}\).}
+\label{fig:ciq}
+\end{marginfigure}
+
+% ------------------------------------------
+% JACOBI ELLIPTICAL FUNCTIONS
+% ------------------------------------------
+
+We could sample these quadrature points uniformly, but...
+Instead, we use Jacobi elliptic functions (\cref{sec:jacobi-elliptic})
+With these functions, we can compute the weights for Gaussian quadrature as
+\begin{align}
+ w_q = - \frac{2 \sqrt{\lambda_\mathrm{min}}}{\pi Q} \mathcal{K}^\prime (k) \cn(i u_q \mathcal{K}^\prime (k) \given k) \dn (i u_q \mathcal{K}^\prime (k) \given k)
+\end{align}
+and shifts as
+\begin{align}
+ \sigma^2_q = \lambda_{\mathrm{min}} \cdot \sn(i u_q \mathcal{K}^\prime (k) \given k)^2
+\end{align}
+
+\cref{alg:quad} describes .
+
+\begin{marginfigure}
+\begin{algorithm}[H]
+\caption{Quadrature}\label{alg:quad}
+\Input{$\lambda_{\min}$, $\lambda_{\max}$, $Q > 0$}
+\Output{$w_q, t_q \given q \in 1, \ldots, Q$}
+$k^2 \gets \lambda_{\min} / \lambda_{\max}$\;
+${k^{\prime}}^2 \gets \sqrt{1 - k^2}$\;
+$K^\prime \gets \mathcal{K}({k^{\prime}}^2)$\;
+\For{$q \gets 1$ \KwTo $Q$}{
+ $u_q \gets (q - 1/2) / Q$\;
+ $\overline{\sn}_q \gets \sn(u_q K^\prime \given {k^{\prime}}^2)$\;
+ $\overline{\cn}_q \gets \cn(u_q K^\prime \given {k^{\prime}}^2)$\;
+ $\overline{\dn}_q \gets \dn(u_q K^\prime \given {k^{\prime}}^2)$\;
+ $\sn_q \gets i ( \overline{\sn}_q / \overline{\cn}_q )$\;
+ $\dn_q \gets ( \overline{\dn}_q / \overline{\cn}_q )$\;
+ $\cn_q \gets ( 1 / \overline{\cn}_q )$\;
+ $w_q \gets \lambda_{\min}^{1/2} K^\prime \cn_q \dn_q$\;
+ $w_q \gets -2 w_q / \left(\pi Q\right) $\;
+ $t_q \gets \lambda_{\min} \sn_q^2$\;
+}
+\end{algorithm}
+\end{marginfigure}
+
+% \begin{algorithm}
+% \caption{Quadrature}\label{alg:quad}
+% \Input{$\lambda_{\min}$, $\lambda_{\max}$, $Q > 0$}
+% \Output{$w_1, \ldots, w_Q, t_1, \ldots, t_Q$}
+% $k^2 \gets \lambda_{\min} / \lambda_{\max}$\;
+% ${k^{\prime}}^2 \gets \sqrt{1 - k^2}$\;
+% $K^\prime \gets \mathcal{K}({k^{\prime}}^2)$ \Comment*[r]{Computed using ellipk}
+% \For{$q \gets 1$ \KwTo $Q$}{
+% $u_q \gets (q - 1/2) / Q$\;
+% $\overline{\sn}_q \gets \sn(u_q K^\prime \given {k^{\prime}}^2)$ \Comment*[r]{Computed using ellipj}
+% $\overline{\cn}_q \gets \cn(u_q K^\prime \given {k^{\prime}}^2)$ \Comment*[r]{Computed using ellipj}
+% $\overline{\dn}_q \gets \dn(u_q K^\prime \given {k^{\prime}}^2)$ \Comment*[r]{Computed using ellipj}
+% $\sn_q \gets i ( \overline{\sn}_q / \overline{\cn}_q )$\;
+% $\dn_q \gets ( \overline{\dn}_q / \overline{\cn}_q )$\;
+% $\cn_q \gets ( 1 / \overline{\cn}_q )$\;
+% $w_q \gets \left(-2 \lambda_{\min}^{1/2}\right) / \left(\pi Q\right) K^\prime \cn_q \dn_q$\;
+% $t_q \gets \lambda_{\min} \sn_q^2$\;
+% }
+% \end{algorithm}
+
+\section{Preconditioning}
+
+One drawback of contour integral quadrature if that the accuracy its of approximation of \(\bm \Lambda^{-1/2} \bm \epsilon_0\) suffers when the precision matrix \(\bm \Lambda\) is poorly conditioned\sidenote{Recall that a matrix is considered poorly conditioned when the ratio of its largest eigenvalue to its lowest eigenvalue, its \emph{conditioning number} (\(\kappa := \lambda_{\mathrm{max}} / \lambda_{\mathrm{min}}\)), is very large.}.
+
+In the case of the Laplace approximation, as per \cref{eq:ggn-laplace}, we have that the precision matrix is given by
+\begin{align}\label{eq:ggn-laplace}
+ \bm \Lambda = \sum_i^N \bm J_i\T \bm H \bm J_i + \alpha \identity,
+\\ \bm \theta \sim \normal(\bm \mu, \bm \Lambda^{-1})
+\end{align}
+where \(N\) is the number of observations in your dataset.
+Since each \(J_i\T H J_i\) is, for \(i \in 1 \ldots N\), a low-rank\sidenote{With rank \(o\), where \(o\) is the number of neural network outputs.} positive semi-definite matrix, it has \(o\) non-zero eigenvalues.
+Let us define \(\lambda_1, \ldots, \lambda_o\) as the eigenvalues of a matrix sorted in decreasing order.
+We can then
+Assume that the non-zero spectrum of \(J_i\T H J_i\) is relatively flat, i.e., \(\lambda_1 \not\gg \lambda_o\).
+
+In a hand-wavy way, we have that the ``more similar'' the eigenvectors (and assuming that the eigenvectors have the same ordering between the two matrices)
+
+For preconditioning, we need to find a preconditioner matrix \(P\) and its inverse \(P^{-1}\) such that
+\begin{align}
+ P^{-1} A \approx{}& \identity \Rightarrow P \approx A
+\\ P^{-1} \Sigma^{-1} ={}& P^{-1} \left( \textstyle{\sum^N_i} J_i\T H J_i + \alpha \identity \right)
+\end{align}
+where \(\Sigma^{-1}\) is our precision matrix, approximated by a GGN Hessian.
+
+The eigenvalues of our GGN approximation are given by
+\begin{align}
+ \lambda\left( \textstyle{\sum^N_i} J_i\T H J_i + \alpha \identity \right) = \big\{ \underbrace{s_1, \ldots, s_p}_{\leq N \cdot k}, \underbrace{\alpha, \ldots, \alpha}_{\geq n - N \cdot k} \big\}
+\end{align}
+where \(\{s_1, \ldots, s_p\}, p \leq N \cdot k\) are the nonzero eigenvalues of the rank-\(p\) matrix \(\sum^N_i J_i\T H J_i\).
+
+Notice that if a preconditioner approximates all eigenvalues and eigenvectors of a
+Something something spectrum, shape of spectrum, how steep is it?
+The more values you add, the steeper the spectrum becomes (even the nonzero one)
+
+\begin{enumerate}
+ \item Scaling
+ \item Eigenvalues and eigenvectors
+ \item Conditioning of the preconditioner
+ \item Targeting high and low eigenvalues
+ \item Rank, summation of low rank matrices
+ \item Speed
+\end{enumerate}
+
+Typically, when preconditioning, you need a matrix which is similar to the
+In this way, the preconditioner acts as an ``initial best guess'' of the value to be computed.
+As such, for CIQ, it would be expected that the preconditioner should approximate the value of \(H^{-1/2}\).
+In reality, we only need an approximation of \(H^{-1}\) wait is this really true? we still need \(H^{1/2} b\), we just calculate this value itself using CIQ?
+
+As such, the conditioning number of our preconditioner also becomes an issue, since we have two conflicting objectives: to have a preconditioner which is as similar to \(H\) as possible and to have a preconditioner which we can invert easily.
+The first point lends itself towards the use of preconditioners with a similar conditioning number as \(H\).
+However, in order to invert an arbitrary positive semi-definite matrix, we will often resort to using the conjugate gradient method which is sentitive to the conditioning number of the preconditioner.
+Thus, there is an intrinsic balance to be struck between a preconditioner which is very similar to \(H\) (but whose conditioning number is an issue in itself) and a preconditioner which is less similar to \(H\) (and will therefore not improve the conditioning of the problem).
+
+Let us consider the toy example of a preconditioner which we can guarantee to have the same eigenvectors as \(H\) and for which we can control the eigenvalues.
+One example of such a preconditioner would be???
+Depending on the rank of the preconditioner, we can then eliminate a number of the largest / lowest eigenvalues of \(H\) equal to the rank of the preconditioner.
+If we have a rank-one preconditioner whose only eigenvector is the largest eigenvector of \(H\), the second largest eigenvalue becomes the largest.
+In the best-case scenario, you can then eliminate
+If \(H\) has a very flat spectrum, then the problem cannot be ill conditioned and thus preconditioning is immaterial.
+If \(H\) has a very
+% This analogy can be extended in a hand-wavy way to an imperfect preconditioner which doesn't exactly approximate the eigenvalue
+
+Finally, performance of computing
+In CIQ, we will, in each iteration, perform one matrix-vector product \(H v\) and one preconditioner-vector product\sidenote{Because of this, to avoid the preconditioner becoming a performance bottleneck, the speed of the preconditioner-vector products should ideally be significantly greater than that of the matrix-vector products.} \textbf{check this}, as seen in \cref{alg:ciq}.
+As such, if the preconditioner-vector product is similarly or more expensive to compute compared to the matrix-vector product, then preconditoineing will ....
+However, an effective preconditioner will significantly improve the convergence speed of CIQ, and this effect can often outweigh the cost of computing the preconditioner.
+
+\subsection{Adam-esque fully linear GGN}\label{ssec:adam-preconditioner}
+If we calculate the GGN approximation of the Hessian by linearizing over the whole model and loss (as described in \cref{ssec:practical-ggn}), we get
+\begin{align}
+ P ={}& \nabla \nabla\T + \alpha \identity
+\\ P^{-1} ={}& \beta \cdot \nabla \nabla\T + \alpha^{-1} \identity
+\\ \beta ={}& \left(\alpha \norm{\nabla}^2 + \norm{\nabla}^4\right)^{-1} - \left(\alpha \norm{\nabla}^2\right)^{-1}.
+\end{align}
+DO I NEED TO PROVE THIS?
+The problem with using this approximation is that the rank of \(P\) is 1.
+This means that, best-case scenario, we can reduce the highest eigenvalue of the Hessian.
+This occurs when the gradient corresponds to the direction of highest precision (empirically, it seems that \emph{this is not the case}).
+This would then leave the second-highest eigenvalue unaffected.
+If the rank of our precision matrix is greater than one, then this value seems likely to be similarly large to the originally largest eigenvalue.
+Therefore, the conditioning number of our problem will not be significantly reduced.
+As such, this preconditioner is not worth pursuing.
+
+\subsection{Sub-sampling the data}
+We could simply choose to calculate the Hessian over fewer points and invert it using the conjugate gradient method to find our preconditioner.
+This would have the advantage of
+\begin{align}
+ \lambda\left( J\T J + \alpha I \right) ={}& \{ \underbrace{s_1, s_2, \ldots, s_k}_{k}, \underbrace{\alpha, \ldots, \alpha}_{n - k} \}
+\\ \lambda\left( N J\T J + \alpha I \right) ={}& \{ \underbrace{N s_1, N s_2, \ldots, N s_k}_{k}, \underbrace{\alpha, \ldots, \alpha}_{n - k} \}
+\\ \lambda\left( \frac{N}{B} \textstyle{\sum^B_i} J_i\T J_i + \alpha I \right) ={}& \{ \underbrace{?, \ldots, ?}_{\leq B \cdot k}, \underbrace{\alpha, \ldots, \alpha}_{\geq n - B \cdot k} \}
+ % \Span_\lambda\left( J\T J + \alpha \identity \right)
+\end{align}
+
+If the eigenvectors of \(J_i\T J_i\) are all (approximately) the same, then the \(\textstyle{\sum^N_i} J_i\T J_i\) term has \(k\) nonzero eigenvalues (the same as \(J\T J\)).
+If these eigenvectors are all different (orthogonal?), then the aforementioned term will have \(B \cdot k\) nonzero eigenvalues.
+
+Alternatively, we can also instead estimate the eigenvectors and eigenvalues of \(\textstyle{N / B \sum^B_i} J_i\T J_i\).
+This would \emph{hopefully} have an eigenbasis that is closer to that of \(\textstyle{\sum^N_i} J_i\T J_i\) without needing to calculate the precision matrix over the whole dataset (we may need to guarantee all classes are represented in \(x_i, i \sim \mathcal{B}\)).
+
+The issue with a few of these ideas is that, when we scale up the \(J\T J\) term to account for multiple observations, we worsen the conditioning number of our preconditioner, which means our conjugate gradient solution is worse.
+Now, dealing with poor conditioning only in the inversion of our preconditioner might be better than dealing with it everywhere else, but it still feels very weird that the preconditioner suffers from the same problems that it's trying to solve.
+
+\[\textstyle{\sum^N_i} s_1 v_i / \alpha \leq \kappa \leq N s_1 / \alpha\]
+
+TALK ABOUT PRECONDITIONER WITH ALL OBSERVATIONS BUT DIFFERENT ALPHA
+NOTE THAT THIS POSES ANOTHE ISSUE: THE FACT THAT THE PRECONDITIONER BECOMES WAY MORE EXPENSIVE ON ALL OBSERVATIONS
+
+As mentioned before, each msMINRES iteration performs one matrix-vector product and one preconditioner-vector product.
+However, even though the preconditioner is constructed in nearly the same way as the GGN matrix, the preconditioner is significantly cheaper to compute.
+For the subsampled GGN preconditioner, the preconditioner-vector product will then cost \(B / N \cdot t_{\mathrm{GGN}}\), where \(t_{\mathrm{GGN}}\) is the computational cost of performing the GGN-vector product\sidenote{Note that this analysis assumes that the computation of the GGN-vector product scales linearly in the number of observations used in the GGN matrix.}.
+Since \(N \gg B\), then the cost of computing the preconditioner will be negligible relative to that of the GGN-vector product.
+
+\subsection{Pivoted Cholesky}
+
+\cite{harbrecht2012low,bach2013sharp,gardner2018gpytorch}
+
+\begin{align}
+ \Sigma^{-1} ={}& \sum_i^N J_i\T J_i + \alpha \identity
+\\ \approx{}& L\T L + \alpha \identity
+\end{align}
+where \(L \in \reals^{k \times n}\), with \(k\) being the chosen rank of the pivoted Cholesky factorisation and \(n\) being the number of parameters.
+We can then use this approximation as a preconditioner by computing its inverse via the Woodbury matrix identity (\cref{sec:woodbury}):
+\begin{align}
+ P ={}& L\T L + \alpha \identity
+\\ P^{-1} ={}& \alpha^{-1} \identity - \alpha^{-2} L\T \left( \identity + \alpha^{-1} L L\T \right)^{-1} L
+\end{align}
+we then obtain the preconditioner-vector and inverse-preconditioner-vector products as in Equation~\ref{eq:pivoted-cholesky-preconditioner}.
+Notice that the ``scariest'' computation in the inverse preconditioner is inverting a \(k \times k \) matrix, where \(k\) is the rank of our pivoted Cholesky factorization (which we choose).
+\begin{align}\label{eq:pivoted-cholesky-preconditioner}
+ P v ={}& L\T ( L v ) + \alpha v
+\\ P^{-1} v ={}& \alpha^{-1} v - \alpha^{-2} L\T ( \underbrace{\identity + \alpha^{-1} L L\T}_{k \times k} )^{-1} ( L v )
+\end{align}
+
+We can precompute \(L\T ( \identity + \alpha^{-1} L L\T )^{-1} \in \reals^{n \times k}\), meaning that we only need to perform two matrix multiplications for both the preconditioner- and inverse preconditioner-vector products.
+This would then require storing \(L \in \reals^{k \times n}\) and the aforementioned \(\reals^{n \times k}\) matrix\sidenote{Note that we can also precompute \(( \identity + \alpha^{-1} L L\T )^{-1} \in \reals^{k \times k}\), which means we would only need to store \(L \in \reals^{k \times n}\) and the aforementioned \(k \times k\) matrix, in which case we would just need to perform one more matrix multiplication (multiplying \(L\T\) with this \(k \times k\) matrix) in every preconditioner call.}.
+
+ADD SOMEWHERE:
+As we sum over multiple low-rank matrices, we will likely find that the minimum non-zero eigenvalue gets progressively smaller relative to the largest.
+When we have a single rank \(o\) GGN matrix, we have
+This means that the preconditioning of \(J J\T\) (which should be low-rank) will progressively become both \emph{less} low-rank and its condition number will progressively become higher.
+
+In terms of eigenvalues and eigenvectors,
+Assume that the pivoted Cholesky factorisation finds the \(k\) largest eigenvectors.
+If the fall-off in the eigenspectrum of this factorisation is steep enough, then the largest eigenvector \(s_1\) will be significantly larger than the smallest eigenvector \(s_k\), especially if the value of \(B\) is large enough that the
+
+Unfortunately, if want to use the pivoted Cholesky decomposition to approximate \(\sum^B_i J_i\T J_i\), we are largely trying to approximate the higher eigenvectors of the sum.
+In order for this to be the case, we will want to oversample vectors from this matrix which correspond to the largest eigenvalues.
+A common approach to sampling the largest eigenvalues is to sample vectors with probability proportional to the diagonal value~\cite{harbrecht2012low}.
+However, it is not, in general, possible to efficiently determine the diagonal of a matrix via matrix-vector products.
+Often,it is necessary to multiply a vector of zeros with a value of one at the index at which to find the diagonal.
+
+Alternatively, it is possible to approximate the diagonal of the GGN matrix as the element-wise product of the gradient with itself, similarly to the preconditioner described in \cref{ssec:adam-preconditioner} and the GGN approximation computed in \cref{eq:fully-linear-ggn}.
+
+In conclusion, to find the optimal parameters for the pivoted Cholesky preconditioner (the number of observations \(B\) and the rank of the pivoted Cholesky factorisation \(k\), there are some rules of thumb to keep in mind.
+Notably, the quality of the preconditioner depends largely on the ease of inverting the \(\identity + \alpha^{-1} L L\T\) matrix, which depends on its conditioning number.
+In practice, increasing the number
+I've found that it's most important to keep \(k\) low, such that the \(k \times k\) outer product of the pivoted Cholesky factorisation matrix \(L\) approximates few enough of the largest eigenvectors that the eigenvalue of the largest is similar to that of the smallest (meaning this outer product is well-conditioned).
+Overall, this means that you need a large enough \(k\) to approximate enough large eigenvectors and a small enough \(k\) to make sure the conditioning number of the outer product is low.
+Furthermore, you want enough observations \(B\) to be included in the preconditioner such that it's possible to accurately approximate the eigenvectors of the problem (why not include all of them, always?).
+
+\subsection{Randomly pivoted Cholesky}
+
+Unfortunately, similarly to the pivoted Cholesky preconditioner, the randomly pivoted Cholesky preconditioner requires the diagonal of the GGN matrix in order for the decomposition to be close to the GGN. (MENTION THIS IN THE PIVOTED CHOLESKY SECTION)
+
+\subsection{Other Laplace approximations}
+
+% Last-Layer Laplace
+
+
+
+% K-FAC Laplace
+
+
+
+However, due to the speed, effectiveness, and theoretical properties of other preconditioners proposed in this section, these methods were not attempted.
+
+\subsection{A Recap on Preconditioning}
+
+In general, it is worth noting the flaw with scaling the term in the \(\sum J\T H J\) term\sidenote{Or, more precisely, the term which is equivalent to the \(J\T H J\) term in the Hessian and is consequently used to approximate the largest eigenvalue.} preconditioner to account for the summing over a Hessian of a limited batch size
+
+\begin{enumerate}
+ \item Never scale your preconditioner up
+\end{enumerate}
+
+\section{Sampling Evaluation}
+
+\begin{align}
+ \epsilon_0\T \epsilon_0 \sim \chi^2(D)
+\end{align}
+
+Samples \(\epsilon\) which are normally distributed but do not have a mean of zero and a standard deviation of one can be normalised and their sum of square deviations be calculated by computing their Mahalanobis distances.
+
+\begin{align}
+ d ={}& \sqrt{(\epsilon - \mu)\T \Sigma^{-1} (\epsilon - \mu)}\label{eq:mahalanobis-distance}
+\\ \Rightarrow{}& d\T d \sim \chi^2(D) \nonumber
+\end{align}
+
+This has the advantage of only requiring computation of the precision matrix \(\Sigma^{-1}\).
+For the Laplace approximation, this can easily be computed as \(\Sigma^{-1} = - \nabla^2_\theta \log p(\theta \given x)\).
+Since \(- \nabla^2_\theta \log p(\theta \given x)\) can be computed exactly, this calculation can be performed to evaluate the whether a set of samples \(\epsilon\) are drawn from a distribution \(\normal(\mu, \Sigma)\) while only accessing the precision matrix in the evaluation.
+As such, this method can be used to evaluate whether the approximate normal samples obtained from CIQ are correctly distributed.
+
+\section{Limitations}
+
+Scaling with data?
+Any comments on the model size and how it scales?
diff --git a/chapters/textnotes.tex b/chapters/textnotes.tex
@@ -0,0 +1,163 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Margin Stuff}
+
+Sidenotes are a distinctive feature of all 1.5-column-layout books.
+Indeed, having wide margins means that some material can be displayed
+there. We use margins for all kind of stuff: sidenotes, marginnotes,
+small tables of contents, citations, and, why not?, special boxes and
+environments.
+
+\section{Sidenotes}
+
+Sidenotes are like footnotes, except that they go in the margin, where
+they are more readable. To insert a sidenote, just use the command
+\Command{sidenote\{Text of the note\}}. You can specify a
+mark\sidenote[O]{This sidenote has a special mark, a big O!} with \\
+\Command{sidenote[mark]\{Text\}}, but you can also specify an offset,
+which moves the sidenote upwards or downwards, so that the full syntax is:
+
+\begin{lstlisting}[style=kaolstplain]
+\sidenote[mark][offset]{Text}
+\end{lstlisting}
+
+If you use an offset, you always have to add the brackets for the mark,
+but they can be empty.\sidenote{If you want to know more about the usage
+of the \Command{sidenote} command, read the documentation of the
+\Package{sidenotes} package.}
+
+In \Class{kaobook} we copied a feature from the \Package{snotez}
+package: the possibility to specify a multiple of \Command{baselineskip}
+as an offset. For example, if you want to enter a sidenote with the
+normal mark and move it upwards one line, type:
+
+\begin{lstlisting}[style=kaolstplain]
+\sidenote[][*-1]{Text of the sidenote.}
+\end{lstlisting}
+
+As we said, sidenotes are handled through the \Package{sidenotes}
+package, which in turn relies on the \Package{marginnote} package.
+
+\section{Marginnotes}
+
+This command is very similar to the previous one. You can create a
+marginnote with \Command{marginnote[offset]\{Text\}}, where the offset
+argument can be left out, or it can be a multiple of
+\Command{baselineskip},\marginnote[-1cm]{While the command for margin
+notes comes from the \Package{marginnote} package, it has been redefined
+in order to change the position of the optional offset argument, which
+now precedes the text of the note, whereas in the original version it
+was at the end. We have also added the possibility to use a multiple of
+\Command{baselineskip} as offset. These things were made only to make
+everything more consistent, so that you have to remember less things!}
+\eg
+
+\begin{lstlisting}[style=kaolstplain]
+\marginnote[-12pt]{Text} or \marginnote[*-3]{Text}
+\end{lstlisting}
+
+\begin{kaobox}[frametitle=To Do]
+A small thing that needs to be done is to renew the \Command{sidenote}
+command so that it takes only one optional argument, the offset. The
+special mark argument can go somewhere else. In other words, we want the
+syntax of \Command{sidenote} to resemble that of \Command{marginnote}.
+\end{kaobox}
+
+We load the packages \Package{marginnote}, \Package{marginfix} and
+\Package{placeins}. Since \Package{sidenotes} uses \Package{marginnote},
+what we said for marginnotes is also valid for sidenotes. Side- and
+margin- notes are shifted slightly upwards
+(\Command{renewcommand\{\textbackslash marginnotevadjust\}\{3pt\}}) in
+order to align them to the bottom of the line of text where the note is
+issued. Importantly, both sidenotes and marginnotes are defined as
+floating if the optional argument (\ie the vertical offset) is left
+blank, but if the offset is specified they are not floating. Recall that
+floats cannot be nested, so in some rare cases you may encounter errors
+about lost floats; in those cases, remember that sidenotes and
+marginnotes are floats. To solve the problem, it may be possible to
+transform them into non-floating elements by specifying an offset of
+0pt.
+
+\section{Footnotes}
+
+Even though they are not displayed in the margin, we will discuss about
+footnotes here, since sidenotes are mainly intended to be a replacement
+of them. Footnotes force the reader to constantly move from one area of
+the page to the other. Arguably, marginnotes solve this issue, so you
+should not use footnotes. Nevertheless, for completeness, we have left
+the standard command \Command{footnote}, just in case you want to put a
+footnote once in a while.\footnote{And this is how they look like.
+Notice that in the PDF file there is a back reference to the text;
+pretty cool, uh?}
+
+\section{Margintoc}
+
+Since we are talking about margins, we introduce here the
+\Command{margintoc} command, which allows one to put small table of
+contents in the margin. Like other commands we have discussed,
+\Command{margintoc} accepts a parameter for the vertical offset, like
+so: \Command{margintoc[offset]}.
+
+The command can be used in any point of the document, but we think it
+makes sense to use it just at the beginning of chapters or parts. In
+this document I make use of a \KOMAScript\xspace feature and put it in
+the chapter preamble, with the following code:
+
+\marginnote{The font used in the margintoc is the same as the one for
+ the chapter entries in the main table of contents at the beginning
+ of the document.}
+
+\begin{lstlisting}[style=kaolstplain]
+\setchapterpreamble[u]{\margintoc}
+\chapter{Chapter title}
+\end{lstlisting}
+
+As the space in the margin is a valuable resource, there is the
+possibility to print a shorter version of the title in the margin toc.
+Thus, there are in total three possible versions for the title of a
+section (or subsection): the one for the main text, the one for the main
+table of contents, and the one for the margintoc. These versions can be
+specified at the same time when the section is created in the source
+\TeX file:
+\begin{lstlisting}[style=kaolstplain]
+\section[alternative-title-for-toc]{title-as-written-in-text}[alternative-title-for-margintoc]
+\end{lstlisting}
+
+By default, the margintoc includes sections and subsections.
+If you only want to show sections, add
+\begin{lstlisting}[style=kaolstplain]
+\setcounter{margintocdepth}{\sectiontocdepth}
+\end{lstlisting}
+somewhere in your preamble.
+
+\section{Marginlisting}
+
+On some occasions it may happen that you have a very short piece of code
+that doesn't look good in the body of the text because it breaks the
+flow of narration: for that occasions, you can use a
+\Environment{marginlisting}. The support for this feature is still
+limited, especially for the captions, but you can try the following
+code:
+
+\begin{marginlisting}[-1.35cm]
+ \caption{An example of a margin listing.}
+ \vspace{0.6cm}
+ \begin{lstlisting}[language=Python,style=kaolstplain]
+print("Hello World!")
+ \end{lstlisting}
+\end{marginlisting}
+
+\begin{verbatim}
+\begin{marginlisting}[-0.5cm]
+ \caption{My caption}
+ \vspace{0.2cm}
+ \begin{lstlisting}[language=Python,style=kaolstplain]
+ ... code ...
+ \end{lstlisting}
+\end{marginlisting}
+\end{verbatim}
+
+Unfortunately, the space between the caption and the listing must be
+adjusted manually; if you find a better way, please let me know.
+
+Not only textual stuff can be displayed in the margin, but also figures.
+Those will be the focus of the next chapter.
diff --git a/chapters/theory.tex b/chapters/theory.tex
@@ -0,0 +1,434 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Theory}
+\labch{theory}
+
+\section{Optimization in Deep Learning}
+
+Suppose a neural network is a real-valued function \(f: \reals^n \times \reals^d \rightarrow \reals^o\) parameterized in \(\bm\theta\) which maps an input \(\bm x\) to an output \(f(\bm x; \bm\theta) \equiv f_{\bm\theta}(\bm x)\).
+Our goal is to find the optimal parameters \(\bm\theta^*\) which best model the observed data.
+
+From a frequentist perspective, we can define a loss function \(\mathcal{L}(\bm\theta): \reals^d \rightarrow \reals\) such that the optimal parameters \(\bm\theta^*\) minimize this loss function.
+Often the loss function is defined as the negative log-likelihood of the data under the model, \(\mathcal{L}(\bm\theta) = -\log \lik\).
+In this case, the goal of optimization is to find the parameters \(\bm\theta^*\) which maximize the likelihood of the data~\footnote{Maximizing the likelihood is equivalent to minimizing the negative log likelihood, since the log function is strictly monotonically increasing.}.
+
+From a Bayesian perspective, we define a prior distribution \(p(\bm\theta)\) and a likelihood function \(\lik\) such that the goal of optimization is to find the parameters \(\bm\theta^*\) which maximize the posterior distribution \(p(\bm\theta \given \bm y)\).
+This way, we can quantify the uncertainty in our model parameters \(\bm\theta\) by computing the posterior distribution \(p(\bm\theta \given \bm y)\).
+
+Due to the assumption of independence between observations, we can factorise the negative log-likelihood loss as
+\begin{align}\label{eq:negative-log-likelihood}
+ p(\bm x \given \bm \theta) ={} & \prod_i^N p(x_i \given \bm \theta) \nonumber
+ \\ \mathcal{L}(\bm\theta) = -\log p(\bm x \given \bm \theta) ={}& - \sum_i^N \log p(x_i \given \bm \theta).
+\end{align}
+
+Similarly, for the negative log-posterior loss, we have
+\begin{align}\label{eq:negative-log-posterior}
+ \mathcal{L}(\bm\theta) = -\log p(\bm \theta \given \bm x) ={} & - \log p(\bm x \given \bm \theta) - \log p(\bm\theta) \nonumber
+ \\ ={}& \underbrace{-\sum_i^N \log p(x_i \given \bm \theta)}_{\text{Negative log-likelihood}} - \underbrace{\log p(\bm\theta)}_{\text{Regularisation}}
+\end{align}
+
+\subsection{Stochastic Gradient Descent}
+
+Stochastic gradient descent (SGD) is a first-order optimization algorithm which iteratively updates the parameters \(\bm\theta\) in the direction of the negative gradient of the loss function \(\mathcal{L}(\bm\theta)\).
+The update rule is given by
+%
+\begin{equation*}
+ \bm\theta_{t+1} = \bm\theta_t - \eta_t \nabla_{\bm\theta} \mathcal{L}(\bm\theta_t)
+\end{equation*}
+%
+where \(\eta_t\) is the learning rate at iteration \(t\).
+The learning rate \(\eta_t\) can be constant or adaptive, and is often chosen to be a decreasing function of \(t\).
+The gradient \(\nabla_{\bm\theta} \mathcal{L}(\bm\theta_t)\) is computed using a single training example \(\bm x_t, y_t\).
+The gradient is computed using the chain rule as
+%
+\begin{equation*}
+ \nabla_{\bm\theta} \mathcal{L}(\bm\theta_t) = \nabla_{\bm\theta} \log \lik(\bm x_t, y_t \given \bm\theta_t) \nabla_{\bm\theta} f_{\bm\theta_t}(\bm x_t)
+\end{equation*}
+%
+where \(\nabla_{\bm\theta} f_{\bm\theta_t}(\bm x_t)\) is the gradient of the neural network with respect to its parameters \(\bm\theta_t\).
+
+Note that \(\eta_t\) can be interpreted as the step size of the gradient descent algorithm.
+It can either be constant throughout training (\(\eta_t = \eta\)) or adaptive (if it varies throughout training).
+
+\section{The Hessian}
+
+The Hessian is a matrix of second-order partial derivatives of a scalar function.
+From now on, we will use the notation \(\nabla^2_{\bm\theta} f\) to represent the Hessian matrix of \(f\), where each element is given by \(\left(\nabla^2_{\bm\theta} f\right)_{i, j} = \frac{\partial^2 f}{\partial\theta_i \partial\theta_j}\).
+Suppose we have a function \(\mathcal{L}: \reals^d \rightarrow \reals\) parameterized by \(\bm \theta \in \reals^d\).
+Then the Hessian can be written as \(J_{\bm \theta}(\nabla_{\bm\theta} \mathcal{L})\).
+
+We are interested in the Hessian of the loss function \(\mathcal{L}(\bm\theta)\) with respect to the parameters \(\bm\theta\).
+For a neural network with \(d\) parameters, the Hessian is therefore a \(d \times d\) square matrix.
+Furthermore, if all of the neural network's second partial derivatives are continuous, then the Hessian is symmetric and positive definite.
+In this case, the Hessian is generally dominated by the block-diagonal \cite{martens2015optimizing}.
+
+Typically, the loss functions is considered as the negative log-likelihood or negative log-posterior.
+In this framing, if considering, e.g., the negative log-posterior loss from \cref{eq:negative-log-posterior}, we get\sidenote{For the common prior given by a zero-mean Gaussian \(\theta \sim \normal(\bm 0, \alpha^{-1} \identity)\), the log-prior regularisation term is given by \(\nabla^2_{\bm\theta} \log p(\bm \theta) = \alpha \identity\).}
+\begin{align}\label{eq:negative-log-posterior-hessian}
+ \nabla^2_{\bm\theta} \mathcal{L}(\bm \theta) ={} & \nabla^2_{\bm\theta} \left( \sum_i^N \log p(x_i \given \bm \theta) + \log p(\bm \theta) \right) \nonumber
+ \\ ={}& \nabla^2_{\bm\theta} \sum_i^N \log p(x_i \given \bm \theta) + \nabla^2_{\bm\theta} \log p(\bm \theta) \nonumber
+ \\ ={}& \sum_i^N \nabla^2_{\bm\theta} \log p(x_i \given \bm \theta) + \nabla^2_{\bm\theta} \log p(\bm \theta).
+\end{align}
+
+\section{The Generalized Gauss-Newton Approximation}
+
+From the equation of the Hessian we can apply the chain rule twice and the product rule once to obtain a simpler expression
+%
+\begin{align}\label{eq:ggn-whole-hessian}
+ \nabla^2_{\bm \theta} \mathcal{L}
+ ={} & J_{\bm \theta}(\nabla_{\bm \theta} \mathcal{L})
+ = J_{\bm \theta}\left(\nabla_f \mathcal{L} \cdot \nabla_{\bm \theta} f\right) \nonumber
+ \\ ={}& J_{\bm \theta} (\nabla_{\bm \theta} f) \cdot \nabla_f \mathcal{L} + J_{\bm \theta} \left(\nabla_f \mathcal{L}\right) \cdot \nabla_{\bm \theta} f \nonumber
+ \\ ={}& \nabla^2_{\bm \theta} f \cdot \nabla_f \mathcal{L} + \nabla_{\bm \theta} f \T \cdot \nabla^2_f \mathcal{L} \cdot \nabla_{\bm \theta} f
+\end{align}
+with \(\nabla_f\) representing the gradient with respect to \(f(\bm x)\).
+
+Assume now we will approximate \(f\) using a first-order Taylor expansion.
+This approximation is given by
+%
+\begin{align}\label{eq:nn-taylor}
+ f_{\bm \theta}(\bm x) \approx{} f_{\bm \theta_0}(\bm x) + \nabla_{\bm \theta} f_{\bm \theta_0}(\bm x) \cdot (\bm \theta - \bm \theta_0) .
+ \\ \text{Notice that } \nabla^2_{\bm \theta} f_{\bm \theta_0}(\bm x) = 0 \nonumber
+\end{align}
+%
+% Notice that, in this approximation, we have \(\nabla^2_{\bm \theta} f_{\bm \theta_0}(\bm x) = 0\).
+In this way, we are linearising our neural network function.
+By combining \cref{eq:ggn-whole-hessian} and \cref{eq:nn-taylor} we then obtain the Gauss-Newton Hessian approximation,
+%
+\begin{align*}
+ \nabla^2_{\bm\theta} \mathcal{L}
+ \approx{} & (\nabla_{\bm\theta} f)\T (\nabla^2_f \mathcal{L}) (\nabla_{\bm\theta} f)
+\end{align*}
+
+For mean square error (MSE) loss, we have one output variable (\(o = 1\)), so \(f: \reals^n \times \reals^d \rightarrow \reals\).
+From the expression of MSE loss, we get the GGN-approximate Hessian
+%
+\begin{align*}%\label{eq:mse-hessian}
+ \nabla^2_{f} \mathcal{L} = & {} \frac{1}{N} \sum_{n=1}^N (y_n - f(\bm x_n))^2 = 1 \nonumber
+ \\ \Longrightarrow \nabla^2_{\bm\theta} \mathcal{L} =&{} (\nabla_{\bm\theta} f)\T (\nabla_{\bm\theta} f)
+\end{align*}
+
+For log-likelihood loss, we get
+\begin{align}\label{eq:negative-log-likelihood-ggn}
+ \nabla^2_{\bm\theta} \mathcal{L}(\bm \theta) ={} & \sum_i^N \nabla^2_{\bm\theta} \log p(x_i \given \bm \theta)
+ \\ \approx{}& \sum_i^N \left[\nabla_{\bm\theta} f (\bm \theta_0, x_i)\right]\T \left[\nabla^2_f \mathcal{L} (f(\bm \theta_0, x_i), t_i)\right] \left[\nabla_{\bm\theta} f(\bm \theta_0, x_i)\right] \nonumber
+ \\ ={}& \sum_i^N J_i\T H_i J_i \nonumber
+\end{align}
+
+\subsection{Practical Considerations for the GGN Approximation}\label{ssec:practical-ggn}
+
+Note that the generalised Gauss-Newton approximation requires that the function for which we would like to compute the Hessian, \(h(x)\), can be framed as a composition of two functions \(h(x) = g(f(x))\).
+However, there are many possible choices of functions \(f\) and \(g\) which, when composed, yield \(h\), and these different compositions can yield wildly varying performance for the GGN approximation~\cite{kunstner2019limitations}.
+Given that, in our case, \(h\) is the loss function with respect to the model parameters of a neural network, a natural choice of functions (and the choice we have assumed above) are \(g: \reals^o \rightarrow \reals\) and \(f: \reals^d \rightarrow \reals^o\) where \(g\) is the loss function with respect to the model output and \(f\) is a function which maps the model parameters to a model output, for some given model input \(x\).
+This construction allows us to capture the curvature of the loss with respect to the model output, which can often be easily computed in a simple closed form, without requiring the computation of the curvature of the model function with respect to its parameters; this therefore corresponds to linearising the model function with a Taylor expansion as in \cref{eq:nn-taylor}.
+However, this is not the only possible construction.
+
+Another choice is to linearise the whole likelihood function with respect to the model parameters.
+This yields \(g(y) = -\log(y)\) and thus we obtain
+\begin{align}\label{eq:fully-linear-ggn}
+ \nabla^2_{\bm \theta} \mathcal{L}
+ ={} & \nabla f \T \cdot \nabla^2 g \cdot \nabla f \nonumber
+ \\ ={}& \nabla f \T \cdot \frac{1}{f(\bm\theta)^2} \cdot \nabla f \nonumber
+ % \\ ={}& \left(\nabla f / f(\bm\theta)\right)\T \cdot \left(\nabla f / f(\bm\theta)\right) \nonumber
+ \\ ={}& \left(\frac{1}{f(\bm\theta)} \nabla f\right)\T \cdot \left(\frac{1}{f(\bm\theta)} \nabla f\right) \nonumber
+ \\ ={}& \left(-\nabla_f g \cdot \nabla_{\bm\theta} f\right)\T \cdot \left(-\nabla_f g \cdot \nabla_{\bm\theta} f\right) \nonumber
+ \\ ={}& \left(\nabla_{\bm\theta} g \circ f \right)\T \cdot \left(\nabla_{\bm\theta} g \circ f \right) \nonumber
+ \\ ={}& \nabla_{\bm\theta} \mathcal{L}\T \cdot \nabla_{\bm\theta} \mathcal{L}
+\end{align}
+The diagonal of this matrix is given by the element-wise square of the gradient \( \nabla_{\bm\theta} \mathcal{L} \odot \nabla_{\bm\theta} \mathcal{L} \).
+
+Note that the choice of application of the generalised Gauss-Newton approximation is not based on having a reduced computational cost --- both the Hessian and the GGN approximation require the same number of forward and backward passes.
+However, the GGN approximation is more better behaved than the exact Hessian, thereby making it more suitable for practical applications.
+This is because the GGN approximation is designed to be positive semi-definite, whereas the exact Hessian can be vulnerable to negative curvature.
+This feature can limit its use for optimisation algorithms that require a positive definite Hessian, such as conjugate gradient descent, or the Laplace approximation, which employs the inverse of the Hessian as the covariance matrix of a Gaussian distribution, thereby assuming a positive semi-definite covariance matrix.
+
+The Levenberg-Marquardt heuristic~\cite{pascanu2013revisiting}
+
+A GGN approximation is any approximation which fulfils \cref{eq:ggn-whole-hessian}.
+\cite{kunstner2019limitations}
+
+\section{Fisher Information}\label{sec:fisher-information}
+% https://agustinus.kristia.de/techblog/2018/03/11/fisher-information/
+
+Consider the gradients of the log-likelihood optimization objective, given by the expression \(\nabla_{\bm\theta} \log\lik\).
+We can show that the expectation of these gradients is zero:
+\begin{align*}
+ \E{\lik}{\nabla_{\bm\theta} \log\lik} = & {} \E{\lik}{\nabla \log \lik}
+ \\ =&{} \int \nabla \log \lik \lik \, d \bm y
+ \\ =&{} \int \frac{\nabla \lik}{\lik} \lik \, d \bm y % as per the chain rule
+ \\ =&{} \int \nabla \lik \, d \bm y % as per the Leibniz integral rule
+ \\ =&{} \nabla \int \lik \, d \bm y
+ \\ =&{} \nabla 1
+ \\ =&{} 0
+\end{align*}
+
+Let us now analyze the covariance of these gradients.
+It is given by
+%
+\begin{align}\label{eq:gradient-covariance}
+ \mathbb{E}_\lik \Big[ & \left(\nabla \log\lik - \E{}{\nabla \log\lik}\right) \nonumber
+ \\ & \cdot \left(\nabla \log\lik - \E{}{\nabla \log\lik}\right)\T\Big] \nonumber
+ \\ ={}& \E{\lik}{\left(\nabla \log\lik - 0\right) \left(\nabla \log\lik - 0\right)\T} \nonumber
+ \\ ={}& \E{\lik}{\nabla \log\lik \nabla \log\lik\T} \nonumber
+ \\ \equiv{}& \mathcal{F}(\bm \theta)
+\end{align}
+%
+where we have defined the Fisher information matrix \(\mathcal{F}(\bm \theta)\) as the expectation of the outer product of the score function under our model.
+This product is usually intractable.
+It can, however, be approximated empirically by using samples from an empirical distribution \(q(\bm y)\) which is close to the true likelihood \(\lik\) \cref{eq:empirical-fisher}.
+%
+\begin{equation}\label{eq:empirical-fisher}
+ \mathcal{F} \approx \frac{1}{N} \sum^N_{n=1} \nabla \log p(y_n \given \bm \theta) \nabla \log p(y_n \given \bm \theta)\T.
+\end{equation}
+%
+Note, however, that this matrix is distinct from the Fisher information matrix~\cite{martens2020new, kunstner2019limitations}, since
+
+Another nontrivial property of the Fisher information matrix is that it is equivalent to the negative expectation of the Hessian of the log-likelihood.
+%
+\begin{align*}
+ H_{\bm\theta} \left(\log \lik\right) = & {} \nabla^2_{\bm\theta} \log \lik
+ \\ =&{} \nabla_{\bm\theta} \nabla_{\bm\theta} \log \lik
+ \\ =&{} \nabla_{\bm\theta} \frac{\nabla_{\bm\theta} \lik}{\lik}
+ \\ =&{} \frac{}{}
+\end{align*}
+%
+The Fisher information matrix can therefore be interpreted as the curvature of the log-likelihood.
+
+RELATIONSHIP WITH GAUSS-NEWTON APPROXIMATION
+for common loss functions, the GGN is equivalent to the Fisher information matrix~\cite{martens2020new}
+\cite{kunstner2019limitations}
+
+RELATIONSHIP WITH KL DIVERGENCE
+% https://agustinus.kristia.de/techblog/2018/03/14/natural-gradient/
+% https://en.wikipedia.org/wiki/Fisher_information#Relation_to_relative_entropy
+
+LIMITATIONS OF FISHER INFORMATION (MAYBE ESPECIALLY EMPIRICAL?)
+\cite{kunstner2019limitations}
+
+\section{The Natural Gradient}
+% https://agustinus.kristia.de/techblog/2018/03/14/natural-gradient/
+
+Natural Gradient Descent is an approximate second-order optimisation method. It has an interpretation as optimizing over a Riemannian manifold using an intrinsic distance metric, which implies the updates are invariant to transformations such as whitening. By using the positive semi-definite (PSD) Gauss-Newton matrix to approximate the (possibly negative definite) Hessian, NGD can often work better than exact second-order methods.
+
+\textcite{amari1998natural} introduced the natural gradient as a way to optimize a function \(f\) parameterized by \(\bm\theta\) by following the direction of steepest descent in the Fisher information metric.
+It can be interpreted as
+% The algorithm can be seen in \cref{natural-gradient}.
+%
+% \begin{algorithm}
+% \caption{Natural Gradient Descent} \label{natural-gradient}
+% \begin{algorithmic}[1]
+% \For {$i = 1$ to $T$}
+% \State Calculate \(\mathcal{L}(\bm\theta)\)
+% \State Calculate the gradient of the loss \(\nabla_{\bm\theta} \mathcal{L}(\bm\theta)\)
+% \State Calculate the Fisher information matrix \(\mathcal{F}(\bm\theta)\)
+% \State Calculate the natural gradient \(\nabla_{\bm\theta} \mathcal{L}(\bm\theta) \mathcal{F}(\bm\theta)^{-1}\)
+% \State Update the parameters \(\bm\theta \gets \bm\theta - \eta \nabla_{\bm\theta} \mathcal{L}(\bm\theta) \mathcal{F}(\bm\theta)^{-1}\)
+% \EndFor
+% \State \Return {\(\bm\theta\)}
+% \end{algorithmic}
+% \end{algorithm}
+%
+In practice, deep learning models have millions (or billions) or parameters, and the Fisher information matrix is consequently often too large to be computed and stored explicitly.
+This therefore limits the applicability of the natural gradient.
+Instead, it is often approximated by the diagonal of the Hessian.
+
+% This brings us to the Adam optimizer, which is a popular choice for optimizing deep neural networks.
+\cite{kingma2014adam}
+\cite{martens2015optimizing}
+\cite{botev2017practical}
+
+For a recent, detailed discussion of the natural gradient, see \cite{martens2020new}.
+\cite{wu2019logan}
+
+\section{Inverting a low-rank matrix}\label{sec:woodbury}
+% Theorem 1.1 ([18, Thm. 1.35]). Let U, V ∈ Cn×k with k ≤ n and assume that V ∗U is nonsingular. Let f be defined on the spectrum of A = αIn + U V ∗, and if k = n let f be defined at α. Then (1.2) f (A) = f (α)In + U (V ∗U )−1(f (αIk + V ∗U ) − f (α)Ik )V ∗.
+
+\begin{align}
+ f\left(J\T J + \alpha \identity\right) ={} & f(\alpha) \identity + J\T \left(J J\T\right)^{-1} \left( f\left( J J\T + \alpha \identity \right) - f(\alpha) \identity \right) J \nonumber
+ \\ f\left(J\T J + \alpha \identity\right) v ={}& f(\alpha) v + J\T \left(J J\T\right)^{-1} \left( f\left( J J\T + \alpha \identity \right) - f(\alpha) \identity \right) J v
+\end{align}
+
+\begin{align}
+ \left(J\T J + \alpha \identity\right)^{-1} ={} & \alpha^{-1} \identity + J\T \left(J J\T\right)^{-1} \left( \left( J J\T + \alpha \identity \right)^{-1} - \alpha^{-1} \identity \right) J
+\end{align}
+
+\begin{align}
+ P ={} & L\T L + \alpha \identity
+ \\ P^{-1} ={}& \alpha^{-1} \identity - \alpha^{-2} L\T \left( \identity + \alpha^{-1} L L\T \right)^{-1} L
+\end{align}
+\begin{align}
+ P v ={} & L\T ( L v ) + \alpha v
+ \\ P^{-1} v ={}& \alpha^{-1} v - \alpha^{-2} L\T ( \underbrace{\identity + \alpha^{-1} L L\T}_{k \times k} )^{-1} ( L v )
+\end{align}
+
+
+
+% https://mathoverflow.net/questions/121411/expectation-of-square-root-of-binomial-r-v
+\begin{align}
+ \E{}{\sqrt{X}} \approx \sqrt{\E{}{X}} \left( 1 - \frac{\Var{X}}{8 \E{}{X}^2} \right)
+\end{align}
+
+\section{Krylov Subspace Methods}
+
+A Krylov subspace of a square matrix \(\bm K \in \reals^{N \times N}\) and a vector \(\bm b \in \reals^{N}\) is a subspace spanned by the set of images of \(\bm b\) under the first \(j\) powers of \(\bm K\).
+\begin{align}
+ \mathcal{K}_j( \bm K, \bm b) = \mathrm{span}\left\{ \bm b, \bm K \bm b, \bm K^2 \bm b, \ldots, \bm K^{j-1} \bm b \right\}
+\end{align}
+
+Because of this, matrix-vector products alone are sufficient to construct a Krylov subspace.
+Krylov subspace methods leverage this fact to estimate functions of matrices multiplied with vectors, \(f(\bm K) \bm b\).
+These methods therefore identify approximate solutions which are contained within the \(j\)\textsuperscript{th} Krylov subspace, i.e., \(f(\bm K) \bm b \approx \bm c_j \in \mathcal{K}_j( \bm K, \bm b)\).
+
+These methods are always exact after \(N\) iterations, however are often almost exact after \(J \ll N\) iterations\cite{pleiss2020fast}.
+One major advantage Krylov subspace methods have is that they only require computation of matrix-vector products \(\bm K \bm b\), thereby not requiring direct instantiation of the matrix \(\bm K\).
+As such, if allocation of memory to store a matrix is infeasible, but the computational graph for this matrix can be determined, then methods such as the conjugate gradient method or the minimum residual method can be used to approximate the inverse of this matrix~\cite{giordano2018covariances, nocedal1999numerical}.
+
+\subsection{The Lanczos Algorithm}
+
+One application known as the Lanczos algorithm adapts the power iteration method, which computes the greatest eigenvector of a square matrix \(\bm K\).
+The power iteration method involves iteratively computing the normalised product of \(\bm K\) with a random vector, i.e.,
+\begin{align}
+ \bm b_{j+1} = \frac{\bm K \bm b_{j}}{\norm{\bm K \bm b_{j}}},
+\end{align}
+for some number of iterations \(J\), such that \(\lim_{j \to \infty} \bm b_j\) equals the eigenvector corresponding to the largest eigenvalue of \(\bm K\).
+This thereby yields a set of vectors which is proportional to\sidenote{This is because the norm \(\zeta_j := \norm{\bm K \bm b_{j}}\) is a scalar, which means that \(\bm b_j = \bm K \bm b_{j-1} \zeta_{j-1}^{-1} = \zeta_{j-1}^{-1} \bm K \bm b_{j-1}\), so \(\bm b_j = Z \bm K \bm b_{j-1}\) where \(Z = \prod_{i=0}^{j-1} \zeta_{i}^{-1} \).} \(\{ \bm b_0, \bm K \bm b_0, \bm K^2 \bm b_0, \ldots, \bm K^{j-1} \bm b_0 \}\) which is clearly a spanning set for a Krylov subspace.
+However, the power iteration method only uses the last vector \(\bm b_J\), thereby effectively ``wasting'' the other vectors \(\bm b_1, \ldots, \bm b_{J-1}\).
+To make use of the entire Krylov subspace \(\mathcal{K}_j(\bm K, \bm b_0)\), the Lanczos algorithm combines the power iteration method with the Gram–Schmidt process, along with the assumption that \(\bm K\) is Hermitian, to produce an orthonormal basis from the Krylov subspace.
+This method then converts an eigendecomposition problem for \(\bm K\) into an eigendecomposition problem for a tridiagonal matrix of rank \(J\) whose maximum and minimum eigenvalues will be close to those of \(\bm K\).
+
+\subsection{The Conjugate Gradient Method}
+
+Further adjustments to the Lanczos algorithm yield the conjugate gradient method, a Krylov subspace method used to compute the inverse of a matrix by solving a system of equations as
+\begin{align*}
+ \bm x = \bm K^{-1} \bm b,
+\end{align*}
+where \(\bm K\) is a positive semi-definite\marginnote{Note that a positive semi-definite matrix is square.} matrix.
+The algorithm itself can be seen in \cref{alg:cg}.
+
+% EXPLANATION
+% \emph{When even calculating or instantiating Hηη is prohibitively time-consuming, one can use conjugate gradient algorithms to approximately compute H−1 ηη gη (Wright and Nocedal, 1999, Chapter 5). The advantage of conjugate gradient algorithms is that they approximate H−1 ηη gη using only the Hessian-vector product Hηη gη, which can be computed efficiently using automatic differentiation without ever forming the full Hessian Hηη. See, for example, the hessian vector product method of the Python autograd package (Maclaurin et al., 2015). Note that a separate conjugate gradient problem must be solved for each column of gη| , so if the parameter of interest g (θ) is high-dimensional it may be faster to pay the price for computing and inverting the entire matrix Hηη . See 5.3.2 for more discussion of a specific example. In Theorem 2, we require η0∗ to be at a true local optimum. Otherwise the estimated sensitivities may not be reliable (e.g., the covariance implied by Eq. (14) may not be positive definite). We find that the classical MFVB coordinate ascent algorithms (Blei et al. (2016, Section 2.4)) and even quasi-second order methods, such as BFGS (e.g., Regier et al., 2015), may not actually find a local optimum unless run for a long time with very stringent convergence criteria. Consequently, we recommend fitting models using second-order Newton trust region methods. When the Hessian is slow to compute directly, as in Section 5, one can use the conjugate gradient trust region method of Wright and Nocedal (1999, Chapter 7), which takes advantage of fast automatic differentiation Hessian-vector products without forming or inverting the full Hessian.}
+
+\begin{marginfigure}
+ \begin{algorithm}[H]
+ \caption{Conjugate Gradient (CG)}\label{alg:cg}
+ \Input{$\bm K \succ 0$, $\bm b$, $\bm P \succ 0$, $J > 0$}
+ \Output{$\bm x = \bm K^{-1} \bm b$, $\bm\alpha$, $\bm\beta$}
+ $\bm x_0 \gets \bm 0$\;
+ $\bm r_0 = \bm b - \bm K \bm x_0$\;
+ $\bm p_0 = \bm z_0 = \bm P \bm r_0$\;
+ $\gamma_0 = \bm r_0 \bm z_0$\;
+ \For{$j \gets 1$ \KwTo $J$}{
+ $\bm y_{j} \gets \bm A \bm p_{j-1}$\;
+ $\alpha_j = \gamma_{j-1} / (\overline{\bm p_{j-1}} \bm y_{j})$\;
+ $\bm x_{j} = \bm x_{j-1} + \alpha_j \bm p_{j-1}$\;
+ $\bm r_{j} = \bm r_{j-1} - \alpha_j \bm y_{j}$\;
+ $\bm z_{j} = \bm M \bm r_{j}$\;
+ $\gamma_{j} = \overline{\bm r_{j}} \bm z_{j}$\;
+ $\beta_{j} = \gamma_{j} / \gamma_{j-1}$\;
+ $\bm p_{j} = \bm z_{j} + \beta_{j} \bm p_{j-1}$\;
+ }
+ $\bm x \gets \bm x_{J}$\;
+ \end{algorithm}
+\end{marginfigure}
+
+% \begin{algorithm}
+% \caption{Conjugate Gradient (CG)}\label{alg:cg}
+% \Input{$K \succ 0$, $b$, $P \succ 0$, $J > 0$}
+% \Output{$x = K^{-1} b$, $\bm\alpha$, $\bm\beta$}
+% $r = b - K x$\;
+% $p = z = P r$\;
+% $\gamma_0 = r z$\;
+% \While{$\norm{r}^2_2 > 10^{-4} \And j < J$}{
+% $y \gets K p$\;
+% $\alpha_j \gets \gamma_{j-1} / (p\T y)$\;
+% $x \gets x + \alpha_j p$\;
+% $r \gets r - \alpha_j y$\;
+% $z \gets P r$\;
+% $\gamma_j \gets {r}\T z$\;
+% $\beta_j \gets \gamma_j / \gamma_{j-1}$\;
+% $p \gets z + \beta_j p$\;
+% }
+% \end{algorithm}
+
+\subsection{The Minimum Residual Method}\label{ssec:msminres}
+
+Another method which is derived from the Lanczos algorithm is the minimum residual method (MINRES).
+Given a certain number of iterations \(J\), the MINRES algorithm constructs a Krylov subspace by exploiting the three-vector recurrence (like conjugate gradient does) and solves a linear system \(\bm K \bm c = \bm b\), where \(\bm K\) is a square matrix (though not necessarily a symmetric one), by finding the least-squares solution which lies within this Krylov subspace of dimension \(J\), i.e., by solving the constrained optimisation problem characterised by
+\begin{align}
+ \bm c^* = \argmin_{\bm c \in \mathcal{K}_J(\bm K, \bm b)} \norm{\bm K \bm c - \bm b}_2.
+\end{align}
+
+This method then
+
+The MINRES algorithm can then be trivially extended to solve for multiple shifts, as seen in \cref{alg:msminres}.
+
+% \begin{algorithm}
+% \caption{Multi-Shift Minimum Residual (msMINRES)}\label{alg:msminres}
+% \Input{$K \succ 0$, $b$, $P \succ 0$, $t_1, \ldots, t_Q$, $J > 0$}
+% \Output{$c_1 = (K + t_1)^{-1} b, \ldots, c_Q = (K + t_Q)^{-1} b$}
+% $q_1 \gets b / \norm{b}_2$\;
+% $v_1 \gets K q_0$\;
+% $\delta_1 \gets \norm{b}_2, \delta_0 \gets 1$\;
+% \For{$q \gets 1$ \KwTo $Q$}{
+% $c_1^{(q)} \gets 0$\;
+% $d_1^{(q)}, d_0^{(q)} \gets 0$\;
+% $\gamma_2^{(q)} \gets \norm{b}_2$\;
+% $\eta_1^{(q)} \gets 1, \eta_0^{(q)} \gets 0$\;
+% }
+% \For{$j \gets 2$ \KwTo $J$}{
+% $q_j \gets v_j / \delta_j$\;
+% $v_j \gets K q_j - \delta_j q_{j-1}$\;
+% $\gamma_j \gets q_j v_j$\;
+% $v_j \gets \gamma_j q_j$\;
+% $\delta_j \gets \norm{v_j}$\;
+% \For{$q \gets 1$ \KwTo $Q$}{
+% $\epsilon_j^{(q)} \gets \delta_{j-1} \left( \delta_{j-2} / \sqrt{\delta_{j-2}^2 + \eta_{j-2}^{(q)2}} \right)$\;
+% $\zeta_j^{(q)} \gets \delta_{j-1} \left( \eta_{j-2}^{(q)} / \sqrt{\delta_{j-2}^2 + \eta_{j-2}^{(q)2}} \right)$\;
+% $\eta_j^{(q)} \gets (\gamma_j + t_q) \left( \eta_{j-1}^{(q)} / \sqrt{\delta_{j-1}^2 + \eta_{j-1}^{(q)2}} \right) + \zeta_j^{(q)} \left( \delta_{j-1} / \sqrt{\delta_{j-1}^2 + \eta_{j-1}^{(q)2}} \right)$\;
+% $\zeta_j^{(q)} \gets \zeta_j^{(q)} \left( \eta_{j-1}^{(q)} / \sqrt{\delta_{j-1}^2 + \eta_{j-1}^{(q)2}} \right) + (\gamma_j + t_q) \left( \delta_{j-1} / \sqrt{\delta_{j-1}^2 + \eta_{j-1}^{(q)2}} \right)$\;
+% $\eta_j^{(q)} \gets \eta_j^{(q)} \left( \eta_{j}^{(q)} / \sqrt{\delta_{j}^2 + \eta_{j}^{(q)2}} \right)$\;
+% $\varphi_j^{(q)} \gets \varphi_{j-1}^{(q)} \left( \delta_{j-1} / \sqrt{\delta_{j-1}^2 + \eta_{j-1}^{(q)2}} \right) \left( \eta_{j}^{(q)} / \sqrt{\delta_{j}^2 + \eta_{j}^{(q)2}} \right)$\;
+% $d_j^{(q)} \gets \left( q - \zeta_j^{(q)} d_{j-1}^{(q)} - \epsilon_{j}^{(q)} d_{j-2}^{(q)} \right) / \eta_{j}^{(q)}$\;
+% $c_j^{(q)} \gets c_{j-1}^{(q)} + \varphi_{j}^{(q)} + d_{j}^{(q)}$\;
+% }
+% }
+% \end{algorithm}
+
+\section{Jacobi Elliptic Functions}\label{sec:jacobi-elliptic}
+
+Complete elliptic integral of the first kind is given by
+\begin{align}
+ \mathcal{K}p) = \int_0^{\pi/2} \left( 1 - m \sin(t)^2 \right)^{-1/2}\,dt, \quad p = 1 - m,
+\end{align}
+and is defined in the domain\sidenote{Note that if \(p > 1\), then what should be computed is instead the identity \(\mathcal{K}(p) = \mathcal{K}(1/p) / \sqrt{p}\).} \(0 < p \leq 1\).
+This integral is approximated by
+\begin{align}
+ \mathcal{K}(p) \approx P(p) - \log(p) Q(p)
+\end{align}
+where \(P\) and \(Q\) are tenth-order polynomials.
+
+Jacobian elliptic functions \(\sn(u \given m)\), \(\cn(u \given m)\), and \(\dn(u \given m)\)
+undefined outside the range \(0 \leq u \leq 1\)??? can't be right
+% These functions are periodic, with quarter-period on the real axis equal to the complete elliptic integral ellipk(m).
+% Relation to incomplete elliptic integral: If u = ellipk(phi,m), then sn(u|m) = sin(phi), and cn(u|m) = cos(phi). The phi is called the amplitude of u.
+% Computation is by means of the arithmetic-geometric mean algorithm, except when m is within 1e-9 of 0 or 1. In the latter case with m close to 1, the approximation applies only for phi < pi/2.
+
+Honestly don't understand any of these whatsoever right now
+
+\begin{marginfigure}
+ % \input{images/ellipk.tex}
+ \includegraphics[scale=1]{ellipk.pdf}
+ \caption{Comparison of SciPy and JAX \texttt{ellipk} implementations. For values for which the complete elliptic integral \(K(p)\) is well-defined, both implementations yield the same results. Furthermore, due to its static nature, JAX implementation is compatible with \emph{just-in-time} compilation.}
+ \labfig{fig:ellipk}
+\end{marginfigure}
+
+\begin{figure}
+ \centering
+ % \input{images/ellipj.tex}
+ \includegraphics[scale=1]{ellipj.pdf}
+ \caption{Comparison of SciPy and JAX \texttt{ellipj} implementations. For values for which the functions are well-defined, both implementations yield the same results. Functions plotted are $\sn(u \given m = 0.7)$, $\cn(u \given m = 0.7)$, and $\dn(u \given m = 0.7)$ Furthermore, due to its static nature, JAX implementation is compatible with \emph{just-in-time} compilation.}
+ \label{fig:ellipj}
+\end{figure}
+
+% \section{The Mahalanobis Distance and the Chi-Squared Distribution}
+
+
diff --git a/chapters/training.tex b/chapters/training.tex
@@ -0,0 +1,27 @@
+\setchapterpreamble[u]{\margintoc}
+\chapter{Training}
+\labch{training}
+
+\section{Maximising the Evidence}
+
+According to Bayes' rule, the posterior probability for a given model is equal to
+\begin{align}
+ p(\theta \given x) = \frac{p(x \given \theta) p(\theta)}{p(x)},
+\end{align}
+where \(p(x)\) is known as the evidence\sidenote{It's also known as the \emph{marginal likelihood}, since it's obtained by marginalising out \(\theta\) from the likelihood, as per \(p(x) = \int_\theta p(x \given \theta) p(\theta)\,d\theta\).}.
+
+\textcite{fong2020marginal} argue that maximising the marginal (and consequently the log-marginal) is equivalent to performing k-fold cross validation for all values \(k = 1, \ldots, \infty\) and choosing the model with the highest average posterior probability (across each of \(k\) folds and across all values of \(k\)).
+
+\section{The Determinant Lower Bound}
+
+\subsection{Hutchinson's Trace Estimator}
+
+
+
+\subsection{Scaling}
+
+
+
+\subsection{Computational Cost}
+
+
diff --git a/compileall.sh b/compileall.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# Compile document
+pdflatex -interaction=nonstopmode main
+
+# Compile nomenclature
+makeindex main.nlo -s nomencl.ist -o main.nls
+
+# Compile index
+makeindex main
+
+# Compile bibliography
+biber main
+
+# Compile document
+pdflatex main
+
+# Compile glossary
+makeglossaries main
+
+# Compile document
+pdflatex main
diff --git a/figures/cauchy_integral.tex b/figures/cauchy_integral.tex
@@ -0,0 +1,30 @@
+\begin{tikzpicture}
+ \coordinate (O) at (0,0);
+ \coordinate (X) at (\xmax,0);
+ \coordinate (c) at (1.5,0);
+ \path (c) +(-\R,0) coordinate (min);
+ \path (c) +(\R,0) coordinate (max);
+ \draw[->,line width=0.9] (\xmin,0) -- (1.08*\xmax,0) node[right] {$\mathrm{Re}$};
+ \draw[->,line width=0.9] (0,\ymin) -- (0,1.08*\ymax) node[left] {$\mathrm{Im}$};
+ \draw (c) circle (\R);
+ \draw[->,black,very thick,line cap=round] (max) arc (0:40:\R) node[midway, right=0.02] {$\tau$};
+ \tick{0.56, 0}{90};
+ \tick{0.71, 0}{90};
+ \tick{1.1, 0}{90};
+ \tick{1.27, 0}{90};
+ \tick{1.5, 0}{90} node[scale=0.9,below=-0.7] {$\lambda_{\mathrm{min}}, \ldots, \lambda_{\mathrm{max}}$};
+ \tick{1.86, 0}{90};
+ \tick{1.95, 0}{90};
+ \tick{2.23, 0}{90};
+ \draw [
+ thick,
+ decoration={
+ brace,
+ mirror,
+ raise=0.40cm
+ },
+ decorate
+ ] (0.5, 0) -- (2.5, 0)
+ node [pos=0.5,anchor=north,yshift=-0.55cm] {$\lambda(\bm K)$};
+ \node at (2.8, -0.95) {$\Gamma$};
+\end{tikzpicture}
diff --git a/figures/cauchy_quadrature.tex b/figures/cauchy_quadrature.tex
@@ -0,0 +1,49 @@
+\begin{tikzpicture}
+ \coordinate (O) at (0,0);
+ \coordinate (X) at (\xmax,0);
+ \coordinate (c) at (1.5,0);
+ \path (c) +(-\R,0) coordinate (min);
+ \path (c) +(\R,0) coordinate (max);
+ \draw[->,line width=0.9] (\xmin,0) -- (1.08*\xmax,0) node[right] {$\mathrm{Re}$};
+ \draw[->,line width=0.9] (0,\ymin) -- (0,1.08*\ymax) node[left] {$\mathrm{Im}$};
+ \draw (c) circle (\R);
+ \node (min') at (0.85, 1.65) {$\lambda_{\mathrm{min}}$};
+ \draw [->] (min') to [bend right=25] ([xshift=-2pt, yshift=2pt] min);
+ \node (max') at (2.5, 1.7) {$\lambda_{\mathrm{max}}$};
+ \draw [->] (max') to [bend right=-45] ([xshift=2pt, yshift=2pt] max);
+ \tick{min}{90};
+ \tick{max}{90};
+ \fill[black] (c) ++(71.9267:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(76.54278905:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(104.21599104:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(135.6918:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(155.38527369:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(156.0644:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(146.670:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(162.42656553:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(164.4984:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(173.6467:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-174.5560:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-173.471:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-172.3679:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-170.0421:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-169.65941668:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-168.5632:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-167.3076:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-155.0252:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-146.67269207:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-144.91918461:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-140.89654159:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-135.86657224:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-105.76963215:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-97.69578016:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-85.51632491:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-22.6600:\R) circle[radius=1.2pt];
+ \fill[black] (c) ++(-3.31083639:\R) circle[radius=1.2pt];
+ % \node[preaction={fill, white, opacity=0.35}] (tau) at (c) {$\tau_1, \ldots, \tau_Q$};
+ % \draw [->] (tau) to [bend right=-45] ++(76.54278905:\R);
+ % \draw [->] (tau) to [bend right=-45] ++(164.4984:\R);
+ % \draw [->] (tau) to [bend right=-45] ++(-146.67269207:\R);
+ % \draw [->] (tau) to [bend right=-45] ++(-105.76963215:\R);
+ % \draw [->] (tau) to [bend right=-45] ++(-3.31083639:\R);
+\end{tikzpicture}
diff --git a/figures/chisq_sin.pdf b/figures/chisq_sin.pdf
Binary files differ.
diff --git a/figures/ellipj.pdf b/figures/ellipj.pdf
Binary files differ.
diff --git a/figures/ellipk.pdf b/figures/ellipk.pdf
Binary files differ.
diff --git a/figures/hessian_profile.pdf b/figures/hessian_profile.pdf
Binary files differ.
diff --git a/figures/hessian_profile_hvp.pdf b/figures/hessian_profile_hvp.pdf
Binary files differ.
diff --git a/figures/hessian_profile_ratios.pdf b/figures/hessian_profile_ratios.pdf
Binary files differ.
diff --git a/figures/sin.pdf b/figures/sin.pdf
Binary files differ.
diff --git a/figures/sin_deterministic.pdf b/figures/sin_deterministic.pdf
Binary files differ.
diff --git a/figures/sin_laplace.pdf b/figures/sin_laplace.pdf
Binary files differ.
diff --git a/glossary.tex b/glossary.tex
@@ -0,0 +1,9 @@
+\newglossaryentry{computer}{
+ name=computer,
+ description={is a programmable machine that receives input, stores and manipulates data, and provides output in a useful format}
+}
+
+% Glossary entries (used in text with e.g. \acrfull{fpsLabel} or \acrshort{fpsLabel})
+\newacronym[longplural={Frames per Second}]{fpsLabel}{FPS}{Frame per Second}
+\newacronym[longplural={Tables of Contents}]{tocLabel}{TOC}{Table of Contents}
+
diff --git a/kao.sty b/kao.sty
@@ -0,0 +1,1355 @@
+\ProvidesPackage{kao}
+
+%----------------------------------------------------------------------------------------
+% USEFUL PACKAGES AND COMMANDS
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{etoolbox} % Easy programming to modify TeX stuff
+\RequirePackage{calc} % Make calculations
+\RequirePackage[usenames,dvipsnames,table]{xcolor} % Colours
+\RequirePackage{iftex} % Check wether XeTeX is being used
+\RequirePackage{xifthen} % Easy conditionals
+\RequirePackage{options} % Manage class options
+\RequirePackage{xparse} % Parse arguments for macros
+\RequirePackage{xpatch} % Patch LaTeX code in external packages
+\RequirePackage{xstring} % Parse strings
+\RequirePackage{afterpage} % Run commands after specific pages
+\RequirePackage{imakeidx} % For the index; must be loaded before the 'caption' and 'hyperref' packages
+\RequirePackage{varioref} % For the cross-references; must be loaded before the 'hyperref' and 'cleveref' packages
+
+% Define \Ifthispageodd (with a capital 'i') to make kaobook compatible with older KOMAScript versions
+\@ifpackagelater{scrbase}{2019/12/22}{%
+}{%
+ \let\Ifthispageodd\ifthispageodd%
+}
+
+%----------------------------------------------------------------------------------------
+% TITLE AND AUTHOR MACROS
+%----------------------------------------------------------------------------------------
+
+% Provide an optional argument to the \title command in which to store a plain text title, without any formatting
+% Usage: \title[Plain Title]{Actual Title}
+\newcommand{\@plaintitle}{}
+\renewcommand{\title}[2][]{%
+ \gdef\@title{#2} % Store the full title in @title
+ \ifthenelse{\isempty{#1}}{ % If there is no plain title
+ \renewcommand{\@plaintitle}{\@title} % Use full title
+ }{ % If there is a plain title
+ \renewcommand{\@plaintitle}{#1} % Use provided plain-text title
+ }%
+ \hypersetup{pdftitle={\@plaintitle}} % Set the PDF metadata title
+}
+
+% Provide an optional argument to the \author command in which to store a plain text author, without any formatting
+% Usage: \author[Plain Author]{Actual Author}
+\newcommand{\@plainauthor}{}
+\renewcommand{\author}[2][]{%
+ \gdef\@author{#2} % Store the full author in @author
+ \ifthenelse{\isempty{#1}}{ % If there is no plain author
+ \renewcommand{\@plainauthor}{\@author}% Use full author
+ }{ % If there is a plain author
+ \renewcommand{\@plainauthor}{#1}% Use provided plain-text author
+ }%
+ \hypersetup{pdfauthor={\@plainauthor}} % Set the PDF metadata author
+}
+
+% Make a bookmark to the title page
+\pretocmd{\maketitle}{\pdfbookmark[1]{\@plaintitle}{title}}{}{}%
+
+%----------------------------------------------------------------------------------------
+% PAGE LAYOUT
+%----------------------------------------------------------------------------------------
+
+% Define lengths to set the scale of the document. Changing these
+% lengths should affect all the other pagesize-dependent elements in the
+% layout, such as the geometry of the page, the spacing between
+% paragraphs, and so on. (As of now, not all the elements rely on hscale
+% and vscale; future work will address this shortcoming.)
+\newlength{\hscale}
+\newlength{\vscale}
+
+% By default, the scales are set to work for a4paper
+\setlength{\hscale}{1mm}
+\setlength{\vscale}{1mm}
+
+\@ifclasswith{\@baseclass}{a0paper}{\setlength{\hscale}{4mm}\setlength{\vscale}{4mm}}{}
+\@ifclasswith{\@baseclass}{a1paper}{\setlength{\hscale}{2.828mm}\setlength{\vscale}{2.828mm}}{}
+\@ifclasswith{\@baseclass}{a2paper}{\setlength{\hscale}{2mm}\setlength{\vscale}{2mm}}{}
+\@ifclasswith{\@baseclass}{a3paper}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{1.414mm}}{}
+\@ifclasswith{\@baseclass}{a4paper}{\setlength{\hscale}{1mm}\setlength{\vscale}{1mm}}{}
+\@ifclasswith{\@baseclass}{a5paper}{\setlength{\hscale}{0.704mm}\setlength{\vscale}{0.704mm}}{}
+\@ifclasswith{\@baseclass}{a6paper}{\setlength{\hscale}{0.5mm}\setlength{\vscale}{0.5mm}}{}
+\@ifclasswith{\@baseclass}{b0paper}{\setlength{\hscale}{4.761mm}\setlength{\vscale}{4.761mm}}{}
+\@ifclasswith{\@baseclass}{b1paper}{\setlength{\hscale}{3.367mm}\setlength{\vscale}{3.367mm}}{}
+\@ifclasswith{\@baseclass}{b2paper}{\setlength{\hscale}{2.380mm}\setlength{\vscale}{2.380mm}}{}
+\@ifclasswith{\@baseclass}{b3paper}{\setlength{\hscale}{1.681mm}\setlength{\vscale}{1.681mm}}{}
+\@ifclasswith{\@baseclass}{b4paper}{\setlength{\hscale}{1.190mm}\setlength{\vscale}{1.190mm}}{}
+\@ifclasswith{\@baseclass}{b5paper}{\setlength{\hscale}{0.837mm}\setlength{\vscale}{0.837mm}}{}
+\@ifclasswith{\@baseclass}{b6paper}{\setlength{\hscale}{0.570mm}\setlength{\vscale}{0.570mm}}{}
+\@ifclasswith{\@baseclass}{c0paper}{\setlength{\hscale}{4.367mm}\setlength{\vscale}{4.367mm}}{}
+\@ifclasswith{\@baseclass}{c1paper}{\setlength{\hscale}{3.085mm}\setlength{\vscale}{3.085mm}}{}
+\@ifclasswith{\@baseclass}{c2paper}{\setlength{\hscale}{2.180mm}\setlength{\vscale}{2.180mm}}{}
+\@ifclasswith{\@baseclass}{c3paper}{\setlength{\hscale}{1.542mm}\setlength{\vscale}{1.542mm}}{}
+\@ifclasswith{\@baseclass}{c4paper}{\setlength{\hscale}{1.090mm}\setlength{\vscale}{1.090mm}}{}
+\@ifclasswith{\@baseclass}{c5paper}{\setlength{\hscale}{0.771mm}\setlength{\vscale}{0.771mm}}{}
+\@ifclasswith{\@baseclass}{c6paper}{\setlength{\hscale}{0.542mm}\setlength{\vscale}{0.542mm}}{}
+\@ifclasswith{\@baseclass}{b0j}{\setlength{\hscale}{4.904mm}\setlength{\vscale}{4.904mm}}{}
+\@ifclasswith{\@baseclass}{b1j}{\setlength{\hscale}{3.467mm}\setlength{\vscale}{3.467mm}}{}
+\@ifclasswith{\@baseclass}{b2j}{\setlength{\hscale}{2.452mm}\setlength{\vscale}{2.452mm}}{}
+\@ifclasswith{\@baseclass}{b3j}{\setlength{\hscale}{1.733mm}\setlength{\vscale}{1.733mm}}{}
+\@ifclasswith{\@baseclass}{b4j}{\setlength{\hscale}{1.226mm}\setlength{\vscale}{1.226mm}}{}
+\@ifclasswith{\@baseclass}{b5j}{\setlength{\hscale}{0.867mm}\setlength{\vscale}{0.867mm}}{}
+\@ifclasswith{\@baseclass}{b6j}{\setlength{\hscale}{0.613mm}\setlength{\vscale}{0.613mm}}{}
+\@ifclasswith{\@baseclass}{ansiapaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
+\@ifclasswith{\@baseclass}{ansibpaper}{\setlength{\hscale}{1.328mm}\setlength{\vscale}{1.454mm}}{}
+\@ifclasswith{\@baseclass}{ansicpaper}{\setlength{\hscale}{2.057mm}\setlength{\vscale}{1.882mm}}{}
+\@ifclasswith{\@baseclass}{ansidpaper}{\setlength{\hscale}{2.662mm}\setlength{\vscale}{2.909mm}}{}
+\@ifclasswith{\@baseclass}{ansiepaper}{\setlength{\hscale}{4.114mm}\setlength{\vscale}{3.764mm}}{}
+\@ifclasswith{\@baseclass}{letterpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
+\@ifclasswith{\@baseclass}{executivepaper}{\setlength{\hscale}{0.876mm}\setlength{\vscale}{0.898mm}}{}
+\@ifclasswith{\@baseclass}{legalpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{1.198mm}}{}
+\@ifclasswith{\@baseclass}{smallpocketpaper}{\setlength{\hscale}{0.571mm}\setlength{\vscale}{0.639mm}}{}
+\@ifclasswith{\@baseclass}{pocketpaper}{\setlength{\hscale}{0.642mm}\setlength{\vscale}{0.723mm}}{}
+\@ifclasswith{\@baseclass}{juvenilepaper}{\setlength{\hscale}{0.738mm}\setlength{\vscale}{0.740mm}}{}
+\@ifclasswith{\@baseclass}{smallphotopaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.572mm}}{}
+\@ifclasswith{\@baseclass}{photopaper}{\setlength{\hscale}{1.00mm}\setlength{\vscale}{0.707mm}}{}
+\@ifclasswith{\@baseclass}{appendixpaper}{\setlength{\hscale}{1.000mm}\setlength{\vscale}{0.505mm}}{}
+\@ifclasswith{\@baseclass}{cookpaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.740mm}}{}
+\@ifclasswith{\@baseclass}{illustratedpaper}{\setlength{\hscale}{0.905mm}\setlength{\vscale}{0.909mm}}{}
+\@ifclasswith{\@baseclass}{f24paper}{\setlength{\hscale}{0.762mm}\setlength{\vscale}{0.808mm}}{}
+\@ifclasswith{\@baseclass}{a4paperlandscape}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{0.707mm}}{}
+
+% Set the default page layout
+\RequirePackage[
+ paperwidth=210\hscale,
+ paperheight=297\vscale,
+]{geometry}
+
+% Command to choose among the three possible layouts
+\DeclareDocumentCommand{\pagelayout}{m}{%
+ \ifthenelse{\equal{margin}{#1}}{\marginlayout\marginfloatsetup}{}%
+ \ifthenelse{\equal{wide}{#1}}{\widelayout\widefloatsetup}{}%
+ \ifthenelse{\equal{fullwidth}{#1}}{\fullwidthlayout\widefloatsetup}{}%
+}
+
+% Layout #1: large margins
+\newcommand{\marginlayout}{%
+ \newgeometry{
+ top=27.4\vscale,
+ bottom=27.4\vscale,
+ inner=24.8\hscale,
+ textwidth=107\hscale,
+ marginparsep=6.2\hscale,
+ marginparwidth=47.7\hscale,
+ }%
+ \recalchead%
+}
+
+% Layout #2: small, symmetric margins
+\newcommand{\widelayout}{%
+ \newgeometry{
+ top=27.4\vscale,
+ bottom=27.4\vscale,
+ inner=24.8\hscale,
+ outer=24.8\hscale,
+ marginparsep=0mm,
+ marginparwidth=0mm,
+ }%
+ \recalchead%
+}
+
+% Layout #3: no margins and no space above or below the body
+\newcommand{\fullwidthpage}{%
+ \newgeometry{
+ top=0mm,
+ bottom=0mm,
+ inner=0mm,
+ outer=0mm,
+ marginparwidth=0mm,
+ marginparsep=0mm,
+ }%
+ \recalchead%
+}
+
+% Set the default page layout
+\AtBeginDocument{\pagelayout{margin}}
+
+%----------------------------------------------------------------------------------------
+% HEADERS AND FOOTERS
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{scrlayer-scrpage} % Customise head and foot regions
+
+% Set the header height to prevent a warning
+%\setlength{\headheight}{27.4\vscale}
+% Increase the space between header and text
+\setlength{\headsep}{11\vscale}
+
+% Define some LaTeX lengths used in the page headers
+\newlength{\headtextwidth}
+\newlength{\headmarginparsep}
+\newlength{\headmarginparwidth}
+\newlength{\headtotal} % This is the total width of the header
+\newcommand{\recalchead}{%
+ \setlength{\headtextwidth}{\textwidth}%
+ \setlength{\headmarginparsep}{\marginparsep}%
+ \setlength{\headmarginparwidth}{\marginparwidth}%
+ \setlength{\headtotal}{\headtextwidth+\headmarginparsep+\headmarginparwidth}%
+}
+
+\AtBeginDocument{% Recalculate the header-related lengths
+ \recalchead%
+}
+
+% Style with chapter number, chapter title, and page in the header (used throughout the document)
+\renewpagestyle{scrheadings}{%
+ {\smash{\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{%
+ \makebox[7\hscale][r]{\thepage}%
+ \makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
+ \makebox[\headtextwidth][l]{\leftmark}}}}%
+ {\smash{\makebox[0pt][l]{\makebox[\headtotal][r]{%
+ \makebox[\headtextwidth][r]{\hfill\rightmark}%
+ \makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
+ \makebox[7\hscale][l]{\thepage}}}}}%
+ {\smash{\makebox[0pt][l]{\makebox[\headtotal][r]{%
+ \makebox[\headtextwidth][r]{\hfill\rightmark}%
+ \makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
+ \makebox[7\hscale][l]{\thepage}}}}}%
+}{%
+ {}%
+ {}%
+ {}%
+}
+
+% Style with neither header nor footer (used for special pages)
+\renewpagestyle{plain.scrheadings}{%
+ {}%
+ {}%
+ {}%
+}{%
+ {}%
+ {}%
+ {}%
+}
+
+% Style with an empty header and the page number in the footer
+\newpagestyle{pagenum.scrheadings}{%
+ {}%
+ {}%
+ {}%
+}{%
+ {\makebox[\textwidth][r]{\thepage}}%
+ {\makebox[\textwidth][l]{\thepage}}%
+ {\makebox[\textwidth][l]{\thepage}}%
+}
+
+% Style with an empty header and the page number in the footer
+\newpagestyle{centeredpagenum.scrheadings}{%
+ {}%
+ {}%
+ {}%
+}{%
+ {\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{\hfill\thepage\hfill}}
+ {\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
+ {\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
+}
+
+% Command to print a blank page
+\newcommand\blankpage{%
+ \null%
+ \thispagestyle{empty}%
+ \newpage%
+}
+
+% Set the default page style
+\pagestyle{plain.scrheadings}
+
+%----------------------------------------------------------------------------------------
+% PARAGRAPH FORMATTING
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{ragged2e} % Required to achieve better ragged paragraphs
+\RequirePackage{setspace} % Required to easily set the space between lines
+\RequirePackage{hyphenat} % Hyphenation for special fonts
+\RequirePackage{microtype} % Improves character and word spacing
+\RequirePackage{nowidow} % Prevent widowing
+\RequirePackage{needspace} % Required to prevent page break right after a sectioning command
+\RequirePackage{xspace} % Better print trailing whitespace
+
+% TODO: recognize space/indent justified/raggedright class options
+
+% Settings for a normal paragraph
+\newcommand{\@body@par}{%
+ \justifying% Justify text
+ \singlespacing% Set the interline space to a single line
+ \frenchspacing% No additional space after periods
+ \normalfont% Use the default font
+ \normalsize% Use the default size
+}
+
+% Settings for paragraphs in the margins
+\newcommand{\@margin@par}{%
+ \justifying% justify text
+ \setlength{\RaggedRightParindent}{0em}% Suppress indentation
+ \setlength{\parindent}{0em}% Suppress indentation
+ \setlength{\parskip}{0.5pc}% Set the space between paragraphs
+ %\singlespacing% Set the space between lines
+ \frenchspacing% No additional space after periods
+ \normalfont% Use the default font
+ \footnotesize% Use a smaller size
+}
+
+% By default, use @body@par settings
+\@body@par
+
+%----------------------------------------------------------------------------------------
+% WIDE PARAGRAPHS
+%----------------------------------------------------------------------------------------
+
+% Environment for a wide paragraph
+\NewDocumentEnvironment{widepar}{}{%
+ \if@twoside%
+ \Ifthispageodd{%
+ \begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
+ }{%
+ \begin{addmargin}[-\marginparwidth-\marginparsep]{0cm}%
+ }%
+ \else%
+ \begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
+ \fi%
+}{%
+ \end{addmargin}%
+}
+
+% Environment for a full width paragraph
+\NewDocumentEnvironment{fullwidthpar}{}{%
+ \if@twoside%
+ \Ifthispageodd{%
+ \begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
+ }{%
+ \begin{addmargin}[-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth]{-\paperwidth+1in+\hoffset+\oddsidemargin+\marginparsep+\marginparwidth+\textwidth}%
+ }%
+ \else%
+ \begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
+ \fi%
+}{%
+ \end{addmargin}%
+}
+
+% Environment for a wide equation
+\NewDocumentEnvironment{wideequation}{}{%
+ \begin{widepar}%
+ \begin{equation}%
+}{%
+ \end{equation}%
+ \end{widepar}%
+}
+
+%----------------------------------------------------------------------------------------
+% FOOTNOTES, MARGINNOTES AND SIDENOTES
+%----------------------------------------------------------------------------------------
+
+\RequirePackage[section]{placeins} % Prevent floats to cross sections
+\extrafloats{100} % Require more floats
+
+\RequirePackage{marginnote} % Provides options for margin notes
+\RequirePackage{marginfix} % Make marginpars float freely
+\RequirePackage{sidenotes} % Use sidenotes
+\RequirePackage{chngcntr} % Reset counters at sections
+
+% TODO: see also page 440 of the KOMA-script guide
+\RequirePackage[
+ bottom,
+ symbol*,
+ hang,
+ flushmargin,
+ % perpage,
+ stable,
+]{footmisc} % Required to set up the footnotes
+\RequirePackage{footnotebackref} % Creates back references from footnotes to text
+
+% Fix the color of the footnote marker when back-referenced
+\patchcmd{\footref}{\ref}{\hypersetup{colorlinks=black}\ref}{}{}%
+% Workaround to fix back references
+\edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
+
+% FIXME: I am not able to choose the paragraph layout of footnotes, probably the footnotes package conflicts with scrbook.
+%\renewcommand{\footnotelayout}{\@margin@par}
+
+%----------------------------------------------------------------------------------------
+
+% Justify and format margin notes
+\renewcommand*{\raggedleftmarginnote}{} % Suppress left margin
+\renewcommand*{\raggedrightmarginnote}{} % Suppress right margin
+\renewcommand*{\marginfont}{\@margin@par} % Format marginnotes according to \@marginpar (see above)
+\renewcommand{\marginnotevadjust}{0.8\vscale} % Bring all marginnotes downwards a bit
+\marginposadjustment=0.1mm % Bring downwards also the marginpars
+\marginheightadjustment=.5cm % Bring downwards also the marginpars
+%\RequirePackage[marginnote=true]{snotez} % Provides options for sidenotes
+
+% Copied from snotez's \sidenote
+\def\kao@if@nblskip#1{%
+ \expandafter\ifx\@car#1\@nil*%
+ \expandafter\@firstoftwo%
+ \else%
+ \expandafter\@secondoftwo%
+ \fi%
+}
+
+% Command to detect whether we are inside an mdframed environment
+\newif\ifinfloat
+\AtBeginEnvironment{mdframed}{\infloattrue}
+\AtBeginEnvironment{minipage}{\infloattrue}
+
+\def\IfInFloatingEnvir{%
+ \ifinfloat%
+ \expandafter\@firstoftwo%
+ \else%
+ \expandafter\@secondoftwo%
+ \fi%
+}
+
+
+% Redefine the command to print marginnotes:
+% (a) the optional offset argument goes at the first position
+% (b) the offset can also be a multiple of baselineskip, like for snotez's \sidenote
+% Usage: \marginnote[<offset>]{Text of the note.}
+\let\oldmarginnote\marginnote%
+\RenewDocumentCommand\marginnote{ o m }{%
+ \IfNoValueOrEmptyTF{#1}{%
+ \IfInFloatingEnvir{%
+ \oldmarginnote{#2}%
+ }{%
+ \marginpar{\@margin@par#2}%
+ }%
+ }{%
+ \oldmarginnote{#2}[\kao@if@nblskip{#1}{\@cdr#1\@nil\baselineskip}{#1}]%
+ }%
+}
+
+% Initially set the counter to zero instead of 1, and update it before printing the sidenote.
+\setcounter{sidenote}{0}%
+\RenewDocumentCommand\sidenote{ o o +m }{%
+ \IfNoValueOrEmptyTF{#1}{%
+ \refstepcounter{sidenote}% This command has been moved here
+ }{%
+ }%
+ \sidenotemark[#1]%
+ \sidenotetext[#1][#2]{#3}%
+ \@sidenotes@multimarker%
+}
+
+% % Formatting sidenotes
+% \setsidenotes{
+% text-mark-format=\textsuperscript{\normalfont#1}, % Use a superscript for the marker in the text
+% note-mark-format=#1:, % Use a normal font for the marker in the margin; use a colon after the sidenote number
+% note-mark-sep=\enskip, % Set the space after the marker
+% }
+
+% Formatting sidenotes
+\RenewDocumentCommand\@sidenotes@thesidenotemark{ m }{%
+ \leavevmode%
+ \ifhmode%
+ \edef\@x@sf{\the\spacefactor}%
+ \nobreak%
+ \fi%
+ \hbox{\@textsuperscript{\normalfont#1}}%
+ \ifhmode%
+ \spacefactor\@x@sf%
+ \fi%
+ \relax%
+}%
+
+\RenewDocumentCommand\sidenotetext{ o o +m }{% number, offset, text
+ \IfNoValueOrEmptyTF{#1}{%
+ \marginnote[#2]{\thesidenote:\enskip#3}%
+ }{%
+ \marginnote[#2]{#1:\enskip#3}%
+ }%
+}
+
+%----------------------------------------------------------------------------------------
+% FIGURES, TABLES, LISTINGS AND CAPTIONS
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{graphicx} % Include figures
+\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio} % Improves figure scaling
+\RequirePackage{tikz} % Allows to draw custom shapes
+\RequirePackage{tikzpagenodes} % Allows to anchor tikz nodes to page elements
+\RequirePackage{booktabs} % Nicer tables
+\RequirePackage{multirow} % Cells occupying multiple rows in tables
+\RequirePackage{multicol} % Multiple columns in dictionary
+\RequirePackage{rotating} % Allows tables and figures to be rotated
+\RequirePackage{listings} % Print code listings
+%\RequirePackage{minted}
+\RequirePackage[hypcap=true]{caption} % Correctly placed anchors for hyperlinks
+% \RequirePackage{atbegshi}
+% \RequirePackage{perpage}
+\let\c@abspage\relax
+% \newcommand{\pp@g@sidenote}{}
+\RequirePackage{floatrow} % Set up captions of floats
+%\RequirePackage{dblfloatfix} % Better positioning of wide figures and tables
+\AtEndPreamble{\RequirePackage{scrhack}} % Make packages compatible with KOMA Script (must be loaded last: https://tex.stackexchange.com/questions/156240/latex-packages-minted-and-scrhack)
+
+% Improve the figure placing (see https://www.overleaf.com/learn/latex/Tips)
+\def\topfraction{.9}%
+\def\textfraction{0.35}%
+\def\floatpagefraction{0.8}%
+
+% Set the space between floats and main text
+\renewcommand\FBaskip{.4\topskip}%
+\renewcommand\FBbskip{\FBaskip}%
+
+% Tighten up space between displays (e.g., equations) and make symmetric (from tufte-latex)
+\setlength\abovedisplayskip{6pt plus 2pt minus 4pt}%
+\setlength\belowdisplayskip{6pt plus 2pt minus 4pt}%
+\abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@%
+\abovedisplayshortskip \z@ \@plus3\p@%
+\belowdisplayskip \abovedisplayskip%
+\belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@%
+
+\setlength\columnseprule{.4pt} % Set the width of vertical rules in tables
+
+\newlength{\kaomarginskipabove} % Specify the space above a marginfigure, margintable or marginlisting
+\newlength{\kaomarginskipbelow} % Specify the space below a marginfigure, margintable or marginlisting
+\setlength{\kaomarginskipabove}{3mm plus 2pt minus 2pt}
+\setlength{\kaomarginskipbelow}{3mm plus 2pt minus 2pt}
+
+% Environment to hold a margin figure (from the sidenotes package)
+% We redefine it here because we want to use our own caption formatting.
+\RenewDocumentEnvironment{marginfigure}{o}{%
+ \FloatBarrier%
+ \marginskip{\kaomarginskipabove}%
+ \begin{lrbox}{\@sidenotes@marginfigurebox}%
+ \begin{minipage}{\marginparwidth}%
+ \captionsetup{type=figure}%
+}{%
+ \end{minipage}%
+ \end{lrbox}%
+ \marginnote[#1]{\usebox{\@sidenotes@marginfigurebox}}%
+ \marginskip{\kaomarginskipbelow}%
+}
+
+% Environment to hold a margin table (from the sidenotes package)
+\RenewDocumentEnvironment{margintable}{o}{%
+ \FloatBarrier%
+ \marginskip{\kaomarginskipabove}%
+ \begin{lrbox}{\@sidenotes@margintablebox}%
+ \begin{minipage}{\marginparwidth}%
+ \captionsetup{type=table}%
+}{%
+ \end{minipage}%
+ \end{lrbox}%
+ \marginnote[#1]{\usebox{\@sidenotes@margintablebox}}%
+ \marginskip{\kaomarginskipbelow}%
+}
+
+% Environment to hold a margin listing
+\newsavebox{\@sidenotes@marginlistingbox}%
+\NewDocumentEnvironment{marginlisting}{o}{% The optional parameter is the vertical offset
+ \FloatBarrier%
+ \marginskip{\kaomarginskipabove}%
+ \begin{lrbox}{\@sidenotes@marginlistingbox}%
+ \begin{minipage}{\marginparwidth}%
+ \captionsetup{type=lstlisting}%
+}{%
+ \end{minipage}%
+ \end{lrbox}%
+ \marginnote[#1]{\usebox{\@sidenotes@marginlistingbox}}%
+ \marginskip{\kaomarginskipbelow}%
+}
+
+% Change the position of the captions
+\DeclareFloatSeparators{marginparsep}{\hskip\marginparsep}%
+
+% 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.
+\newtoggle{kaocaption}
+\AtBeginEnvironment{figure}{%
+ \let\oldcaption\caption%
+ \RenewDocumentCommand{\caption}{s o m}{%
+ \IfBooleanTF{#1}{%
+ \oldcaption*{#3}%
+ }{%
+ \IfValueTF{#2}{%
+ \oldcaption[#2]{#3}%
+ }{%
+ \oldcaption{#3}%
+ }%
+ }%
+ \toggletrue{kaocaption}%
+ }%
+}
+\AtEndEnvironment{figure}{%
+ \iftoggle{kaocaption}{%
+ }{%
+ \RawFloats%
+ \centering%
+ }%
+ \togglefalse{kaocaption}%
+}
+\AtBeginEnvironment{table}{%
+ \let\oldcaption\caption%
+ \RenewDocumentCommand{\caption}{s o m}{%
+ \IfBooleanTF{#1}{%
+ \oldcaption*{#3}%
+ }{%
+ \IfValueTF{#2}{%
+ \oldcaption[#2]{#3}%
+ }{%
+ \oldcaption{#3}%
+ }%
+ }%
+ \toggletrue{kaocaption}%
+ }%
+}
+\AtEndEnvironment{table}{%
+ \iftoggle{kaocaption}{%
+ }{%
+ \RawFloats%
+ \centering%
+ }%
+ \togglefalse{kaocaption}%
+}
+
+% Change the formatting of the captions
+\addtokomafont{captionlabel}{\bfseries} % Bold font for the figure label
+% Declare a new style to format the caption according to \@margin@par (see above)
+\DeclareCaptionFormat{margin}{\@margin@par #1#2#3}
+% Declare a new caption style for lstlistings
+\newsavebox\mycap
+\DeclareCaptionFormat{llap}{%
+ \begin{lrbox}{\mycap}%
+ \begin{minipage}{\marginparwidth}%
+ \@margin@par #1:#2#3%
+ \end{minipage}%
+ \end{lrbox}%
+ \marginnote[0.2cm]{\usebox\mycap}%
+}
+% Set the global caption style
+\captionsetup{
+ format=margin, % Use the style previously declared
+ strut=no,%
+ %hypcap=true, % Links point to the top of the figure
+ singlelinecheck=false,%
+ %width=\marginparwidth,
+ indention=0pt, % Suppress indentation
+ parindent=0pt, % Suppress space between paragraphs
+ aboveskip=6pt, % Increase the space between the figure and the caption
+ belowskip=6pt, % Increase the space between the caption and the table
+}
+
+% Needed to have continued figures and tables (https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures_in_multiple_parts)
+\DeclareCaptionLabelFormat{cont}{#1~#2\alph{ContinuedFloat}}
+\captionsetup[ContinuedFloat]{labelformat=cont}
+
+% Captions for the 'margin' layout
+\NewDocumentCommand{\marginfloatsetup}{}{%
+\if@twoside%
+ \floatsetup[figure]{% Captions for figures
+ margins=hangoutside,% Put captions in the margins
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={bottom,outside},%
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[widefigure]{% Captions for wide figures
+ margins=hangoutside,% Put captions below the figure
+ facing=yes,%
+ capposition=bottom%
+ }%
+ \floatsetup[table]{% Captions for tables
+ margins=hangoutside,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,outside},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[widetable]{% Captions for wide tables
+ margins=hangoutside,% Put captions above the table
+ facing=yes,%
+ capposition=above%
+ }%
+ \floatsetup[longtable]{% Captions for longtables
+ margins=raggedright,% Overwrite the hangright setting from the `table' environment
+ %LTcapwidth=table,% Set the width of the caption equal to the table's
+ }%
+ \floatsetup[lstlisting]{% Captions for lstlistings
+ margins=hangoutside,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,outside},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[listing]{% Captions for listings (minted package)
+ margins=hangoutside,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,outside},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,%Width of the figure equal to the width of the text
+ }%
+ \captionsetup*[lstlisting]{%
+ format=llap,%
+ labelsep=space,%
+ singlelinecheck=no,%
+ belowskip=-0.6cm,%
+ }%
+\else%
+ \floatsetup[figure]{% Captions for figures
+ margins=hangright,% Put captions in the margins
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={bottom,right},%
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[widefigure]{% Captions for wide figures
+ margins=hangright,% Put captions below the figure
+ facing=no,%
+ capposition=bottom%
+ }%
+ \floatsetup[table]{% Captions for tables
+ margins=hangright,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,right},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[widetable]{% Captions for wide tables
+ margins=hangright,% Put captions above the table
+ facing=no,%
+ capposition=above%
+ }%
+ \floatsetup[longtable]{% Captions for longtables
+ margins=raggedright,% Overwrite the hangright setting from the `table' environment
+ %LTcapwidth=table,% Set the width of the caption equal to the table's
+ }%
+ \floatsetup[lstlisting]{% Captions for lstlisting
+ margins=hangright,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,right},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \floatsetup[listing]{% Captions for listing (minted package)
+ margins=hangright,% Put captions in the margin
+ facing=yes,%
+ capposition=beside,%
+ capbesideposition={top,right},%
+ %capbesideposition=outside,
+ capbesideframe=yes,%
+ capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
+ capbesidesep=marginparsep,%
+ floatwidth=\textwidth,% Width of the figure equal to the width of the text
+ }%
+ \captionsetup*[lstlisting]{%
+ format=llap,%
+ labelsep=space,%
+ singlelinecheck=no,%
+ belowskip=-0.6cm,%
+ }%
+\fi%
+}
+
+% Captions for the 'wide' layout
+\NewDocumentCommand{\widefloatsetup}{}{%
+ \floatsetup[figure]{ % Captions for figures
+ capposition=bottom,%
+ margins=centering,%
+ floatwidth=\textwidth%
+ }
+ \floatsetup[widefigure]{ % Captions for wide figures
+ margins=hangoutside, % Put captions below the figure
+ facing=yes,%
+ capposition=bottom%
+ }
+ \floatsetup[table]{ % Captions for tables
+ capposition=above,%
+ margins=centering,%
+ floatwidth=\textwidth%
+ }
+ \floatsetup[widetable]{ % Captions for wide tables
+ margins=hangoutside, % Put captions above the table
+ facing=yes,%
+ capposition=above%
+ }
+ \floatsetup[lstlisting]{ % Captions for lstlistings
+ capposition=above,%
+ margins=centering,%
+ floatwidth=\textwidth%
+ }
+ \floatsetup[listing]{ % Captions for listings (minted package)
+ capposition=above,%
+ margins=centering,%
+ floatwidth=\textwidth%
+ }
+ \captionsetup*[lstlisting]{% Captions style for lstlistings
+ %format=margin,%
+ labelsep=colon,%
+ strut=no,%
+ singlelinecheck=false,%
+ indention=0pt,%
+ parindent=0pt,%
+ aboveskip=6pt,%
+ belowskip=6pt,%
+ belowskip=-0.1cm%
+ }
+}
+
+%----------------------------------------------------------------------------------------
+% TOC, LOF & LOT
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{tocbasic}
+
+% Show an entry for the TOC in the TOC
+\setuptoc{toc}{totoc}
+
+% Choose the levels in table of contents
+\setcounter{tocdepth}{\subsectiontocdepth}
+
+% Define the style for toc entries
+\@ifpackagelater{scrbase}{2019/10/11}{%
+ \DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes,pagenumberwidth=2.1em]{tocline}{figure,table}%
+ \DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
+ \DeclareTOCStyleEntries[pagenumberwidth=2.5em]{tocline}{chapter,part}%
+ \DeclareTOCStyleEntries[pagenumberwidth=2.1em]{tocline}{subsubsection,subsection,section}%
+}{%
+ \DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes]{tocline}{figure,table}%
+ \DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
+}
+
+% Define the names for the headings
+% \renewcaptionname{english}{\contentsname}{Detailed Contents}
+% \renewcaptionname{english}{\listfigurename}{Figures}
+% \renewcaptionname{english}{\listtablename}{Tables}
+% \newcaptionname{english}{\listtheoremname}{Theorems}
+
+%----------------------------------------------------------------------------------------
+% MARGIN TOC
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{etoc} % Required to insert local tables of contents
+
+\newcounter{margintocdepth} % Set the depth of the margintoc
+\setcounter{margintocdepth}{\subsectiontocdepth}
+
+\newlength{\mtocshift} % Length of the vertical offset used for margin tocs
+\setlength{\mtocshift}{1\vscale}% \mtocshift is overridden by \setchapterstyle
+
+% Optional title for margintoc (by hCarlFelix,
+% https://github.com/fmarotta/kaobook/issues/101)
+% We want to create an additional entries in the toc which is to be used for the margintoc
+% We define these as mtocsection and mtocsubsection for section and subsection`
+\newcommand{\mtocsection}[1]{
+ \addcontentsline{toc}{mtocsection}{%
+ \ifnum\value{secnumdepth}>0 \protect\numberline{\thesection}%
+ \else \protect\nonumberline%
+ \fi #1}%
+}
+\newcommand{\mtocsubsection}[1]{
+ \addcontentsline{toc}{mtocsubsection}{%
+ \ifnum\value{secnumdepth}>1 \protect\numberline{\thesubsection}%
+ \else \protect\nonumberline%
+ \fi #1}%
+}
+
+% Next, we redefine \section and \subsection so that they accept an additional parameter
+% \section[alternative-title-for-toc]{title-as-written-in-text}[alternative-title-for-margintoc]
+
+% Adapted from Frank Mittelbach's answer at Stackexchange
+% https://tex.stackexchange.com/a/109560/226693
+\let\oldsection\section % save the old command
+\let\oldsubsection\subsection % save the old command
+
+\RenewDocumentCommand\section{s o m o}{%
+ \IfBooleanTF{#1}{%
+ \oldsection*{#3}
+ \IfNoValueF{#2}{% if TOC arg is given create a TOC entry
+ \addxcontentsline{toc}{section}[\thesection]{#2}%
+ }%
+ }{% no star given
+ \IfNoValueTF{#2}{%
+ \oldsection{#3}%
+ }{% no TOC arg
+ \oldsection[#2]{#3}%
+ }%
+ \IfNoValueTF{#4}{% optional label given, if not we do nothing
+ \mtocsection{#3}%
+ }{%
+ \mtocsection{#4}%
+ }%
+ }%
+}
+
+\RenewDocumentCommand\subsection{s o m o}{
+ \IfBooleanTF{#1}{%
+ \oldsubsection*{#3}%
+ \IfNoValueF{#2}{% if TOC arg is given create a TOC entry
+ \addxcontentsline{toc}{subsection}[\thesubsection]{#2}%
+ }%
+ }{% no star given
+ \IfNoValueTF{#2}{%
+ \oldsubsection{#3}%
+ }{% no TOC arg
+ \oldsubsection[#2]{#3}%
+ }%
+ \IfNoValueTF{#4}{% optional label given, if not we do nothing
+ \mtocsubsection{#3}%
+ }{%
+ \mtocsubsection{#4}%
+ }%
+ }%
+}
+
+\etocsetlevel{mtocsection}{6}% dummy sectioning level
+\etocsetlevel{mtocsubsection}{6}% dummy sectioning level
+
+% Command to print a table of contents in the margin
+\NewDocumentCommand{\margintoc}{O{\mtocshift}}{ % The first parameter is the vertical offset; by default it is \mtocshift
+ \begingroup%
+ % 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.
+ % Adapted from https://tex.stackexchange.com/a/133559/226693
+ \etocsetlevel{section}{6}
+ \etocsetlevel{subsection}{6}
+ \etocsetlevel{mtocsection}{1}
+ \etocsetlevel{mtocsubsection}{2}
+
+ % Define default widths
+ \def\margintocnumwidth{-.8mm}%
+ \def\margintocpagenumwidth{8pt}%
+ \setlength{\RaggedRightParfillskip}{0pt}
+
+ % Dry run to get the needed widths
+ \etocsetstyle{mtocsection}%
+ {}%
+ {\setbox0\hbox{\usekomafont{section}\small\etocthenumber\kern.8mm}%%
+ \setbox1\hbox{\usekomafont{section}\small\etocthepage}}%
+ {\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%%
+ \ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
+ {}%
+ \etocsetstyle{mtocsubsection}%
+ {}%
+ {\setbox0\hbox{\usekomafont{section}\small\etocthenumber\kern.8mm}%
+ \setbox1\hbox{\usekomafont{section}\small\etocthepage}}%
+ {\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%
+ \ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
+ {}%
+ \etocsetstyle{subsubsection}%
+ {}%
+ {}%
+ {}%
+ {}%
+ \etocsetstyle{paragraph}%
+ {}%
+ {}%
+ {}%
+ {}%
+ \etocsettocstyle{}{%
+ \global\let\margintocnumwidth\margintocnumwidth%
+ \global\let\margintocpagenumwidth\margintocpagenumwidth%
+ }%
+ \localtableofcontents%
+
+ % Set the style for section entries
+ \etocsetstyle{mtocsection}%
+ {\parindent 0pt \parskip 2.5pt \parfillskip 0pt \RaggedRight}%
+ {\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth} %
+ {\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\etocnumber}}\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\etocpage}}\par}%
+ {}%
+ % Set the style for subsection entries
+ \etocsetstyle{mtocsubsection}%
+ {\parindent 0pt \parskip 0pt \parfillskip 0pt \RaggedRight}%
+ {\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth}%
+ {\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\etocnumber}}\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\etocpage}}\par}%
+ {\parskip 2.5pt}%
+ % Set the global style of the toc
+ \etocsettocstyle{\usekomafont{section}\small}{}%
+ \etocsetnexttocdepth{\themargintocdepth}%
+ % Print the table of contents in the margin
+ \marginnote[#1]{\localtableofcontents}%
+ \FloatBarrier%
+ \endgroup%
+}
+
+%----------------------------------------------------------------------------------------
+% ENCODING AND FONTS
+%----------------------------------------------------------------------------------------
+
+% https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation
+% Introduce a command to find out whether the compiler is XeTeX or LuaTeX
+\newif\ifxetexorluatex
+\ifxetex
+ \xetexorluatextrue
+\else
+ \ifluatex
+ \xetexorluatextrue
+ \else
+ \xetexorluatexfalse
+ \fi
+\fi
+
+\ifxetexorluatex
+ \RequirePackage{amssymb} % Must be loaded before unicode-math
+ \RequirePackage[force]{filehook} % Fixes an error
+ \RequirePackage{unicode-math} % Math fonts in xetexorluatex
+ \setromanfont[
+ Scale=1.04
+ ]{Libertinus Serif}
+ \setsansfont[
+ Scale=1
+ ]{Libertinus Sans}
+ \setmonofont[
+ Scale=.89
+ ]{Liberation Mono}
+ \setmathfont{Libertinus Math}
+ \ifluatex
+ \else
+ \RequirePackage{morewrites} % Fix some errors related to floats
+ \fi
+\else
+ \RequirePackage[utf8]{inputenc} % utf8 encoding in the input (.tex) file
+ \RequirePackage[T1]{fontenc} % utf8 encoding in the output (.pdf) file
+
+ \RequirePackage{amssymb} % Math symbols, including \blacktriangleright, used for bullets
+ \RequirePackage[scaled=.97,helvratio=.93,p,theoremfont]{newpxtext} % Serif palatino font
+ % \RequirePackage[vvarbb,smallerops,bigdelims]{newpxmath} % Math palatino font
+ \RequirePackage[scaled=.85]{beramono} % Monospace font
+ % \RequirePackage[scr=rsfso,cal=boondoxo]{mathalfa} % Mathcal from STIX, unslanted a bit
+ \RequirePackage{morewrites} % Fix some errors related to floats
+\fi
+
+% When using the Palatino (newpxtext) font, it is better to use a
+% slightly larger stretch.
+%\setstretch{1.10}
+\linespread{1.07} % Give Palatino more leading (space between lines)
+
+%----------------------------------------------------------------------------------------
+% HYPERREFERENCES
+%----------------------------------------------------------------------------------------
+
+\RequirePackage{hyperref} % Required for hyperlinks
+\RequirePackage{bookmark} % Required for pdf bookmarks
+
+\PassOptionsToPackage{hyphens}{url} % Break long URLs and use hyphens to separate the pieces
+
+\hypersetup{ % Set up hyperref options
+ unicode, % Use unicode for links
+ pdfborder={0 0 0}, % Suppress border around pdf
+ %xetex,
+ %pagebackref=true,
+ %hyperfootnotes=false, % We already use footmisc
+ bookmarksdepth=section,
+ bookmarksopen=true, % Expand the bookmarks as soon as the pdf file is opened
+ %bookmarksopenlevel=4,
+ linktoc=all, % Toc entries and numbers links to pages
+ breaklinks=true,
+ colorlinks=true,
+ %allcolors=DarkGreen,
+ citecolor = DarkOrange,
+ linkcolor = Blue,
+ %pagecolor = Blue,
+ urlcolor = OliveGreen,
+}
+
+% Define a new color for the footnote marks
+\def\@footnotecolor{black}
+\define@key{Hyp}{footnotecolor}{%
+ \HyColor@HyperrefColor{#1}\@footnotecolor%
+}
+\def\@footnotemark{%
+ \leavevmode
+ \ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
+ \stepcounter{Hfootnote}%
+ \global\let\Hy@saved@currentHref\@currentHref
+ \hyper@makecurrent{Hfootnote}%
+ \global\let\Hy@footnote@currentHref\@currentHref
+ \global\let\@currentHref\Hy@saved@currentHref
+ \hyper@linkstart{footnote}{\Hy@footnote@currentHref}%
+ \@makefnmark
+ \hyper@linkend
+ \ifhmode\spacefactor\@x@sf\fi
+ \relax
+}
+
+\let\oldthanks\thanks
+\renewcommand\thanks[1]{%
+ \label{bhfn:0}%
+ \oldthanks{#1}%
+}
+
+% Adjust the colour of the footnotes marks using the colour defined above
+\renewcommand\@makefntext[1]{%
+ \renewcommand\@makefnmark{%
+ \mbox{\textsuperscript{\normalfont%
+ \hyperref[\BackrefFootnoteTag]{%
+ \color{\@footnotecolor}{\@thefnmark}%
+ }}\,%
+ }%
+ }%
+ \BHFN@OldMakefntext{#1}%
+}
+
+\providecommand*{\toclevel@mtocsection}{0}
+\providecommand*{\toclevel@mtocsubsection}{0}
+
+%----------------------------------------------------------------------------------------
+% COLOURS
+%----------------------------------------------------------------------------------------
+
+% Uncomment to have coloured headings
+%\addtokomafont{title}{\color{Maroon}}
+%\addtokomafont{part}{\color{Maroon}}
+%\addtokomafont{chapter}{\color{Maroon}}
+%\addtokomafont{section}{\color{Maroon}}
+%\addtokomafont{subsection}{\color{Maroon}}
+%\addtokomafont{subsubsection}{\color{Maroon}}
+%\addtokomafont{paragraph}{\color{Maroon}}
+%\addtokomafont{captionlabel}{\color{Blue}}
+%\addtokomafont{pagenumber}{\color{Maroon}}
+
+\hypersetup{
+ %anchorcolor=Red,
+ %citecolor=DarkOrange!70!black,
+ citecolor=OliveGreen,
+ filecolor=OliveGreen,
+ %linkcolor=Blue,
+ linkcolor=Black,
+ %menucolor=Red,
+ %runcolor=Red,
+ urlcolor=OliveGreen,
+}
+
+%----------------------------------------------------------------------------------------
+% ITEMS
+%----------------------------------------------------------------------------------------
+
+\renewcommand{\labelitemi}{\small$\blacktriangleright$}
+\renewcommand{\labelitemii}{\textbullet}
+\RequirePackage{enumitem}
+\setlist[itemize]{noitemsep}
+\setlist[enumerate]{noitemsep}
+\setlist[description]{noitemsep}
+
+%----------------------------------------------------------------------------------------
+% SIMPLE BOXED ENVIRONMENT
+%----------------------------------------------------------------------------------------
+
+% kaobox (while tcolorbox may be more rich, I find it too complicated so I prefer mdframed)
+\RequirePackage{tikz}
+\RequirePackage[framemethod=TikZ]{mdframed}
+
+%\mdfsetup{skipabove=\topskip,skipbelow=0pt}
+\mdfdefinestyle{kaoboxstyle}{
+ skipabove=1.5\topskip,
+ skipbelow=.5\topskip,
+ rightmargin=0pt,
+ leftmargin=0pt,
+ %innertopmargin=3pt,
+ %innerbottommargin=3pt,
+ innerrightmargin=7pt,
+ innerleftmargin=7pt,
+ topline=false,
+ bottomline=false,
+ rightline=false,
+ leftline=false,
+ %linewidth=1pt,
+ %roundcorner=0pt,
+ %font={},
+ %frametitlefont={},
+ frametitlerule=true,
+ linecolor=black,
+ %backgroundcolor=LightBlue,
+ fontcolor=black,
+ %frametitlebackgroundcolor=LightBlue,
+}
+
+\newmdenv[
+ style=kaoboxstyle,
+ backgroundcolor=RoyalBlue!25!White,
+ frametitlebackgroundcolor=RoyalBlue!25!White,
+]{kaobox}
+
+%----------------------------------------------------------------------------------------
+% ENVIRONMENT WITH A COUNTER
+%----------------------------------------------------------------------------------------
+
+\newenvironment{kaocounter}{
+ \refstepcounter{kaocounter}
+ \begin{kaobox}[frametitle=Comment~\thekaocounter\autodot]
+}{
+ \end{kaobox}
+}
+
+\newcounter{kaocounter}
+\counterwithin{kaocounter}{section}
+\newcommand*{\kaocounterformat}{% Format for the caption
+ Comment~\thekaocounter\csname autodot\endcsname}
+\newcommand*{\fnum@kaocounter}{\kaocounterformat}
+
+
+%----------------------------------------------------------------------------------------
+% FLOATING ENVIRONMENT WITH TOC ENTRIES
+%----------------------------------------------------------------------------------------
+
+\newenvironment{kaofloating}{%
+ \@float{kaofloating}%
+}{%
+ \end@float%
+}
+
+\newcommand*{\fps@floatingbox}{tbph}
+\newcommand*{\ftype@floatingbox}{5}
+\newcommand*{\floatingboxformat}{%
+ Insight~\thefloatingbox\csname autodot\endcsname}
+\newcommand*{\fnum@floatingbox}{\floatingboxformat}
+\newcommand*{\ext@floatingbox}{loi}
+
+\addtotoclist[float]{loi}
+\newcommand*{\listofloiname}{List of Insights}
+\newcommand*{\l@floatingbox}{\l@figure}
+\newcommand*{\listofinsights}{\listoftoc{loi}}
+
+%----------------------------------------------------------------------------------------
+% ADDITIONAL PACKAGES
+%----------------------------------------------------------------------------------------
+
+% Productivity
+\RequirePackage{pdfpages} % Insert PDF pages
+\RequirePackage{subfiles} % Adopt a modular structure
+\RequirePackage{todonotes} % Add useful notes in the margins
+
+% Listings code
+\RequirePackage{listings} % Code
+%\RequirePackage{minted}
+
+\definecolor{listingkeywords}{rgb}{0.0, 0.5, 0.0}
+\definecolor{listingidentifiers}{rgb}{0, 0, 0}
+\definecolor{listingcomments}{rgb}{0.25, 0.5, 0.5}
+\definecolor{listingstrings}{rgb}{0.73, 0.13, 0.13}
+\definecolor{listingnumbers}{rgb}{0.25, 0.25, 0.25}
+\lstdefinestyle{kaolst}{
+ aboveskip=0.7\topskip,
+ belowskip=0.1\topskip,
+ basicstyle=\small\ttfamily,
+ commentstyle=\color{listingcomments}\itshape,
+ keywordstyle=\color{listingkeywords}\bfseries,
+ numberstyle=\scriptsize\color{listingnumbers}\ttfamily,
+ stringstyle=\color{listingstrings},
+ identifierstyle=\color{listingidentifiers},
+ backgroundcolor=\color{White},
+ breakatwhitespace=false,
+ breaklines=true,
+ captionpos=t,
+ keepspaces=true,
+ showspaces=false,
+ showstringspaces=false,
+ showtabs=false,
+ numbers=left,
+ numbersep=1em,
+ %frame=lines,
+ frame=l,
+ framerule=.7pt,
+ tabsize=4,
+ defaultdialect=[LaTeX]Tex,
+}
+\lstdefinestyle{kaolstplain}{
+ aboveskip=0.6\topskip,
+ belowskip=-0.1\topskip,
+ basicstyle=\small\ttfamily,
+ commentstyle=\color{listingcomments}\itshape,
+ keywordstyle=\color{listingkeywords}\bfseries,
+ numberstyle=\scriptsize\color{listingnumbers}\ttfamily,
+ stringstyle=\color{listingstrings},
+ identifierstyle=\color{listingidentifiers},
+ backgroundcolor=\color{White},
+ breakatwhitespace=false,
+ breaklines=true,
+ captionpos=b,
+ keepspaces=true,
+ showspaces=false,
+ showstringspaces=false,
+ showtabs=false,
+ numbers=none,
+ frame=none,
+ tabsize=4,
+ defaultdialect=[LaTeX]Tex,
+}
+\lstset{style=kaolst}
+
+% Verbatim
+%\RequirePackage{fancyvrb} % Customization of verbatim environments
+%\fvset{fontsize=\normalsize} % Change here the font size of all
+%verbatim \preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
+
+% Algorithms
+\RequirePackage[ruled, vlined]{algorithm2e} % Algorithms
+% \RequirePackage[linesnumbered, ruled, vlined]{algorithm2e} % Algorithms
+
+% Special gliphs
+\RequirePackage{ccicons} % Creative Commons icons
+\RequirePackage{metalogo} % XeTeX logo
+
+% Index, glossary and nomenclature
+\RequirePackage{imakeidx}
+\RequirePackage[xindy,toc,numberedsection=nameref]{glossaries}
+\RequirePackage[intoc]{nomencl}
+
+% Commands to print specific words always in the same way
+% TODO: in \Command, automatically escape braces {} and replace backslashes with \textbackslash
+\newcommand{\Class}[1]{\texttt{#1}}
+\newcommand{\Package}[1]{\texttt{#1}}
+\newcommand{\Option}[1]{\texttt{#1}}
+\newcommand{\Command}[1]{\texttt{\textbackslash#1}}
+\newcommand{\Environment}[1]{\texttt{#1}}
+\newcommand{\Path}[1]{\texttt{#1}}
+
+% Print latin words in italics (The xspace package is required but we already loaded it in the class)
+\newcommand{\hairsp}{\hspace{1pt}} % Command to print a very short space
+\newcommand{\invitro}{\textit{in vitro}\xspace}
+\newcommand{\invivo}{\textit{in vivo}\xspace}
+\newcommand{\cis}{\textit{cis}\xspace}
+\newcommand{\trans}{\textit{trans}\xspace}
+\newcommand{\etal}{\textit{et al.}\xspace}
+\newcommand{\denovo}{\textit{de novo}\xspace}
+\newcommand{\adhoc}{\textit{ad hoc}\xspace}
+\newcommand{\etcetera}{\textit{et cetera}\xspace}
+\newcommand{\etc}{\textit{etc.}\xspace}
+\newcommand{\ie}{\textit{i.\nobreak\hairsp{}e.}\xspace}
+\newcommand{\eg}{\textit{e.\nobreak\hairsp{}g.}\xspace}
+\newcommand{\vs}{\textit{vs}\xspace}
+\newcommand{\cfr}{\textit{cfr.}\xspace}
+
+% Tables
+\newcommand{\na}{\quad--} % Used in tables for N/A cells
+\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
+\newcommand{\hangstar}{\makebox[0pt][l]{*}} % Create asterisks in tables which take up no horizontal space - this improves column spacing
+
+% A command to print the current month and year (from tufte-latex)
+\newcommand{\monthyear}{\ifcase\month\or January\or February\or March\or
+April\or May\or June\or July\or August\or September\or October\or
+November\or December\fi\space\number\year}
diff --git a/kaobiblio.sty b/kaobiblio.sty
@@ -0,0 +1,421 @@
+\ProvidesPackage{kaobiblio}
+
+\RequirePackage{etoolbox} % Easy programming to modify TeX stuff
+\RequirePackage{perpage} % Reset counters
+\RequirePackage{iflang} % Check the document language
+\RequirePackage{xparse} % Parse arguments for macros
+\RequirePackage{xstring} % Parse strings
+\RequirePackage{hyperref} % Required for hyperlinks
+\RequirePackage{kvoptions} % Handle package options
+
+\SetupKeyvalOptions{
+ family = kaobiblio,
+ prefix = kaobiblio@
+}
+
+\DeclareBoolOption{addspace}
+\DeclareBoolOption{linkeverything}
+
+% Choose the default options, which will be overwritten by the options
+% passed to this package.
+\PassOptionsToPackage{
+ %style=numeric-comp,
+ %citestyle=authortitle-icomp,
+ citestyle=numeric-comp,
+ %bibstyle=authoryear,
+ bibstyle=numeric,
+ sorting=none,
+ %sorting=nyt,
+ %sortcites=true,
+ %autocite=footnote,
+ backend=biber, % Compile the bibliography with biber
+ hyperref=true,
+ backref=true,
+ citecounter=true,
+ pagetracker=true,
+ citetracker=true,
+ ibidtracker=context,
+ autopunct=true,
+ autocite=plain,
+}{biblatex}
+
+% Pass the unknown options to biblatex, overwriting the previous settings. Avoid passing the kao-specific options.
+\DeclareDefaultOption{%
+ \IfBeginWith{\CurrentOption}{addspace}{}{%
+ \IfBeginWith{\CurrentOption}{linkeverything}{}{%
+ \PassOptionsToPackage{\CurrentOption}{biblatex}%
+ }}%
+}
+
+% Process the options
+\ProcessKeyvalOptions{kaobiblio}
+
+% Load biblatex
+\RequirePackage{biblatex}
+
+% Remove some unwanted entries from the bibliography
+\AtEveryBibitem{
+ \clearfield{issn}
+ \clearfield{isbn}
+ \clearfield{archivePrefix}
+ \clearfield{arxivId}
+ \clearfield{pmid}
+ \clearfield{eprint}
+ \ifentrytype{online}{}{\ifentrytype{misc}{}{\clearfield{url}}}
+ \ifentrytype{book}{\clearfield{doi}}{}
+}
+
+% Convert months to integers
+\DeclareSourcemap{
+ \maps[datatype=bibtex]{
+ \map[overwrite]{
+ \step[fieldsource=month, match={jan}, replace=${1}]
+ \step[fieldsource=month, match={feb}, replace=${2}]
+ \step[fieldsource=month, match={mar}, replace=${3}]
+ \step[fieldsource=month, match={apr}, replace=${4}]
+ \step[fieldsource=month, match={may}, replace=${5}]
+ \step[fieldsource=month, match={jun}, replace=${6}]
+ \step[fieldsource=month, match={jul}, replace=${7}]
+ \step[fieldsource=month, match={aug}, replace=${8}]
+ \step[fieldsource=month, match={sep}, replace=${9}]
+ \step[fieldsource=month, match={oct}, replace=${10}]
+ \step[fieldsource=month, match={nov}, replace=${11}]
+ \step[fieldsource=month, match={dec}, replace=${12}]
+ }
+ }
+}
+
+%----------------------------------------------------------------------------------------
+% BACK REFERENCES
+%----------------------------------------------------------------------------------------
+
+% Check if a string is in a comma-separated list
+\newcommand\IfStringInList[2]{\IfSubStr{,#2,}{,#1,}}
+
+% Set the language-specific back reference strings
+% #LANGUAGE
+\@ifpackageloaded{polyglossia}{%
+ \IfLanguageName{danish}{%
+ \DefineBibliographyStrings{danish}{%
+ backrefpage = {citeret på side},
+ backrefpages = {citeret på sider},
+ }
+ }{}
+ \IfLanguageName{english}{%
+ \DefineBibliographyStrings{english}{%
+ backrefpage = {cited on page},
+ backrefpages = {cited on pages},
+ }
+ }{}
+ \IfLanguageName{italian}{%
+ \DefineBibliographyStrings{italian}{%
+ backrefpage = {citato a pag.},
+ backrefpages = {citato a pagg.},
+ }
+ }{}
+}{
+ \@ifpackageloaded{babel}{%
+ \IfStringInList{danish}{\bbl@loaded}{%
+ \DefineBibliographyStrings{danish}{%
+ backrefpage = {citeret på side},
+ backrefpages = {citeret på sider},
+ }
+ }{}
+ \IfStringInList{english}{\bbl@loaded}{%
+ \DefineBibliographyStrings{english}{%
+ backrefpage = {cited on page},
+ backrefpages = {cited on pages},
+ }
+ }{}
+ \IfStringInList{italian}{\bbl@loaded}{%
+ \DefineBibliographyStrings{italian}{%
+ backrefpage = {citato a pag.},
+ backrefpages = {citato a pagg.},
+ }
+ }{}
+ }{}
+}
+
+%----------------------------------------------------------------------------------------
+% CITATION COMMANDS
+%----------------------------------------------------------------------------------------
+
+% Command to format the marginnote created for cited items
+\NewDocumentCommand{\formatmargincitation}{m}{% The parameter is a single citation key
+ \parencite{#1}: \citeauthor*{#1} (\citeyear{#1}), \citetitle{#1}%
+}
+
+% Command to format the marginnote created for supercited items
+\NewDocumentCommand{\formatmarginsupercitation}{m}{% The parameter is a single citation key
+ \supercite{#1} \citeauthor*{#1} (\citeyear{#1})%
+}
+
+% The following command needs to be redefined every time \sidecite is called in order for \DeclareCiteCommand's wrapper to work correctly
+\NewDocumentCommand{\kaobiblio@marginnote}{m}{%
+ \marginnote{#1}%
+}
+
+% biblatex-like commands that also print a citation in the margin
+% Usage:
+ % First optional argument is always vertical shift and must be given as an (empty) argument when using following a postnote and/or prenote
+ % Second optional argument is always the postnote if the third argument isn't specified or is the prenote if the third argument is specified (same pattern as the biblatex commands)
+ % Third optional argument is always the postnote
+ % Mandatory argument is always the citation key(s)
+
+% Command to \cite and print a citation in the margin
+% First optional argument: vertical shift
+% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
+% Third optional argument: postnote
+% Mandatory argument: citation key
+\NewDocumentCommand{\sidecite}{o o o m}{%
+ \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
+ \marginnote[#1]{##1}%
+ }%
+ \DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
+ }{%
+ \formatmargincitation{\thefield{entrykey}}%
+ }{%
+ \\% separator between multiple citations
+ }{%
+ }%
+ % With this we print the marker in the text and add the item to the bibliography at the end
+ \IfNoValueOrEmptyTF{#2}%
+ {\def\@tempa{\cite{#4}\kaobiblio@sidecite{#4}}}%
+ {\IfNoValueOrEmptyTF{#3}%
+ {\IfNoValueTF{#3}%
+ {\def\@tempa{\cite[#2]{#4}\kaobiblio@sidecite{#4}}}%
+ {\def\@tempa{\cite[#2][]{#4}\kaobook@sidecite{#4}}}% postnote is empty, so pass empty postnote
+ }%
+ {\def\@tempa{\cite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
+ }%
+ \ifkaobiblio@addspace%
+ \unskip~\@tempa%
+ \else%
+ \@tempa%
+ \fi%
+}
+
+% Command to \supercite and print a citation in the margin
+% First optional argument: vertical shift
+% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
+% Third optional argument: postnote
+% Mandatory argument: citation key
+\NewDocumentCommand{\sidesupercite}{o o o m}{%
+ \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
+ \marginnote[#1]{##1}%
+ }%
+ \DeclareCiteCommand{\kaobiblio@sidesupercite}[\kaobiblio@marginnote]{%
+ }{%
+ \formatmarginsupercitation{\thefield{entrykey}}%
+ }{%
+ \\% separator between multiple citations
+ }{%
+ }%
+ % With this we print the marker in the text and add the item to the bibliography at the end
+ \IfNoValueOrEmptyTF{#2}%
+ {\def\@tempa{\supercite{#4}\kaobiblio@sidesupercite{#4}}}%
+ {\IfNoValueOrEmptyTF{#3}%
+ {\IfNoValueTF{#3}%
+ {\def\@tempa{\supercite[#2]{#4}\kaobiblio@sidesupercite{#4}}}%
+ {\def\@tempa{\supercite[#2][]{#4}\kaobook@sidesupercite{#4}}}% postnote is empty, so pass empty postnote
+ }%
+ {\def\@tempa{\supercite[#2][#3]{#4}\kaobiblio@sidesupercite{#4}}}%
+ }%
+ \@tempa%
+}
+
+% Command to \textcite and print a citation in the margin
+% First optional argument: vertical shift
+% Second optional argument: postnote if the third argument isn't specified; prenote if the third argument is specified (same pattern as the \textcite command)
+% Third optional argument: postnote
+% Mandatory argument: citation key
+\NewDocumentCommand{\sidetextcite}{o o o m}{%
+ \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
+ \marginnote[#1]{##1}%
+ }%
+ \DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
+ }{%
+ \formatmargincitation{\thefield{entrykey}}%
+ }{%
+ \\% separator between multiple citations
+ }{%
+ }%
+ % With this we print the marker in the text and add the item to the bibliography at the end
+ \IfNoValueOrEmptyTF{#2}%
+ {\def\@tempa{\textcite{#4}\kaobiblio@sidecite{#4}}}%
+ {\IfNoValueOrEmptyTF{#3}%
+ {\IfNoValueTF{#3}%
+ {\def\@tempa{\textcite[#2]{#4}\kaobiblio@sidecite{#4}}}%
+ {\def\@tempa{\textcite[#2][]{#4}\kaobook@sidecite{#4}}}% postnote is empty, so pass empty postnote
+ }%
+ {\def\@tempa{\textcite[#2][#3]{#4}\kaobiblio@sidecite{#4}}}%
+ }%
+ \ifkaobiblio@addspace%
+ \unskip~\@tempa%
+ \else%
+ \@tempa%
+ \fi%
+}
+
+% Command to \parencite or \parencite* and print a citation in the margin
+% First optional (star) argument: use \parencite* if included; otherwise use \parencite
+% Second optional argument: vertical shift
+% Third optional argument: postnote if the fourth argument isn't specified; prenote if the fourth argument is specified (same pattern as the \parencite command)
+% Fourth optional argument: postnote
+% Mandatory argument: citation key
+\NewDocumentCommand{\sideparencite}{s o o o m}{%
+ \RenewDocumentCommand{\kaobiblio@marginnote}{m}{%
+ \marginnote[#2]{##1}%
+ }%
+ \DeclareCiteCommand{\kaobiblio@sidecite}[\kaobiblio@marginnote]{%
+ }{%
+ \formatmargincitation{\thefield{entrykey}}%
+ }{%
+ \\% separator between multiple citations
+ }{%
+ }%
+ % With this we print the marker in the text and add the item to the bibliography at the end
+ \IfBooleanTF#1%
+ {\IfNoValueOrEmptyTF{#3}%
+ {\parencite*{#5}}%
+ {\IfNoValueOrEmptyTF{#4}%
+ {\IfNoValueTF{#4}%
+ {\def\@tempa{\parencite*[#3]{#5}}}%
+ {\def\@tempa{\parencite*[#3][]{#5}}}% postnote is empty, so pass empty postnote
+ }%
+ {\def\@tempa{\parencite*[#3][#4]{#5}}}%
+ }%
+ }%
+ {\IfNoValueOrEmptyTF{#3}%
+ {\def\@tempa{\parencite{#5}}}%
+ {\IfNoValueOrEmptyTF{#4}%
+ {\IfNoValueTF{#4}%
+ {\def\@tempa{\parencite[#3]{#5}}}%
+ {\def\@tempa{\parencite[#3][]{#5}}}% postnote is empty, so pass empty postnote
+ }%
+ {\def\@tempa{\parencite[#3][#4]{#5}}}%
+ }%
+ }%
+ \ifkaobiblio@addspace%
+ \unskip~\@tempa%
+ \else%
+ \@tempa%
+ \fi%
+}
+
+
+%----------------------------------------------------------------------------------------
+% LINKING THE AUTHOR'S NAME
+%----------------------------------------------------------------------------------------
+
+% In biblatex, when citing with the style authoryear or using \textcite, only the year is linked to the reference in the bibliography. Despite the arguments of one of the mantainers of the biblatex package (https://github.com/plk/biblatex/issues/428), some users think that in the author* style the author name should be a link as well. The `linkname' option provides an easy way to activate this behaviour.
+
+\ifkaobiblio@linkeverything
+ \xpatchbibmacro{cite}
+ {\usebibmacro{cite:label}%
+ \setunit{\printdelim{nonameyeardelim}}%
+ \usebibmacro{cite:labeldate+extradate}}
+ {\printtext[bibhyperref]{%
+ \DeclareFieldAlias{bibhyperref}{default}%
+ \usebibmacro{cite:label}%
+ \setunit{\printdelim{nonameyeardelim}}%
+ \usebibmacro{cite:labeldate+extradate}}}
+ {}
+ {\PackageWarning{biblatex-patch}
+ {Failed to patch cite bibmacro}}
+
+ % Include labelname in labelyear link
+ \xpatchbibmacro{cite}
+ {\printnames{labelname}%
+ \setunit{\printdelim{nameyeardelim}}%
+ \usebibmacro{cite:labeldate+extradate}}
+ {\printtext[bibhyperref]{%
+ \DeclareFieldAlias{bibhyperref}{default}%
+ \printnames{labelname}%
+ \setunit{\printdelim{nameyeardelim}}%
+ \usebibmacro{cite:labeldate+extradate}}}
+ {}
+ {\PackageWarning{biblatex-patch}
+ {Failed to patch cite bibmacro}}
+
+ % Access hyperref's citation link start/end commands
+ \makeatletter
+ \protected\def\blx@imc@biblinkstart{%
+ \@ifnextchar[%]
+ {\blx@biblinkstart}
+ {\blx@biblinkstart[\abx@field@entrykey]}}
+ \def\blx@biblinkstart[#1]{%
+ \blx@sfsave\hyper@natlinkstart{\the\c@refsection @#1}\blx@sfrest}
+ \protected\def\blx@imc@biblinkend{%
+ \blx@sfsave\hyper@natlinkend\blx@sfrest}
+ \blx@regimcs{\biblinkstart \biblinkend}
+ \makeatother
+
+ \newbool{cbx:link}
+
+ % Include parentheses around labelyear in \textcite only in
+ % single citations without pre- and postnotes
+ \def\iflinkparens{%
+ \ifboolexpr{ test {\ifnumequal{\value{multicitetotal}}{0}} and
+ test {\ifnumequal{\value{citetotal}}{1}} and
+ test {\iffieldundef{prenote}} and
+ test {\iffieldundef{postnote}} }}
+
+ \xpatchbibmacro{textcite}
+ {\printnames{labelname}}
+ {\iflinkparens
+ {\DeclareFieldAlias{bibhyperref}{default}%
+ \global\booltrue{cbx:link}\biblinkstart%
+ \printnames{labelname}}
+ {\printtext[bibhyperref]{\printnames{labelname}}}}
+ {}
+ {\PackageWarning{biblatex-patch}
+ {Failed to patch textcite bibmacro}}
+
+ \xpatchbibmacro{textcite}
+ {\usebibmacro{cite:label}}
+ {\iflinkparens
+ {\DeclareFieldAlias{bibhyperref}{default}%
+ \global\booltrue{cbx:link}\biblinkstart%
+ \usebibmacro{cite:label}}
+ {\usebibmacro{cite:label}}}
+ {}
+ {\PackageWarning{biblatex-patch}
+ {Failed to patch textcite bibmacro}}
+
+ \xpretobibmacro{textcite:postnote}
+ {\ifbool{cbx:link}
+ {\ifbool{cbx:parens}
+ {\bibcloseparen\global\boolfalse{cbx:parens}}
+ {}%
+ \biblinkend\global\boolfalse{cbx:link}}
+ {}}
+ {}
+ {\PackageWarning{biblatex-patch}
+ {Failed to patch textcite:postnote bibmacro}}
+\else
+\fi
+
+%----------------------------------------------------------------------------------------
+% CITATION ENVIRONMENTS
+%----------------------------------------------------------------------------------------
+
+% TODO: create a fancy environment for this. Perhaps printing also the
+% abstract.
+
+% Cite commands (assuming biblatex is loaded)
+\DeclareCiteCommand{\fullcite}{%
+ \defcounter{maxnames}{99}%
+ \usebibmacro{prenote}}
+ {\clearfield{url}%
+ \clearfield{pages}%
+ \clearfield{pagetotal}%
+ \clearfield{edition}%
+ \clearfield{issn}%
+ \clearfield{doi}%
+ \usedriver
+ {\DeclareNameAlias{sortname}{default}}
+ {\thefield{entrytype}}
+}
+{\multicitedelim}
+{\usebibmacro{postnote}}
diff --git a/kaobook.cls b/kaobook.cls
@@ -0,0 +1,266 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% kaobook
+% LaTeX Class
+% Version 0.9.7 (2021/06/02)
+%
+% This template originates from:
+% https://www.LaTeXTemplates.com
+%
+% For the latest template development version and to make contributions:
+% https://github.com/fmarotta/kaobook
+%
+% Authors:
+% Federico Marotta (federicomarotta@mail.com)
+% Based on the doctoral thesis of Ken Arroyo Ohori (https://3d.bk.tudelft.nl/ken/en)
+% and on the Tufte-LaTeX class.
+% Modified for LaTeX Templates by Vel (vel@latextemplates.com)
+%
+% License:
+% LPPL (see included MANIFEST.md file)
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%----------------------------------------------------------------------------------------
+% CLASS CONFIGURATION
+%----------------------------------------------------------------------------------------
+
+\NeedsTeXFormat{LaTeX2e}
+\ProvidesClass{kaobook}[2021/06/02 v0.9.7 kaobook]
+\newcommand{\@baseclass}{scrbook} % Base class name
+
+%\RequirePackage{kvoptions} % Manage class key-value options
+
+%\SetupKeyvalOptions{
+% family = kao,
+% prefix = kao@
+%}
+
+% Set the default options
+\PassOptionsToClass{a4paper}{\@baseclass}
+\PassOptionsToClass{fontsize=10pt}{\@baseclass}
+\PassOptionsToClass{parskip=half}{\@baseclass}
+\PassOptionsToClass{headings=optiontoheadandtoc}{\@baseclass}
+
+% Pass through any other options to the base class
+\DeclareOption*{\PassOptionsToClass{\CurrentOption}{\@baseclass}}
+
+%\ProcessKeyvalOptions*
+\ProcessOptions\relax % Process the options
+
+\LoadClass{\@baseclass} % Load the base class
+
+% Define kao-specific options
+%\DeclareStringOption[1]{secnumdepth}
+\DefineFamily{kao}
+\DefineFamilyMember[kaobook]{kao}
+\DefineFamilyKey[kaobook]{kao}{secnumdepth}[1]{\setcounter{secnumdepth}{#1}\FamilyKeyStateProcessed}
+% \FamilyProcessOptions[kaobook]{kao} % I COMMENTED THIS
+
+\RequirePackage{kao} % Load the code common to all classes
+
+%----------------------------------------------------------------------------------------
+% FRONT-, MAIN-, BACK- MATTERS BEHAVIOUR
+%----------------------------------------------------------------------------------------
+
+% Front matter
+\let\oldfrontmatter\frontmatter % Store the old command
+\renewcommand{\frontmatter}{%
+ \oldfrontmatter% First of all, call the old command
+ \pagestyle{plain.scrheadings}% Use a plain style for the header and the footer
+ \pagelayout{wide}% Use a wide page layout
+ \setchapterstyle{plain} % Choose the default chapter heading style
+ % \sloppy % Required to better break long lines
+}
+
+%------------------------------------------------
+
+% Main matter
+\let\oldmainmatter\mainmatter % Store the old command
+\renewcommand{\mainmatter}{%
+ \oldmainmatter% Call the old command
+ \pagestyle{scrheadings}% Use a fancy style for the header and the footer
+ \pagelayout{margin}% Use a 1.5 column layout
+ \setchapterstyle{kao} % Choose the default chapter heading style
+}
+
+%------------------------------------------------
+
+% Appendix
+\let\oldappendix\appendix% Store the old command
+\renewcommand{\appendix}{%
+ \oldappendix% Call the old command
+ \bookmarksetup{startatroot}% Reset the bookmark depth
+}
+
+%------------------------------------------------
+
+% Back matter
+\let\oldbackmatter\backmatter% Store the old command
+\renewcommand{\backmatter}{%
+ \oldbackmatter% Call the old command
+ \bookmarksetup{startatroot}% Reset the bookmark depth
+ \pagestyle{plain.scrheadings}% Use a plain style for the header and the footer
+ \pagelayout{wide}% Use a wide page layout
+ \setchapterstyle{plain} % Choose the default chapter heading style
+}
+
+%----------------------------------------------------------------------------------------
+% CHAPTER HEADING STYLES
+%----------------------------------------------------------------------------------------
+
+\DeclareDocumentCommand{\setchapterstyle}{m}{%
+ \ifthenelse{\equal{plain}{#1}}{\chapterstyleplain}{}
+ \ifthenelse{\equal{bar}{#1}}{\chapterstylebar}{}
+ \ifthenelse{\equal{lines}{#1}}{\chapterstylelines}{}
+ \ifthenelse{\equal{kao}{#1}}{\chapterstylekao}{}
+}
+
+% The default definition in KOMA script
+\DeclareDocumentCommand{\chapterstyleplain}{}{%
+ \renewcommand{\chapterlinesformat}[3]{%
+ \@hangfrom{##2}{##3}}
+ \renewcommand*{\chapterformat}{%
+ \mbox{\chapappifchapterprefix{\nobreakspace}\thechapter%
+ \autodot\IfUsePrefixLine{}{\enskip}}}
+ \RedeclareSectionCommand[beforeskip=0cm,afterskip=10\vscale]{chapter}
+ \setlength{\mtocshift}{-1\vscale}
+}
+
+% Gray bar
+\DeclareDocumentCommand{\chapterstylebar}{}{%
+ \renewcommand*{\chapterformat}{%
+ \mbox{\chapappifchapterprefix{\nobreakspace}\thechapter%
+ \autodot\IfUsePrefixLine{}{\enskip}}%
+ }
+ \renewcommand{\chapterlinesformat}[3]{%
+ \begin{tikzpicture}[remember picture, overlay]
+ \node[
+ anchor=south west,
+ xshift=\dimexpr - \hoffset - \oddsidemargin - 1in -1mm,%-30\hscale,
+ yshift=4.3mm,
+ rectangle,
+ fill=gray!20!white,
+ fill opacity=0.8,
+ inner ysep=5\vscale,
+ inner xsep=\dimexpr \hoffset + \oddsidemargin + 1in,%30\hscale,
+ text opacity=1,
+ text width=\paperwidth-40\hscale,
+ ]{\@hangfrom{##2}{##3}};
+ \end{tikzpicture}
+ }
+ \RedeclareSectionCommand[beforeskip=-55\vscale,afterskip=6\vscale]{chapter}
+ \setlength{\mtocshift}{-1\vscale}
+}
+
+% Lines
+\renewcommand{\hrulefill}[1][0.4pt]{%
+ \leavevmode\leaders\hrule height #1\hfill\kern\z@%
+}
+\DeclareDocumentCommand{\chapterstylelines}{}{%
+ \renewcommand*{\chapterformat}{%
+ \chapappifchapterprefix{\nobreakspace}\scalebox{3.5}{\thechapter\autodot}%
+ }%
+ \renewcommand\chapterlinesformat[3]{%
+ %\vspace*{-1cm}%
+ \leavevmode%
+ \makebox[0pt][l]{%
+ \makebox[\textwidth][l]{\hrulefill[1pt]##2}%\hfill%\par%\bigskip
+ \makebox[\marginparsep][l]{}%
+ \makebox[\marginparwidth][l]{}%
+ }\\
+ %\vspace{.5cm}
+ \makebox[0pt][l]{%
+ \makebox[\textwidth][l]{##3}%
+ \makebox[\marginparsep][l]{}%
+ \makebox[\marginparwidth][l]{}%
+ }\\
+ \makebox[0pt][l]{%
+ \makebox[\textwidth+\marginparsep+\marginparwidth][l]{\hrulefill[1.1pt]}%
+ }%
+ }%
+ \RedeclareSectionCommand[beforeskip=0cm,afterskip=10\vscale]{chapter}
+ \setlength{\mtocshift}{-1\vscale}%
+}
+
+% The Kao style
+\DeclareDocumentCommand{\chapterstylekao}{}{%
+ \renewcommand*{\chapterformat}{%
+ \mbox{\chapappifchapterprefix{\nobreakspace}\scalebox{2.85}{\thechapter\autodot}}%
+ }%
+ \renewcommand\chapterlinesformat[3]{%
+ \vspace{3.5\vscale}%
+ \if@twoside%
+ \Ifthispageodd{%
+ \smash{\makebox[0pt][l]{%
+ \parbox[b]{\textwidth}{\flushright{##3}}%
+ \makebox[\marginparsep][c]{\rule[-2\vscale]{1pt}{27.4\vscale+\f@size mm}}%
+ \parbox[b]{\marginparwidth}{##2}%
+ }}%
+ }{
+ \smash{\makebox[\textwidth][r]{%
+ \parbox[b]{\marginparwidth}{\flushright{##2}}%
+ \makebox[\marginparsep][c]{\rule[-2\vscale]{1pt}{27.4\vscale+\f@size mm}}%
+ \parbox[b]{\textwidth}{\flushleft{##3}}%
+ }}%
+ }
+ \else%
+ \smash{\makebox[0pt][l]{%
+ \parbox[b]{\textwidth}{\flushright{##3}}%
+ \makebox[\marginparsep][c]{\rule[-2\vscale]{1pt}{27.4\vscale+\f@size mm}}%
+ \parbox[b]{\marginparwidth}{##2}%
+ }}%
+ \fi%
+ }%
+ \RedeclareSectionCommand[beforeskip=0cm,afterskip=10\vscale]{chapter}%
+ \setlength{\mtocshift}{-3\vscale}%
+}
+
+% Takes as input the image path and optionally the "beforeskip"
+\DeclareDocumentCommand{\setchapterimage}{O{55\vscale} m}{%
+ \setchapterpreamble[o]{%
+ \vspace*{-27\vscale}\hspace*{\dimexpr - \hoffset - \oddsidemargin - 1in}%
+ \includegraphics[width=\paperwidth,height=#1+27\vscale,keepaspectratio=false]{#2}%
+ }%
+ \chapterstylebar%
+ % beforeskip=-(figure_height-top_margin)
+ \RedeclareSectionCommand[beforeskip=-#1, afterskip=6\vscale]{chapter}%
+ \setlength{\mtocshift}{0cm}%
+}
+
+% By default start with plain style
+\chapterstyleplain
+
+%----------------------------------------------------------------------------------------
+% FONTS AND STYLES
+%----------------------------------------------------------------------------------------
+
+% Set KOMA fonts for book-specific elements
+\addtokomafont{part}{\normalfont\scshape\bfseries}
+\addtokomafont{partentry}{\normalfont\scshape\bfseries}
+\addtokomafont{chapter}{\normalfont\bfseries}
+\addtokomafont{chapterentry}{\normalfont\bfseries}
+
+% Set KOMA fonts for elements common to all classes
+\addtokomafont{section}{\normalfont\bfseries}
+\addtokomafont{subsection}{\normalfont\bfseries}
+\addtokomafont{subsubsection}{\normalfont\bfseries}
+\addtokomafont{paragraph}{\normalfont\bfseries}
+\setkomafont{descriptionlabel}{\normalfont\bfseries}
+
+%----------------------------------------------------------------------------------------
+% TOC, LOF & LOT
+%----------------------------------------------------------------------------------------
+
+\PassOptionsToClass{toc=listof}{\@baseclass}
+\PassOptionsToClass{toc=index}{\@baseclass}
+\PassOptionsToClass{toc=bibliography}{\@baseclass}
+
+%----------------------------------------------------------------------------------------
+% NUMBERING
+%----------------------------------------------------------------------------------------
+
+%\setcounter{secnumdepth}{\kao@secnumdepth} % Set section numbering
+%depth
+
+\counterwithin*{sidenote}{chapter} % Uncomment to reset the sidenote counter at each chapter
+%\counterwithout{sidenote}{chapter} % Uncomment to have one sidenote counter for the whole document
diff --git a/kaorefs.sty b/kaorefs.sty
@@ -0,0 +1,205 @@
+\ProvidesPackage{kaorefs}
+
+% Easily label and reference elements
+% Note that \label must appear after \caption
+% Load this package last
+
+% Pass this package's options to hyperref and varioref
+\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{varioref}}
+\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref}}
+\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{cleveref}}
+\ProcessOptions\relax
+
+\let\thmname\relax % Workaround to get rid of an annoying error
+\RequirePackage{varioref}
+\RequirePackage{hyperref}
+\RequirePackage[capitalise,nameinlink,noabbrev]{cleveref}
+
+
+% Language-specific strings
+% #LANGUAGE
+\newcommand{\chapternameshort}{}
+\newcommand{\sectionname}{}
+\newcommand{\sectionnameshort}{}
+\newcommand{\subsectionname}{}
+\newcommand{\subsectionnameplural}{}
+\newcommand{\subsectionnameshort}{}
+\newcommand{\figurenameshort}{}
+\newcommand{\tablenameshort}{}
+\newcommand{\eqname}{}
+\newcommand{\eqnameshort}{}
+\newcommand{\defname}{}
+\newcommand{\assumname}{}
+\newcommand{\thmname}{}
+\newcommand{\propname}{}
+\newcommand{\lemmaname}{}
+\newcommand{\remarkname}{}
+\newcommand{\examplename}{}
+\newcommand{\exercisename}{}
+
+\addto\captionsdanish{%
+ \renewcommand{\chapternameshort}{Kap.}
+ \renewcommand{\sectionname}{Sektion}
+ \renewcommand{\sectionnameshort}{Sek.}
+ \renewcommand{\subsectionname}{Undersektion}
+ \renewcommand{\subsectionnameplural}{Undersectioner}
+ \renewcommand{\subsectionnameshort}{Undersek.}
+ \renewcommand{\figurenameshort}{Fig.}
+ \renewcommand{\tablenameshort}{Tab.}
+ \renewcommand{\eqname}{Formel}
+ \renewcommand{\eqnameshort}{Frml.}
+ \renewcommand{\defname}{Definition}
+ \renewcommand{\assumname}{Hypotese}
+ \renewcommand{\thmname}{Sætning}
+ \renewcommand{\propname}{Postulat}
+ \renewcommand{\lemmaname}{Hjælpesætning}
+ \renewcommand{\remarkname}{Bemærkning}
+ \renewcommand{\examplename}{Eksempel}
+ \renewcommand{\exercisename}{Øvelse}
+}
+\addto\captionsenglish{%
+ \renewcommand{\chapternameshort}{Chap.}
+ \renewcommand{\sectionname}{Section}
+ \renewcommand{\sectionnameshort}{Sec.}
+ \renewcommand{\subsectionname}{Subsection}
+ \renewcommand{\subsectionnameplural}{Subsections}
+ \renewcommand{\subsectionnameshort}{Subsec.}
+ \renewcommand{\figurenameshort}{Fig.}
+ \renewcommand{\tablenameshort}{Tab.}
+ \renewcommand{\eqname}{Equation}
+ \renewcommand{\eqnameshort}{Eq.}
+ \renewcommand{\defname}{Definition}
+ \renewcommand{\assumname}{Assumption}
+ \renewcommand{\thmname}{Theorem}
+ \renewcommand{\propname}{Proposition}
+ \renewcommand{\lemmaname}{Lemma}
+ \renewcommand{\remarkname}{Remark}
+ \renewcommand{\examplename}{Example}
+ \renewcommand{\exercisename}{Exercise}
+}
+\addto\captionsitalian{%
+ \renewcommand{\chapternameshort}{Cap.}
+ \renewcommand{\sectionname}{Sezione}
+ \renewcommand{\sectionnameshort}{Sez.}
+ \renewcommand{\subsectionname}{Sottosezione}
+ \renewcommand{\subsectionnameplural}{Sottosezioni}
+ \renewcommand{\subsectionnameshort}{Sottosezione}
+ \renewcommand{\figurenameshort}{Fig.}
+ \renewcommand{\tablenameshort}{Tab.}
+ \renewcommand{\eqname}{Equazione}
+ \renewcommand{\eqnameshort}{Eq.}
+ \renewcommand{\defname}{Definizione}
+ \renewcommand{\assumname}{Assunzione}
+ \renewcommand{\thmname}{Teorema}
+ \renewcommand{\propname}{Proposizione}
+ \renewcommand{\lemmaname}{Lemma}
+ \renewcommand{\remarkname}{Osservazione}
+ \renewcommand{\examplename}{Esempio}
+ \renewcommand{\exercisename}{Esercizio}
+}
+% Do you speak other languages? Please, feel free to add the captions!
+
+
+% Labelling commands
+\newcommand{\labpage}[1]{\label{page:#1}}
+\newcommand{\labpart}[1]{\label{part:#1}}
+\newcommand{\labch}[1]{\label{ch:#1}}
+\newcommand{\labsec}[1]{\label{sec:#1}}
+\newcommand{\labsubsec}[1]{\label{subsec:#1}}
+\newcommand{\labfig}[1]{\label{fig:#1}}
+\newcommand{\labtab}[1]{\label{tab:#1}}
+\newcommand{\labeq}[1]{\label{eq:#1}}
+\newcommand{\labdef}[1]{\label{def:#1}}
+\newcommand{\labthm}[1]{\label{thm:#1}}
+\newcommand{\labassum}[1]{\label{assum:#1}}
+\newcommand{\labprop}[1]{\label{prop:#1}}
+\newcommand{\lablemma}[1]{\label{lemma:#1}}
+\newcommand{\labremark}[1]{\label{remark:#1}}
+\newcommand{\labexample}[1]{\label{example:#1}}
+\newcommand{\labexercise}[1]{\label{exercise:#1}}
+
+
+% Referencing commands
+\newcommand{\refpage}[1]{\hyperref[#1]{\pagename}\xspace\pageref{page:#1}} % Page 84
+\newcommand{\vrefpage}[1]{\vpageref*{page:#1}} % on the following page, on page 84
+
+% For unnumbered parts
+\newcommand{\arefpart}[1]{\hyperref[part:#1]{\partname}\xspace`\nameref{part:#1}'} % Part `Name of the Part'
+\newcommand{\avrefpart}[1]{\hyperref[part:#1]{\partname}\xspace`\nameref{part:#1}' \vpageref{part:#1}} % Part `Name of the Part' on page 84
+
+% For numbered parts
+\newcommand{\refpart}[1]{\hyperref[part:#1]{\partname}\xspace\ref{part:#1}} % Part IV
+\newcommand{\vrefpart}[1]{\hyperref[part:#1]{\partname}\xspace\vref{part:#1}} % Part IV, Part IV on the following page, Part IV on page 84
+\newcommand{\nrefpart}[1]{\hyperref[part:#1]{\partname}\xspace\ref{part:#1} (\nameref{part:#1})}
+\newcommand{\frefpart}[1]{\hyperref[part:#1]{\partname\xspace\ref{part:#1} (\nameref{part:#1}) \vpageref{part:#1}}} % Part IV (Name of the Part), Part IV (Name of the Part) on the following page, Part IV (Name of the Part) on page 84)
+
+%\newcommand{\refch}[1]{\hyperref[#1]{\chaptername\xspace\usekomafont{chapter}\normalsize\nameref{ch:#1}}\xspace\vpageref{ch:#1}\,}
+\newcommand{\refchshort}[1]{\hyperref[ch:#1]{\chapternameshort\xspace\ref{ch:#1}}}
+\newcommand{\refch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1}}}
+\newcommand{\vrefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} \vpageref{ch:#1}}}
+\newcommand{\nrefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} (\nameref{ch:#1})}}
+\newcommand{\frefch}[1]{\hyperref[ch:#1]{\chaptername\xspace\ref{ch:#1} (\nameref{ch:#1}) \vpageref{ch:#1}}}
+
+%\newcommand{\refsec}[1]{Section~{\usekomafont{section}\normalsize\nameref{sec:#1}}\xspace\vpageref{sec:#1}\,}
+\newcommand{\refsecshort}[1]{\hyperref[sec:#1]{\sectionnameshort\xspace\ref{sec:#1}}}
+\newcommand{\refsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1}}}
+\newcommand{\vrefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\vref{sec:#1}}}
+\newcommand{\nrefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1} (\nameref{sec:#1})}}
+\newcommand{\frefsec}[1]{\hyperref[sec:#1]{\sectionname\xspace\ref{sec:#1} (\nameref{sec:#1}) \vpageref{sec:#1}}}
+
+\newcommand{\refsubsecshort}[1]{\hyperref[subsec:#1]{\sectionnameshort\xspace\ref{subsec:#1}}}
+\newcommand{\refsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1}}}
+\newcommand{\vrefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\vref{subsec:#1}}}
+\newcommand{\nrefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1} (\nameref{subsec:#1})}}
+\newcommand{\frefsubsec}[1]{\hyperref[subsec:#1]{\subsectionname\xspace\ref{subsec:#1} (\nameref{subsec:#1}) \vpageref{subsec:#1}}}
+
+%\newcommand{\reffig}[1]{{\hypersetup{colorlinks=false}\usekomafont{captionlabel}\hyperref[fig:#1]{Figure}\xspace\ref{fig:#1}}}
+\newcommand{\reffigshort}[1]{\hyperref[fig:#1]{\figurenameshort\xspace\ref{fig:#1}}}
+\newcommand{\reffig}[1]{\hyperref[fig:#1]{\figurename}\xspace\ref{fig:#1}}
+\newcommand{\vreffig}[1]{\hyperref[fig:#1]{\figurename\xspace\vref{fig:#1}}}
+
+%\newcommand{\reftab}[1]{{\hypersetup{colorlinks=false}\usekomafont{captionlabel}\hyperref[tab:#1]{Table}\xspace\ref{tab:#1}}}
+\newcommand{\reftab}[1]{\hyperref[tab:#1]{\tablename}\xspace\ref{tab:#1}}
+\newcommand{\vreftab}[1]{\hyperref[tab:#1]{\tablename\xspace\vref{tab:#1}}}
+
+\newcommand{\refeqshort}[1]{\hyperref[eq:#1]\eqnameshort\xspace(\ref{eq:#1})}
+\newcommand{\refeq}[1]{\hyperref[eq:#1]\eqname\xspace\ref{eq:#1}}
+\newcommand{\vrefeq}[1]{\hyperref[eq:#1]\eqname\xspace\vref{eq:#1}}
+
+\newcommand{\refdef}[1]{\hyperref[def:#1]\defname\xspace\ref{def:#1}}
+\newcommand{\vrefdef}[1]{\hyperref[def:#1]\defname\xspace\vref{def:#1}}
+
+\newcommand{\refassum}[1]{\hyperref[assum:#1]\assumname\xspace\ref{assum:#1}}
+\newcommand{\vrefassum}[1]{\hyperref[assum:#1]\assumname\xspace\vref{assum:#1}}
+
+\newcommand{\refthm}[1]{\hyperref[thm:#1]\thmname\xspace\ref{thm:#1}}
+\newcommand{\vrefthm}[1]{\hyperref[thm:#1]\thmname\xspace\vref{thm:#1}}
+
+\newcommand{\refprop}[1]{\hyperref[prop:#1]\propname\xspace\ref{prop:#1}}
+\newcommand{\vrefprop}[1]{\hyperref[prop:#1]\propname\xspace\vref{prop:#1}}
+
+\newcommand{\reflemma}[1]{\hyperref[lemma:#1]\lemmaname\xspace\ref{lemma:#1}}
+\newcommand{\vreflemma}[1]{\hyperref[lemma:#1]\lemmaname\xspace\vref{lemma:#1}}
+
+\newcommand{\refremark}[1]{\hyperref[remark:#1]\remarkname\xspace\ref{remark:#1}}
+\newcommand{\vrefremark}[1]{\hyperref[remark:#1]\remarkname\xspace\vref{remark:#1}}
+
+\newcommand{\refexample}[1]{\hyperref[example:#1]\examplename\xspace\ref{example:#1}}
+\newcommand{\vrefexample}[1]{\hyperref[example:#1]\examplename\xspace\vref{example:#1}}
+
+\newcommand{\refexercise}[1]{\hyperref[exercise:#1]\exercisename\xspace\ref{exercise:#1}}
+\newcommand{\vrefexercise}[1]{\hyperref[exercise:#1]\exercisename\xspace\vref{exercise:#1}}
+
+
+% cleveref customisation
+
+% Hyperlink the page reference as well
+\let\oldvpageref\vpageref
+\renewcommand{\vpageref}[1]{\hyperref[#1]{\oldvpageref{#1}}}
+
+% Remove parentheses around equations
+\creflabelformat{equation}{#2\textup{#1}#3}
+
+% Set the refname for subsections
+\crefname{subsection}{\subsectionname}{\subsectionnameplural}
+\Crefname{subsection}{\subsectionname}{\subsectionnameplural}
diff --git a/kaotheorems.sty b/kaotheorems.sty
@@ -0,0 +1,391 @@
+\ProvidesPackage{kaotheorems}
+
+\RequirePackage{kvoptions} % Handle package options
+\SetupKeyvalOptions{
+ family = kaotheorems,
+ prefix = kaotheorems@
+}
+
+\DeclareBoolOption{framed}
+
+\newcommand{\kaotheorems@defaultbg}{Goldenrod!45!white}
+\DeclareStringOption[\kaotheorems@defaultbg]{background}
+\DeclareStringOption[\kaotheorems@defaultbg]{theorembackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{propositionbackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{lemmabackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{corollarybackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{definitionbackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{assumptionbackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{remarkbackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{examplebackground}
+\DeclareStringOption[\kaotheorems@defaultbg]{exercisebackground}
+
+\ProcessKeyvalOptions{kaotheorems}
+
+\let\openbox\relax
+\RequirePackage{amsmath} % Improved mathematics
+\RequirePackage{amsthm} % Mathematical environments
+\RequirePackage{thmtools} % Theorem styles
+
+\ifkaotheorems@framed%
+ \RequirePackage{tikz} % Colorful boxes
+ \RequirePackage[framemethod=TikZ]{mdframed}
+
+ % Box style
+ \mdfsetup{skipabove=\topskip,skipbelow=0pt}%-.5\topskip}
+ \mdfdefinestyle{mdfkao}{
+ skipabove=\topskip,
+ skipbelow=\topskip, % Does not work :(
+ rightmargin=0pt,
+ leftmargin=0pt,
+ innertopmargin=7pt,
+ innerbottommargin=3pt,
+ innerrightmargin=5pt,
+ innerleftmargin=5pt,
+ topline=false,
+ bottomline=false,
+ rightline=false,
+ leftline=false,
+ %linewidth=1pt,
+ %roundcorner=0pt,
+ %font={},
+ %frametitlefont={},
+ frametitlerule=true,
+ %linecolor=black,
+ %backgroundcolor=LightBlue,
+ %fontcolor=black,
+ %frametitlebackgroundcolor=LightBlue,
+ }
+
+ % Theorem styles
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,%\scshape,
+ %notefont=\normalfont, notebraces={ (}{)},
+ bodyfont=\normalfont\itshape,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ %postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ ]{kaoplain}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,%\scshape,
+ %notefont=\normalfont, notebraces={ (}{)},
+ bodyfont=\normalfont\itshape,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ ]{kaodefinition}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,%\scshape,
+ %notefont=\normalfont, notebraces={ (}{)},
+ bodyfont=\normalfont\itshape,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ ]{kaoassumption}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\normalfont,
+ %headformat={\footnotesize$\triangleright$\space\normalsize\NAME\space\NUMBER\space\NOTE},
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoremark}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\normalfont,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoexample}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\small,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoexercise}
+
+ \theoremstyle{kaoplain}
+ \declaretheorem[
+ name=Theorem,
+ style=kaoplain,
+ %refname={theorem,theorems},
+ refname={Theorem,Theorems},
+ Refname={Theorem,Theorems},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@theorembackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{theorem}
+ \declaretheorem[
+ name=Proposition,
+ %refname={proposition,propositions},
+ refname={Proposition,Propositions},
+ Refname={Proposition,Propositions},
+ sibling=theorem,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@propositionbackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{proposition}
+ \declaretheorem[
+ name=Lemma,
+ %refname={lemma,lemmas},
+ refname={Lemma,Lemmas},
+ Refname={Lemma,Lemmas},
+ sibling=theorem,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@lemmabackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{lemma}
+ \declaretheorem[
+ name=Corollary,
+ %refname={corollary,corollaries},
+ refname={Corollary,Corollaries},
+ Refname={Corollary,Corollaries},
+ sibling=theorem,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@corollarybackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{corollary}
+
+ \theoremstyle{kaodefinition}
+ \declaretheorem[
+ name=Definition,
+ %refname={definition,definitions},
+ refname={Definition,Definitions},
+ Refname={Definition,Definitions},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@definitionbackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{definition}
+
+ \theoremstyle{kaoassumption}
+ \declaretheorem[
+ name=Assumption,
+ %refname={assumption,assumptions},
+ refname={Assumption,Assumptions},
+ Refname={Assumption,Assumptions},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@assumptionbackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{assumption}
+
+ \theoremstyle{kaoremark}
+ \declaretheorem[
+ name=Remark,
+ %refname={remark,remarks},
+ refname={Remark,Remarks},
+ Refname={Remark,Remarks},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@remarkbackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{remark}
+
+ \theoremstyle{kaoexample}
+ \declaretheorem[
+ name=Example,
+ %refname={example,examples},
+ refname={Example,Examples},
+ Refname={Example,Examples},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@examplebackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{example}
+
+ \theoremstyle{kaoexercise}
+ \declaretheorem[
+ name=Exercise,
+ %refname={example,examples},
+ refname={Exercise,Exercises},
+ Refname={Exercise,Exercises},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\kaotheorems@exercisebackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{exercise}
+
+ %\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{section}.\arabic{theorem}}
+ %\renewcommand{\thetheorem}{\arabic{subsection}.\arabic{theorem}}
+ %\renewcommand{\qedsymbol}{$\blacksquare$}
+\else
+
+ % Theorem styles
+ \declaretheoremstyle[
+ spaceabove=.6\thm@preskip,
+ spacebelow=.1\thm@postskip,
+ %headfont=\normalfont\bfseries,%\scshape,
+ %notefont=\normalfont, notebraces={ (}{)},
+ bodyfont=\normalfont\itshape,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ %postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ ]{kaoplain}
+ \declaretheoremstyle[
+ spaceabove=.6\thm@preskip,
+ spacebelow=.1\thm@postskip,
+ %headfont=\normalfont\bfseries,%\scshape,
+ %notefont=\normalfont, notebraces={ (}{)},
+ bodyfont=\normalfont\itshape,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ %postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ ]{kaodefinition}
+ \declaretheoremstyle[
+ spaceabove=.6\thm@preskip,
+ spacebelow=.1\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\normalfont,
+ %headformat={\footnotesize$\triangleright$\space\normalsize\NAME\space\NUMBER\space\NOTE},
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ %postheadspace={.5em plus .1em minus .1em},
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoremark}
+ \declaretheoremstyle[
+ spaceabove=.6\thm@preskip,
+ spacebelow=.1\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\normalfont,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ %postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoexample}
+ \declaretheoremstyle[
+ %spaceabove=.5\thm@preskip,
+ %spacebelow=.5\thm@postskip,
+ %headfont=\normalfont\bfseries,
+ %notefont=\normalfont, notebraces={ (}{)},
+ %bodyfont=\normalfont,
+ %headformat={\NAME\space\NUMBER\space\NOTE},
+ headpunct={},
+ postheadspace={.5em plus .1em minus .1em},
+ %prefoothook={\hfill\qedsymbol}
+ %refname={theorem,theorems},
+ %Refname={Theorem,Theorems},
+ ]{kaoexercise}
+
+ \theoremstyle{kaoplain}
+ \declaretheorem[
+ name=Theorem,
+ refname={theorem,theorems},
+ Refname={Theorem,Theorems},
+ numberwithin=section,
+ ]{theorem}
+ \declaretheorem[
+ name=Proposition,
+ refname={proposition,propositions},
+ Refname={Proposition,Propositions},
+ sibling=theorem,
+ ]{proposition}
+ \declaretheorem[
+ name=Lemma,
+ refname={lemma,lemmas},
+ Refname={Lemma,Lemmas},
+ sibling=theorem,
+ ]{lemma}
+ \declaretheorem[
+ name=Corollary,
+ refname={corollary,corollaries},
+ Refname={Corollary,Corollaries},
+ sibling=theorem,
+ ]{corollary}
+
+ \theoremstyle{kaodefinition}
+ \declaretheorem[
+ name=Definition,
+ refname={definition,definitions},
+ Refname={Definition,Definitions},
+ numberwithin=section,
+ ]{definition}
+
+ \theoremstyle{kaoremark}
+ \declaretheorem[
+ name=Remark,
+ refname={remark,remarks},
+ Refname={Remark,Remarks},
+ numberwithin=section,
+ ]{remark}
+
+ \theoremstyle{kaoexample}
+ \declaretheorem[
+ name=Example,
+ refname={example,examples},
+ Refname={Example,Examples},
+ numberwithin=section,
+ ]{example}
+
+ \theoremstyle{kaoexercise}
+ \declaretheorem[
+ name=Exercise,
+ %refname={example,examples},
+ refname={Exercise,Exercises},
+ Refname={Exercise,Exercises},
+ numberwithin=section,
+ mdframed={
+ style=mdfkao,
+ backgroundcolor=\@exercisebackground,
+ %frametitlebackgroundcolor=\@theorembackground,
+ },
+ ]{exercise}
+
+ %\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{section}.\arabic{theorem}}
+ %\renewcommand{\thetheorem}{\arabic{subsection}.\arabic{theorem}}
+ %\renewcommand{\qedsymbol}{$\blacksquare$}
+\fi
+
diff --git a/macros.tex b/macros.tex
@@ -0,0 +1,45 @@
+
+\newcommand{\E}[2]{\operatorname{E}_{#1}\left[#2\right]} % Expectation
+\newcommand{\Var}[1]{\operatorname{Var}\left[#1\right]} % Expectation
+% \newcommand{\E}[1]{\mathbb{E}\left[#1\right]} % Expectation
+\newcommand{\cov}[2]{\operatorname{Cov}_{#1}\left[#2\right]} % Covariance
+
+\renewcommand{\vec}[1]{\bm{#1}}
+
+\newcommand{\reals}{\mathbb{R}}
+
+% \newcommand{\identity}{\mathbb{I}}
+\newcommand{\identity}{\bm{I}}
+
+\newcommand{\normal}{\mathcal{N}}
+
+% \newcommand{\inside}{\subseteq}
+% \newcommand{\contains}{\supseteq}
+% \newcommand{\intersect}{\cap}
+% \newcommand{\union}{\cup}
+
+\newcommand{\elbo}{\mathcal{L}\left[q_{\bm \phi}\right]}
+\newcommand{\entropy}[1]{\mathbb{H}\left[#1\right]}
+\newcommand{\crossentropy}[2]{\mathbb{H}\left[#1, #2\right]}
+
+\newcommand{\given}{\, | \,}
+% \newcommand{\given}{\mid}
+
+\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
+\newcommand{\abs}[1]{\left\lvert#1\right\rvert}
+
+\newcommand*{\T}{^{\mkern-1.5mu\mathsf{T}}}
+
+\newcommand{\kld}[2]{D_{\mathrm{KL}}\left[#1\ \lVert\ #2\right]}
+
+\newcommand{\lik}{p\left(\bm y \given \bm \theta\right)}
+
+\DeclareMathOperator*{\argmax}{arg\,max}
+\DeclareMathOperator*{\argmin}{arg\,min}
+
+\DeclareMathOperator{\sn}{sn}
+\DeclareMathOperator{\cn}{cn}
+\DeclareMathOperator{\dn}{dn}
+
+\renewcommand\Re{\operatorname{Re}}
+\renewcommand\Im{\operatorname{Im}}
diff --git a/main.bib b/main.bib
@@ -0,0 +1,468 @@
+@article{amari1998natural,
+ title = {Natural gradient works efficiently in learning},
+ author = {Amari, Shun-Ichi},
+ journal = {Neural computation},
+ volume = {10},
+ number = {2},
+ pages = {251--276},
+ year = {1998},
+ publisher = {MIT Press}
+}
+
+@inproceedings{bach2013sharp,
+ title = {Sharp analysis of low-rank kernel matrix approximations},
+ author = {Bach, Francis},
+ booktitle = {Conference on learning theory},
+ pages = {185--209},
+ year = {2013},
+ organization = {PMLR}
+}
+
+@article{bae2018eigenvalue,
+ title = {Eigenvalue corrected noisy natural gradient},
+ author = {Bae, Juhan and Zhang, Guodong and Grosse, Roger},
+ journal = {arXiv preprint arXiv:1811.12565},
+ year = {2018}
+}
+
+@book{bernardo2009bayesian,
+ title = {Bayesian theory},
+ author = {Bernardo, Jos{\'e} M and Smith, Adrian FM},
+ volume = {405},
+ year = {2009},
+ publisher = {John Wiley \& Sons}
+}
+
+@article{bingham2018pyro,
+ author = {Bingham, Eli and Chen, Jonathan P. and Jankowiak, Martin and Obermeyer, Fritz and
+ Pradhan, Neeraj and Karaletsos, Theofanis and Singh, Rohit and Szerlip, Paul and
+ Horsfall, Paul and Goodman, Noah D.},
+ title = {{Pyro: Deep Universal Probabilistic Programming}},
+ journal = {Journal of Machine Learning Research},
+ year = {2018}
+}
+
+@book{bishop1995neural,
+ title = {Neural networks for pattern recognition},
+ author = {Bishop, Christopher},
+ year = {1995},
+ publisher = {Oxford university press}
+}
+
+@book{bishop2006pattern_evidence,
+ title = {Pattern recognition and machine learning},
+ author = {Bishop, Christopher M and Nasrabadi, Nasser M},
+ volume = {4},
+ number = {4},
+ year = {2006},
+ publisher = {Springer},
+ pages = {165-172}
+}
+
+@inproceedings{botev2017practical,
+ title = {Practical gauss-newton optimisation for deep learning},
+ author = {Botev, Aleksandar and Ritter, Hippolyt and Barber, David},
+ booktitle = {International Conference on Machine Learning},
+ pages = {557--565},
+ year = {2017},
+ organization = {PMLR}
+}
+
+@article{chang2021bayesian,
+ title = {Bayesian Neural Networks: Essentials},
+ author = {Chang, Daniel T},
+ journal = {arXiv preprint arXiv:2106.13594},
+ year = {2021}
+}
+
+@article{cohn1996active,
+ title = {Active learning with statistical models},
+ author = {Cohn, David A and Ghahramani, Zoubin and Jordan, Michael I},
+ journal = {Journal of artificial intelligence research},
+ volume = {4},
+ pages = {129--145},
+ year = {1996}
+}
+
+@article{coker2021wide,
+ title = {Wide Mean-Field Variational Bayesian Neural Networks Ignore the Data},
+ journal = {International Conference on Machine Learning},
+ year = {2021},
+ author = {Coker, Beau and Pan, Weiwei and Doshi-Velez, Finale}
+}
+
+@article{daxberger2021laplace,
+ title = {Laplace Redux-Effortless Bayesian Deep Learning},
+ author = {Daxberger, Erik and Kristiadi, Agustinus and Immer, Alexander and Eschenhagen, Runa and Bauer, Matthias and Hennig, Philipp},
+ journal = {Advances in Neural Information Processing Systems},
+ year = {2021}
+}
+
+@article{farquhar2020liberty,
+ title = {Liberty or depth: Deep bayesian neural nets do not need complex weight posterior approximations},
+ author = {Farquhar, Sebastian and Smith, Lewis and Gal, Yarin},
+ journal = {Advances in Neural Information Processing Systems},
+ year = {2020}
+}
+
+@inproceedings{farquhar2020radial,
+ title = {Radial bayesian neural networks: Beyond discrete support in large-scale bayesian deep learning},
+ author = {Farquhar, Sebastian and Osborne, Michael A and Gal, Yarin},
+ booktitle = {International Conference on Artificial Intelligence and Statistics},
+ year = {2020},
+ organization = {PMLR}
+}
+
+@article{fong2020marginal,
+ title = {On the marginal likelihood and cross-validation},
+ author = {Fong, Edwin and Holmes, Chris C},
+ journal = {Biometrika},
+ volume = {107},
+ number = {2},
+ pages = {489--496},
+ year = {2020},
+ publisher = {Oxford University Press}
+}
+
+@article{foong2020expressiveness,
+ title = {On the expressiveness of approximate inference in bayesian neural networks},
+ author = {Foong, Andrew and Burt, David and Li, Yingzhen and Turner, Richard},
+ journal = {Advances in Neural Information Processing Systems},
+ volume = {33},
+ pages = {15897--15908},
+ year = {2020}
+}
+
+@article{fortuin2021priors,
+ title = {Priors in bayesian deep learning: A review},
+ author = {Fortuin, Vincent},
+ journal = {arXiv preprint arXiv:2105.06868},
+ year = {2021}
+}
+
+@inproceedings{gal2016dropout,
+ title = {Dropout as a bayesian approximation: Representing model uncertainty in deep learning},
+ author = {Gal, Yarin and Ghahramani, Zoubin},
+ booktitle = {International Conference on Machine Learning},
+ year = {2016},
+ organization = {PMLR}
+}
+
+@inproceedings{gal2017deep,
+ title = {Deep bayesian active learning with image data},
+ author = {Gal, Yarin and Islam, Riashat and Ghahramani, Zoubin},
+ booktitle = {International Conference on Machine Learning},
+ year = {2017},
+ organization = {PMLR}
+}
+
+@article{gardner2018gpytorch,
+ title = {GPyTorch: Blackbox Matrix-Matrix Gaussian Process Inference with GPU Acceleration},
+ author = {Gardner, Jacob and Pleiss, Geoff and Weinberger, Kilian Q and Bindel, David and Wilson, Andrew G},
+ journal = {Advances in neural information processing systems},
+ volume = {31},
+ year = {2018}
+}
+
+@article{giordano2018covariances,
+ title = {Covariances, robustness and variational bayes},
+ author = {Giordano, Ryan and Broderick, Tamara and Jordan, Michael I},
+ journal = {Journal of machine learning research},
+ volume = {19},
+ number = {51},
+ year = {2018}
+}
+
+@article{harbrecht2012low,
+ title = {On the low-rank approximation by the pivoted Cholesky decomposition},
+ author = {Harbrecht, Helmut and Peters, Michael and Schneider, Reinhold},
+ journal = {Applied numerical mathematics},
+ volume = {62},
+ number = {4},
+ pages = {428--440},
+ year = {2012},
+ publisher = {Elsevier}
+}
+
+@inproceedings{huang2017densely,
+ title = {Densely connected convolutional networks},
+ author = {Huang, Gao and Liu, Zhuang and Van Der Maaten, Laurens and Weinberger, Kilian Q},
+ booktitle = {Proceedings of the IEEE conference on computer vision and pattern recognition},
+ year = {2017}
+}
+
+@inproceedings{izmailov2021bayesian,
+ title = {What are Bayesian neural network posteriors really like?},
+ author = {Izmailov, Pavel and Vikram, Sharad and Hoffman, Matthew D and Wilson, Andrew Gordon Gordon},
+ booktitle = {International Conference on Machine Learning},
+ year = {2021},
+ organization = {PMLR}
+}
+
+@software{jax2018github,
+ author = {James Bradbury and Roy Frostig and Peter Hawkins and Matthew James Johnson and Chris Leary and Dougal Maclaurin and George Necula and Adam Paszke and Jake Vander{P}las and Skye Wanderman-{M}ilne and Qiao Zhang},
+ title = {{JAX}: composable transformations of {P}ython+{N}um{P}y programs},
+ url = {http://github.com/google/jax},
+ version = {0.3.13},
+ year = {2018}
+}
+
+@article{khan2017variational,
+ title = {Variational adaptive-Newton method for explorative learning},
+ author = {Khan, Mohammad Emtiyaz and Lin, Wu and Tangkaratt, Voot and Liu, Zuozhu and Nielsen, Didrik},
+ journal = {arXiv preprint arXiv:1711.05560},
+ year = {2017}
+}
+
+@article{kingma2014adam,
+ title = {Adam: A method for stochastic optimization},
+ author = {Kingma, Diederik P and Ba, Jimmy},
+ journal = {arXiv preprint arXiv:1412.6980},
+ year = {2014}
+}
+
+@article{kingma2015variational,
+ title = {Variational dropout and the local reparameterization trick},
+ author = {Kingma, Durk P and Salimans, Tim and Welling, Max},
+ journal = {Advances in Neural Information Processing Systems},
+ year = {2015}
+}
+
+@article{krizhevsky2009learning,
+ title = {Learning multiple layers of features from tiny images},
+ author = {Krizhevsky, Alex and Hinton, Geoffrey and others},
+ year = {2009},
+ publisher = {Citeseer}
+}
+
+% -------------------------------------------------------------------------
+
+@article{kunstner2019limitations,
+ title = {Limitations of the empirical Fisher approximation for natural gradient descent},
+ author = {Kunstner, Frederik and Hennig, Philipp and Balles, Lukas},
+ journal = {Advances in neural information processing systems},
+ volume = {32},
+ year = {2019}
+}
+
+@article{lakshminarayanan2017simple,
+ title = {Simple and scalable predictive uncertainty estimation using deep ensembles},
+ author = {Lakshminarayanan, Balaji and Pritzel, Alexander and Blundell, Charles},
+ journal = {Advances in neural information processing systems},
+ year = {2017}
+}
+
+@article{laplace1774memoire,
+ title = {Mémoire sur la probabilité des causes par les événements.},
+ author = {Laplace, Pierre-Simon},
+ journal = {M\'em. De Math. Et Phys. Pr\'esent\'es à l’Acad. Roy. Des Sci},
+ volume = {6},
+ pages = {621–656},
+ year = {1774}
+}
+
+@article{lecun1998gradient,
+ title = {Gradient-based learning applied to document recognition},
+ author = {LeCun, Yann and Bottou, L{\'e}on and Bengio, Yoshua and Haffner, Patrick},
+ journal = {Proceedings of the IEEE},
+ year = {1998}
+}
+
+@article{mackay1992evidence,
+ title = {The evidence framework applied to classification networks},
+ author = {MacKay, David JC},
+ journal = {Neural computation},
+ year = {1992},
+ publisher = {MIT Press}
+}
+
+@book{mackay2003information,
+ title = {Information theory, inference and learning algorithms},
+ author = {MacKay, David},
+ year = {2003},
+ publisher = {Cambridge university press}
+}
+
+@article{maddox2019simple,
+ title = {A simple baseline for bayesian uncertainty in deep learning},
+ author = {Maddox, Wesley J and Izmailov, Pavel and Garipov, Timur and Vetrov, Dmitry P and Wilson, Andrew Gordon},
+ journal = {Advances in Neural Information Processing Systems},
+ volume = {32},
+ year = {2019}
+}
+
+@inproceedings{martens2010deep,
+ title = {Deep learning via hessian-free optimization.},
+ author = {Martens, James and others},
+ booktitle = {ICML},
+ volume = {27},
+ pages = {735--742},
+ year = {2010}
+}
+
+@inproceedings{martens2015optimizing,
+ title = {Optimizing neural networks with kronecker-factored approximate curvature},
+ author = {Martens, James and Grosse, Roger},
+ booktitle = {International conference on machine learning},
+ pages = {2408--2417},
+ year = {2015},
+ organization = {PMLR}
+}
+
+@article{martens2020new,
+ title = {New insights and perspectives on the natural gradient method},
+ author = {Martens, James},
+ journal = {The Journal of Machine Learning Research},
+ volume = {21},
+ number = {1},
+ pages = {5776--5851},
+ year = {2020},
+ publisher = {JMLRORG}
+}
+
+@article{miani2022laplacian,
+ title = {Laplacian Autoencoders for Learning Stochastic Representations},
+ author = {Miani, Marco and Warburg, Frederik and Moreno-Mu{\~n}oz, Pablo and Detlefsen, Nicke Skafte and Hauberg, S{\o}ren},
+ journal = {arXiv preprint arXiv:2206.15078},
+ year = {2022}
+}
+
+@article{netzer2011reading,
+ title = {Reading digits in natural images with unsupervised feature learning},
+ author = {Netzer, Yuval and Wang, Tao and Coates, Adam and Bissacco, Alessandro and Wu, Bo and Ng, Andrew Y},
+ year = {2011}
+}
+
+@article{nguyen2017variational,
+ title = {Variational continual learning},
+ author = {Nguyen, Cuong V and Li, Yingzhen and Bui, Thang D and Turner, Richard E},
+ journal = {arXiv preprint arXiv:1710.10628},
+ year = {2017}
+}
+
+@book{nocedal1999numerical,
+ title = {Numerical optimization},
+ author = {Nocedal, Jorge and Wright, Stephen J},
+ year = {1999},
+ publisher = {Springer}
+}
+
+@article{osawa2019practical,
+ title = {Practical deep learning with Bayesian principles},
+ author = {Osawa, Kazuki and Swaroop, Siddharth and Khan, Mohammad Emtiyaz E and Jain, Anirudh and Eschenhagen, Runa and Turner, Richard E and Yokota, Rio},
+ journal = {Advances in neural information processing systems},
+ volume = {32},
+ year = {2019}
+}
+
+@article{pascanu2013revisiting,
+ title = {Revisiting natural gradient for deep networks},
+ author = {Pascanu, Razvan and Bengio, Yoshua},
+ journal = {arXiv preprint arXiv:1301.3584},
+ year = {2013}
+}
+
+@incollection{paszke2019pytorch,
+ title = {PyTorch: An Imperative Style, High-Performance Deep Learning Library},
+ author = {Paszke, Adam and Gross, Sam and Massa, Francisco and Lerer, Adam and Bradbury, James and Chanan, Gregory and Killeen, Trevor and Lin, Zeming and Gimelshein, Natalia and Antiga, Luca and Desmaison, Alban and Kopf, Andreas and Yang, Edward and DeVito, Zachary and Raison, Martin and Tejani, Alykhan and Chilamkurthy, Sasank and Steiner, Benoit and Fang, Lu and Bai, Junjie and Chintala, Soumith},
+ booktitle = {Advances in Neural Information Processing Systems}
+}
+
+@article{pleiss2020fast,
+ title = {Fast matrix square roots with applications to Gaussian processes and Bayesian optimization},
+ author = {Pleiss, Geoff and Jankowiak, Martin and Eriksson, David and Damle, Anil and Gardner, Jacob},
+ journal = {Advances in Neural Information Processing Systems},
+ volume = {33},
+ pages = {22268--22281},
+ year = {2020}
+}
+
+@article{rajpurkar2017mura,
+ title = {Mura: Large dataset for abnormality detection in musculoskeletal radiographs},
+ author = {Rajpurkar, Pranav and Irvin, Jeremy and Bagul, Aarti and Ding, Daisy and Duan, Tony and Mehta, Hershel and Yang, Brandon and Zhu, Kaylie and Laird, Dillon and Ball, Robyn L and others},
+ journal = {arXiv preprint arXiv:1712.06957},
+ year = {2017}
+}
+
+@article{ritter2021tyxe,
+ title = {Ty{X}e: Pyro-based {B}ayesian neural nets for {P}ytorch},
+ author = {Ritter, Hippolyt and Karaletsos, Theofanis},
+ journal = {arXiv preprint arXiv:2110.00276},
+ year = {2021}
+}
+
+@article{rue2017bayesian,
+ title = {Bayesian computing with INLA: a review},
+ author = {Rue, H{\aa}vard and Riebler, Andrea and S{\o}rbye, Sigrunn H and Illian, Janine B and Simpson, Daniel P and Lindgren, Finn K},
+ journal = {Annual Review of Statistics and Its Application},
+ volume = {4},
+ pages = {395--421},
+ year = {2017},
+ publisher = {Annual Reviews}
+}
+
+@article{sabne2020xla,
+ title = {XLA: Compiling Machine Learning for Peak Performance},
+ author = {Sabne, Amit},
+ year = {2020}
+}
+
+@article{shannon1948mathematical,
+ title = {A mathematical theory of communication},
+ author = {Shannon, Claude Elwood},
+ journal = {The Bell system technical journal},
+ volume = {27},
+ number = {3},
+ year = {1948},
+ publisher = {Nokia Bell Labs}
+}
+
+@inproceedings{sutskever2013importance,
+ title = {On the importance of initialization and momentum in deep learning},
+ author = {Sutskever, Ilya and Martens, James and Dahl, George and Hinton, Geoffrey},
+ booktitle = {International conference on machine learning},
+ pages = {1139--1147},
+ year = {2013},
+ organization = {PMLR}
+}
+
+@article{wilson2020bayesian,
+ title = {Bayesian deep learning and a probabilistic perspective of generalization},
+ author = {Wilson, Andrew G and Izmailov, Pavel},
+ journal = {Advances in neural information processing systems},
+ year = {2020}
+}
+
+@article{wu2018deterministic,
+ title = {Deterministic variational inference for robust bayesian neural networks},
+ author = {Wu, Anqi and Nowozin, Sebastian and Meeds, Edward and Turner, Richard E and Hern{\'a}ndez-Lobato, Jos{\'e} Miguel and Gaunt, Alexander L},
+ journal = {arXiv preprint arXiv:1810.03958},
+ year = {2018}
+}
+
+@article{wu2019logan,
+ title = {Logan: Latent optimisation for generative adversarial networks},
+ author = {Wu, Yan and Donahue, Jeff and Balduzzi, David and Simonyan, Karen and Lillicrap, Timothy},
+ journal = {arXiv preprint arXiv:1912.00953},
+ year = {2019}
+}
+
+@article{yip1986note,
+ title = {A note on the stability of solving a rank-p modification of a linear system by the Sherman--Morrison--Woodbury formula},
+ author = {Yip, Elizabeth L},
+ journal = {SIAM Journal on Scientific and Statistical Computing},
+ volume = {7},
+ number = {2},
+ pages = {507--513},
+ year = {1986},
+ publisher = {SIAM}
+}
+
+@inproceedings{zhang2018noisy,
+ title = {Noisy natural gradient as variational inference},
+ author = {Zhang, Guodong and Sun, Shengyang and Duvenaud, David and Grosse, Roger},
+ booktitle = {International Conference on Machine Learning},
+ pages = {5852--5861},
+ year = {2018},
+ organization = {PMLR}
+}
diff --git a/main.tex b/main.tex
@@ -0,0 +1,369 @@
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% kaobook
+% LaTeX Template
+% Version 1.3 (December 9, 2021)
+%
+% This template originates from:
+% https://www.LaTeXTemplates.com
+%
+% For the latest template development version and to make contributions:
+% https://github.com/fmarotta/kaobook
+%
+% Authors:
+% Federico Marotta (federicomarotta@mail.com)
+% Based on the doctoral thesis of Ken Arroyo Ohori (https://3d.bk.tudelft.nl/ken/en)
+% and on the Tufte-LaTeX class.
+% Modified for LaTeX Templates by Vel (vel@latextemplates.com)
+%
+% License:
+% CC0 1.0 Universal (see included MANIFEST.md file)
+%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+%----------------------------------------------------------------------------------------
+% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
+%----------------------------------------------------------------------------------------
+
+\documentclass[
+ a4paper, % Page size
+ fontsize=10pt, % Base font size
+ twoside=true, % Use different layouts for even and odd pages (in particular, if twoside=true, the margin column will be always on the outside)
+ %open=any, % If twoside=true, uncomment this to force new chapters to start on any page, not only on right (odd) pages
+ %chapterentrydots=true, % Uncomment to output dots from the chapter name to the page number in the table of contents
+ numbers=noenddot, % Comment to output dots after chapter numbers; the most common values for this option are: enddot, noenddot and auto (see the KOMAScript documentation for an in-depth explanation)
+]{kaobook}
+
+% Choose the language
+\ifxetexorluatex
+ \usepackage{polyglossia}
+ \setmainlanguage{english}
+\else
+ \usepackage[english]{babel} % Load characters and hyphenation
+\fi
+\usepackage[english=british]{csquotes} % English quotes
+
+% Load packages for testing
+\usepackage{lipsum}
+%\usepackage{showframe} % Uncomment to show boxes around the text area, margin, header and footer
+%\usepackage{showlabels} % Uncomment to output the content of \label commands to the document where they are used
+
+% Load the bibliography package
+\usepackage{kaobiblio}
+\addbibresource{main.bib} % Bibliography file
+
+% Load mathematical packages for theorems and related environments
+\usepackage[framed=true]{kaotheorems}
+
+% Load the package for hyperreferences
+\usepackage{kaorefs}
+\usepackage[utf8]{inputenc}
+
+\usepackage{amsmath,amssymb,amsthm,bm}
+\usepackage{censor}
+
+\usepackage{tikz}
+\usepackage{pgfplots}
+\DeclareUnicodeCharacter{2212}{−}
+\usepgfplotslibrary{groupplots,dateplot}
+\usetikzlibrary{patterns,shapes.arrows,plotmarks,angles,quotes}
+\pgfplotsset{compat=newest}
+\def\figurewidth{147pt}
+\def\figureheight{147pt}
+
+\usepackage{xcolor}
+\colorlet{vcol}{green!70!black}
+\colorlet{xcol}{blue!85!black}
+\colorlet{unitcol}{xcol!60!black!85}
+\colorlet{unitcol2}{vcol!60!black!85}
+\tikzstyle{vector}=[->,very thick,xcol]
+\tikzstyle{mydashed}=[dash pattern=on 2pt off 2pt]
+\newcommand{\tick}[2]{\draw[thick] (#1) ++ (#2:0.1) --++ (#2-180:0.2)} %0.03*\xmax
+% \newcommand{\tick}[2]{\draw[thick, line width=0.75mm] (#1) ++ (#2:0.1) --++ (#2-180:0.2)} %0.03*\xmax
+
+
+\input{macros.tex}
+
+\graphicspath{{examples/documentation/figures/}{figures/}} % Paths in which to look for images
+
+\makeindex[columns=3, title=Alphabetical Index, intoc] % Make LaTeX produce the files required to compile the index
+
+\makeglossaries % Make LaTeX produce the files required to compile the glossary
+\input{glossary.tex} % Include the glossary definitions
+
+\makenomenclature % Make LaTeX produce the files required to compile the nomenclature
+
+% Reset sidenote counter at chapters
+%\counterwithin*{sidenote}{chapter}
+
+\SetKwComment{Comment}{/* }{ */}
+\SetKwInOut{Input}{Input}
+\SetKwInOut{Output}{Output}
+
+%----------------------------------------------------------------------------------------
+
+\begin{document}
+
+%----------------------------------------------------------------------------------------
+% BOOK INFORMATION
+%----------------------------------------------------------------------------------------
+
+% \titlehead{M.Sc. Thesis}
+\subject{Masters of Science in Engineering}
+
+\title{Effortless Bayesian Deep Learning}
+\subtitle{Tapping into the Potential of Modern Optimizers}
+
+\author[Silas Brack]{Silas Brack}
+
+\date{February 2023}
+
+\publishers{DTU Compute}
+
+%----------------------------------------------------------------------------------------
+
+\frontmatter % Denotes the start of the pre-document content, uses roman numerals
+
+%----------------------------------------------------------------------------------------
+% OPENING PAGE
+%----------------------------------------------------------------------------------------
+
+%\makeatletter
+%\extratitle{
+% % In the title page, the title is vspaced by 9.5\baselineskip
+% \vspace*{9\baselineskip}
+% \vspace*{\parskip}
+% \begin{center}
+% % In the title page, \huge is set after the komafont for title
+% \usekomafont{title}\huge\@title
+% \end{center}
+%}
+%\makeatother
+
+%----------------------------------------------------------------------------------------
+% COPYRIGHT PAGE
+%----------------------------------------------------------------------------------------
+
+\makeatletter
+\uppertitleback{\@titlehead} % Header
+
+\lowertitleback{
+ \textbf{Author}\\
+ Silas Brack (s174433)
+
+ \medskip
+
+ \textbf{Supervisors}\\
+ S{\o}ren Hauberg (\href{mailto:sohau@dtu.dk}{sohau@dtu.dk})\\
+ Frederik Warburg (\href{mailto:frwa@dtu.dk}{frwa@dtu.dk})\\
+ Marco Miani (\href{mailto:mmia@dtu.dk}{mmia@dtu.dk})
+
+ \medskip
+
+ \textbf{Project period}\\ 12\textsuperscript{th} September 2022 -- 12\textsuperscript{th} February 2023 \medskip
+
+ \textbf{ECTS points}\\ 30 \medskip
+
+ \textbf{Education}\\ Master of Science (M.Sc.) \medskip
+
+ \textbf{Class}\\ Public \medskip
+
+ \textbf{Edition}\\ 1\textsuperscript{st}
+
+ \medskip
+
+ \textbf{Colophon} \\
+ This document was typeset with the help of \href{https://sourceforge.net/projects/koma-script/}{\KOMAScript} and \href{https://www.latex-project.org/}{\LaTeX} using the \href{https://github.com/fmarotta/kaobook/}{kaobook} class.
+
+ The source code of this book is available at:\\\url{https://github.com/fmarotta/kaobook}
+
+ \medskip
+
+ \textbf{Publisher} \\
+ \@publishers, February 2023
+}
+\makeatother
+
+%----------------------------------------------------------------------------------------
+% DEDICATION
+%----------------------------------------------------------------------------------------
+
+\dedication{
+ % \blackout
+ % {It’s an incredibly exciting thing, this one, meaningless life of yours.}\\
+ {You don't have to have a dream.}\\
+ % {Life is about learning as much as you can as much as you can.}\\
+ \flushright -- Tim Minchin
+}
+
+%----------------------------------------------------------------------------------------
+% OUTPUT TITLE PAGE AND PREVIOUS
+%----------------------------------------------------------------------------------------
+
+% Note that \maketitle outputs the pages before here
+
+\maketitle
+
+%----------------------------------------------------------------------------------------
+% PREFACE
+%----------------------------------------------------------------------------------------
+
+\input{chapters/preface.tex}
+\index{preface}
+
+\input{chapters/acknowledgments.tex}
+\index{acknowledgments}
+
+\input{chapters/abstract.tex}
+\index{abstract}
+
+%----------------------------------------------------------------------------------------
+% TABLE OF CONTENTS & LIST OF FIGURES/TABLES
+%----------------------------------------------------------------------------------------
+
+\begingroup % Local scope for the following commands
+
+% Define the style for the TOC, LOF, and LOT
+%\setstretch{1} % Uncomment to modify line spacing in the ToC
+%\hypersetup{linkcolor=blue} % Uncomment to set the colour of links in the ToC
+\setlength{\textheight}{230\hscale} % Manually adjust the height of the ToC pages
+
+% Turn on compatibility mode for the etoc package
+\etocstandarddisplaystyle % "toc display" as if etoc was not loaded
+\etocstandardlines % "toc lines" as if etoc was not loaded
+
+\tableofcontents % Output the table of contents
+
+\listoffigures % Output the list of figures
+
+% Comment both of the following lines to have the LOF and the LOT on different pages
+\let\cleardoublepage\bigskip
+\let\clearpage\bigskip
+
+\listoftables % Output the list of tables
+
+\listofalgorithms
+
+\endgroup
+
+%----------------------------------------------------------------------------------------
+% MAIN BODY
+%----------------------------------------------------------------------------------------
+
+\mainmatter % Denotes the start of the main document content, resets page numbering and uses arabic numbers
+\setchapterstyle{kao} % Choose the default chapter heading style
+
+% \newpage
+
+\input{chapters/introduction.tex}
+\input{chapters/theory.tex}
+\input{chapters/literature.tex}
+\input{chapters/training.tex}
+\input{chapters/sampling.tex}
+\input{chapters/mixture.tex}
+\input{chapters/experiments.tex}
+\input{chapters/results.tex}
+\input{chapters/conclusion.tex}
+
+%----------------------------------------------------------------------------------------
+% APPENDIX
+%----------------------------------------------------------------------------------------
+
+\appendix % From here onwards, chapters are numbered with letters, as is the appendix convention
+
+\pagelayout{wide} % No margins
+\addpart{Appendix}
+\pagelayout{margin} % Restore margins
+
+\input{chapters/appendix.tex}
+
+%----------------------------------------------------------------------------------------
+
+\backmatter % Denotes the end of the main document content
+\setchapterstyle{plain} % Output plain chapters from this point onwards
+
+%----------------------------------------------------------------------------------------
+% BIBLIOGRAPHY
+%----------------------------------------------------------------------------------------
+
+% The bibliography needs to be compiled with biber using your LaTeX editor, or on the command line with 'biber main' from the template directory
+
+\defbibnote{bibnote}{Here are the references in citation order.\par\bigskip} % Prepend this text to the bibliography
+\printbibliography[heading=bibintoc, title=Bibliography, prenote=bibnote] % Add the bibliography heading to the ToC, set the title of the bibliography and output the bibliography note
+
+% %----------------------------------------------------------------------------------------
+% % NOMENCLATURE
+% %----------------------------------------------------------------------------------------
+
+% % The nomenclature needs to be compiled on the command line with 'makeindex main.nlo -s nomencl.ist -o main.nls' from the template directory
+
+% \nomenclature{$c$}{Speed of light in a vacuum inertial frame}
+% \nomenclature{$h$}{Planck constant}
+
+% \renewcommand{\nomname}{Notation} % Rename the default 'Nomenclature'
+% \renewcommand{\nompreamble}{The next list describes several symbols that will be later used within the body of the document.} % Prepend this text to the nomenclature
+
+% \printnomenclature % Output the nomenclature
+
+% %----------------------------------------------------------------------------------------
+% % GREEK ALPHABET
+% % Originally from https://gitlab.com/jim.hefferon/linear-algebra
+% %----------------------------------------------------------------------------------------
+
+% \vspace{1cm}
+
+% {\usekomafont{chapter}Greek Letters with Pronunciations}
+% \begin{center}
+% \newcommand{\pronounced}[1]{\hspace*{.2em}\small\textit{#1}}
+% \begin{tabular}{l l @{\hspace*{3em}} l l}
+% \toprule
+% Character & Name & Character & Name \\
+% \midrule
+% $\alpha$ & alpha \pronounced{AL-fuh} & $\nu$ & nu \pronounced{NEW} \\
+% $\beta$ & beta \pronounced{BAY-tuh} & $\xi$, $\Xi$ & xi \pronounced{KSIGH} \\
+% $\gamma$, $\Gamma$ & gamma \pronounced{GAM-muh} & o & omicron \pronounced{OM-uh-CRON} \\
+% $\delta$, $\Delta$ & delta \pronounced{DEL-tuh} & $\pi$, $\Pi$ & pi \pronounced{PIE} \\
+% $\epsilon$ & epsilon \pronounced{EP-suh-lon} & $\rho$ & rho \pronounced{ROW} \\
+% $\zeta$ & zeta \pronounced{ZAY-tuh} & $\sigma$, $\Sigma$ & sigma \pronounced{SIG-muh} \\
+% $\eta$ & eta \pronounced{AY-tuh} & $\tau$ & tau \pronounced{TOW (as in cow)} \\
+% $\theta$, $\Theta$ & theta \pronounced{THAY-tuh} & $\upsilon$, $\Upsilon$ & upsilon \pronounced{OOP-suh-LON} \\
+% $\iota$ & iota \pronounced{eye-OH-tuh} & $\phi$, $\Phi$ & phi \pronounced{FEE, or FI (as in hi)} \\
+% $\kappa$ & kappa \pronounced{KAP-uh} & $\chi$ & chi \pronounced{KI (as in hi)} \\
+% $\lambda$, $\Lambda$ & lambda \pronounced{LAM-duh} & $\psi$, $\Psi$ & psi \pronounced{SIGH, or PSIGH} \\
+% $\mu$ & mu \pronounced{MEW} & $\omega$, $\Omega$ & omega \pronounced{oh-MAY-guh} \\
+% \bottomrule
+% \end{tabular} \\[1.5ex]
+% Capitals shown are the ones that differ from Roman capitals.
+% \end{center}
+
+%----------------------------------------------------------------------------------------
+% GLOSSARY
+%----------------------------------------------------------------------------------------
+
+% The glossary needs to be compiled on the command line with 'makeglossaries main' from the template directory
+
+\setglossarystyle{listgroup} % Set the style of the glossary (see https://en.wikibooks.org/wiki/LaTeX/Glossary for a reference)
+\printglossary[title=Special Terms, toctitle=List of Terms] % Output the glossary, 'title' is the chapter heading for the glossary, toctitle is the table of contents heading
+
+% %----------------------------------------------------------------------------------------
+% % INDEX
+% %----------------------------------------------------------------------------------------
+
+% % The index needs to be compiled on the command line with 'makeindex main' from the template directory
+
+% \printindex % Output the index
+
+% %----------------------------------------------------------------------------------------
+% % BACK COVER
+% %----------------------------------------------------------------------------------------
+
+% % If you have a PDF/image file that you want to use as a back cover, uncomment the following lines
+
+% %\clearpage
+% %\thispagestyle{empty}
+% %\null%
+% %\clearpage
+% %\includepdf{cover-back.pdf}
+
+% %----------------------------------------------------------------------------------------
+
+\end{document}