%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% IHEID THESIS TEMPLATE %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Use this template to produce a standard thesis that meets the Geneva Graduate Institute requirements.
% Inherited a lot of functionality from ociamthesis Oxford Thesis template.
% Originally by Keith A. Gillow (gillow@maths.ox.ac.uk), 
% modified by Sam Evans, John McManigle, and Ulrik Lyngs
%
% Broad permissions are granted to use, modify, and distribute this software
% as specified in the MIT License included in this distribution's LICENSE file.

%%%% PAGE LAYOUT
% Pass this in from YAML
\documentclass[a4paper, $if(page-layout)$$page-layout$$endif$]{report}
\usepackage[includehead,hmargin={3.1cm, 3.1cm}, vmargin={2.5cm,2.7cm}, headsep=.8cm,footskip=1.2cm]{geometry}

\usepackage{fancyhdr}
\setlength{\headheight}{15pt}
\fancyhf{} % clear the header and footers
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter. #1}{\thechapter. #1}}
\renewcommand{\sectionmark}[1]{\markright{\thesection. #1}} 
\renewcommand{\headrulewidth}{0pt}
\fancyhead[LO]{\emph{\leftmark}}
$if(page-layout.twoside)$
\fancyhead[RE]{\emph{\rightmark}} 
\fancyhead[RO,LE]{\emph{\thepage}}
$endif$

\fancypagestyle{plain}{\fancyhf{}\fancyfoot[C]{\emph{\thepage}}}

\fancyfoot[C]{\emph{\thepage}}

% This adds a "DRAFT" footer to every normal page.  (The first page of each chapter is not a "normal" page.) Page numbers are also printed on the right side.
$if(draft)$
\fancyfoot[C]{\emph{DRAFT: \today}}
\fancyfoot[R]{\emph{\thepage}}
\usepackage{lineno}
\linenumbers
$endif$

%%%%% FONTS
\RequirePackage{microtype} % this makes fonts almost imperceptibly smoother
\RequirePackage{droidserif} % Droid Serif font for body
\RequirePackage[T1]{fontenc} % requires XeLatex or LuaTex (remove to use pdfLaTex)
%\RequirePackage[utf8]{inputenc} % ignored when using XeLaTex or LuaTex (uncomment when using pdfLaTex)
\RequirePackage{fontspec} % requires XeLatex or LuaTex (remove to use pdfLaTex)
% For the headings we will use Helvetica
\RequirePackage{helvet}
\usepackage[immediate]{silence}
\WarningFilter[temp]{latex}{Command} % silence the warning
\usepackage{sectsty}
\DeactivateWarningFilters[temp] % So nothing unrelated gets silenced
\allsectionsfont{\sffamily}
% For the main text and equations we will use Baskerville and Palatino (Old version)
% \RequirePackage{mathpazo}
% \RequirePackage{baskervald}
% For formatting code or package names, we will use Lucida Console
\RequirePackage{zi4} %Lucida console TypeWriter Font
% Enable strikethrough
\usepackage[normalem]{ulem}


% This highlights (in blue) corrections marked with (for words) \mccorrect{blah} or (for whole
% paragraphs) \begin{mccorrection} . . . \end{mccorrection}.  This can be useful for sending a PDF of
% your corrected thesis to your examiners for review.  Turn it off, and the blue disappears.
$if(params.corrections)$
\correctionstrue
$endif$

% UL 30 Nov 2018 pandoc puts lists in 'tightlist' command when no space between bullet points in Rmd file
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
 
% UL 1 Dec 2018, fix to include code in shaded environments
$if(highlighting-macros)$
$highlighting-macros$
%UL 2 Dec 2018 add a bit of white space before and after code blocks
\renewenvironment{Shaded}
{
  \vspace{4pt}%
  \begin{snugshade}%
}{%
  \end{snugshade}%
  \vspace{4pt}%
}
$endif$

%UL 2 Dec 2018 reduce whitespace around verbatim (code) environments
\usepackage{etoolbox}
\makeatletter
\preto{\@verbatim}{\topsep=0pt \partopsep=0pt }
\makeatother

%UL 15 Oct 2019, enable link highlighting to be turned off from YAML
% \usepackage[dvipsnames]{color}
% \usepackage[colorlinks=true,pdfpagelabels,hidelinks=$hidelinks$]{hyperref}
\RequirePackage[colorlinks=true,linkcolor=red,
citecolor=red,filecolor=red,urlcolor=black]{hyperref} % uses IHEID red for external links
% \hypersetup{citecolor=red}

%%%%% BIBLIOGRAPHY SETUP
% Note that your bibliography will require some tweaking depending on your department, preferred format, etc.
% The options included below are just very basic "sciencey" and "humanitiesey" options to get started.
% If you've not used LaTeX before, I recommend reading a little about biblatex/biber and getting started with it.
% If you're already a LaTeX pro and are used to natbib or something, modify as necessary.
% Either way, you'll have to choose and configure an appropriate bibliography format...

% The science-type option: numerical in-text citation with references in order of appearance.
% \usepackage[style=numeric-comp, sorting=none, backend=biber, doi=false, isbn=false]{biblatex}
% \newcommand*{\bibtitle}{References}

% The humanities-type option: author-year in-text citation with an alphabetical works cited.
% \usepackage[style=authoryear, sorting=nyt, backend=biber, maxcitenames=2, useprefix, doi=false, isbn=false]{biblatex}
% \newcommand*{\bibtitle}{Works Cited}

\usepackage[style=$bib-style$, sorting=$bib-sorting$, backend=biber, maxcitenames=2, useprefix,
doi=true, isbn=true, dashed=false, uniquename=false]{biblatex}
%-----------------------------------------------------
% More versatile and less restrictive version that lets users apply biblatex style options from the Yaml header in the index.Rmd file.
% Uncomment and copy the YAML part below in the YAML header of your Index file.
% \usepackage[style=$bib-style$, sorting=$bib-sorting$, backend=biber, maxcitenames=$max-authors$, useprefix,
% doi=$if(doi-in-bibliography)$$doi-in-bibliography$$else$false$endif$, isbn=$bib-isbn$, dashed=$bib-dashed$, uniquename=false]{biblatex}

% Copy the part below in your YAML header to set the options from there manually.
% bibliography: bib/references.bib #Set your bibliography file here.
% packagebibliography: bib/packages.bib #Sets the automatically generated R-Package Bibliography.
% bibliography-heading-in-pdf: References
% bib-style: authoryear # See https://www.overleaf.com/learn/latex/biblatex_citation_styles for a list of the commonly used built-in citations styles of biblatex.
% bib-sorting: nyt #See https://www.overleaf.com/learn/latex/Articles/Getting_started_with_BibLaTeX for different bibliography sorting options.
% max-authors: 2 # This sets the maximum of author names that are displayed before an et.al kicks in.
% doi-in-bibliography: true #set to true if you want DOI's to be shown in the bibliography.
% bib-isbn: true #Set to true if you want the ISBN to be displayed
% bib-dashed: false #Set to true if you want dashed entries instead of repeaded author names if your bibliography contains two entries of the same author.
% bib-smallfont: false #Set to true if you want a small fontsize for your bibliography.
% citeall: true #Set this to true if you want all elements in your .bib file to appear in your bibliography (i.e. all R-packages you used).
%-----------------------------------------------------

% Change this to the name of your .bib file (usually exported from a citation manager like Zotero or EndNote).
\addbibresource{$bibliography$}
\addbibresource{bib/packages.bib}

% This makes the bibliography left-aligned (not 'justified') and slightly smaller font.
\renewcommand*{\bibfont}{\raggedright$if(bib-smallfont)$\small$endif$}

% Cites all the entries in the various bibliography files
$if(citeall)$
\nocite{*}
$endif$

% Uncomment this if you want equation numbers per section (2.3.12), instead of per chapter (2.18):
%\numberwithin{equation}{subsection}


%%%%% THESIS / TITLE PAGE INFORMATION
% Everybody needs to complete the following:
\title{$doctitle$}
\author{$author$}

% JEM: Lengths for single spacing (ie separate abstract, captions), front matter (abstract,
%   acknowledgements, table of contents, etc), and main body text.
\newlength{\singlebaselineskip}
\newlength{\frontmatterbaselineskip}
\newlength{\textbaselineskip}

\setlength{\singlebaselineskip}{\baselineskip}
\setlength{\frontmatterbaselineskip}{17pt plus1pt minus1pt}
\setlength{\textbaselineskip}{22pt plus2pt}

%
% Environments
%

% This macro define an environment for front matter that is always 
% single column even in a double-column document.

\makeatletter
\newenvironment{alwayssingle}{%
       \@restonecolfalse
       \if@twocolumn\@restonecoltrue\onecolumn
       \else\if@openright\cleardoublepage\else\clearpage\fi
       \fi}%
       {\if@restonecol\twocolumn
       \else\newpage\thispagestyle{empty}\fi}
\makeatother

% DEDICATION
%
% The dedication environment makes sure the dedication gets its
% own page and is set out in verse format.

\newenvironment{dedication}
{\begin{alwayssingle}
  \thispagestyle{empty}
  \vspace*{\fill} \begin{center}}
{\end{center}\vspace*{\fill}\vspace*{\fill} \end{alwayssingle}}


% ACKNOWLEDGEMENTS
%
% The acknowledgements environment puts a large, bold, centered 
% "Acknowledgements" label at the top of the page. The acknowledgements
% themselves appear in a quote environment, i.e. tabbed in at both sides, and
% on its own page.

\newenvironment{acknowledgements}%
{   \begin{alwayssingle}\chapter*{Acknowledgements}
    \thispagestyle{empty}
	\pagestyle{empty}
	\setlength{\baselineskip}{\frontmatterbaselineskip}
}
{\end{alwayssingle}}

%ROMANPAGES
%
% The romanpages environment set the page numbering to lowercase roman one
% for the contents and figures lists. It also resets
% page-numbering for the remainder of the dissertation (arabic, starting at 1).
%
% Edited by JEM

\newenvironment{romanpages}
{\cleardoublepage\setlength{\baselineskip}{\frontmatterbaselineskip}\setcounter{page}{1}\renewcommand{\thepage}{\roman{page}}}
{\cleardoublepage\setcounter{page}{1}\renewcommand{\thepage}{\arabic{page}}}

%%%%% YOUR OWN PERSONAL MACROS
% This is a good place to dump your own LaTeX macros as they come up.

% To make text superscripts shortcuts
	% \renewcommand{\th}{\textsuperscript{th}} % ex: I won 4\th place
	% \newcommand{\nd}{\textsuperscript{nd}}
	% \renewcommand{\st}{\textsuperscript{st}}
	% \newcommand{\rd}{\textsuperscript{rd}}

% Setup different header and footer for title page(s)
\fancypagestyle{firstpages} {
	\fancyhf{} % clear header and footer
	\renewcommand{\headrulewidth}{0pt} % remove header line
	\renewcommand{\footrulewidth}{0pt} % remove footer line
}

% Setup custom title format
\makeatletter
\renewcommand\maketitle
  {\vspace*{3cm} % distance from top of page to title
  \centering{
  \Large\sffamily\textbf{\@title}
  \vspace{1cm}\par % distance to programme statement
  \centering\large\sffamily\textbf{THESIS}\\
  submitted at the Geneva Graduate Institute\\
  in fulfillment of the requirements of the\\
  $department$
  \vspace*{0.5cm}\par % distance to author
  \centering\large\sffamily{
	by\\
	\vspace{0.5cm} % distance between 'by' and author name
	$firstnames$ $lastnames$
  }
  \bigskip\par
  }
  }
\makeatother

% \usepackage[nohints,tight]{minitoc} 
% 	\setcounter{minitocdepth}{2} 
	% Generates mini tables of contents per chapter
	
% % JEM: The following fixes minitoc to not be multiple-spaced.
% \makeatletter
% \let\oldmtc@verse\mtc@verse
% \renewcommand{\mtc@verse}[1]{\oldmtc@verse{#1}\setlength{\baselineskip}{\z@}}
% \makeatother

% For the abbreviations
\usepackage{enumitem}
\newlist{abbrv}{itemize}{1}
\setlist[abbrv,1]{label=,labelwidth=1in,align=parleft,itemsep=0.1\baselineskip,leftmargin=!}

% Packages used to display math
\usepackage{nicefrac}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{textcomp}
\usepackage{adjustbox} % adjust things to page width


\usepackage{longtable} 
	% Allows tables to span multiple pages (this package must be called before hyperref)

\usepackage{colortbl, xcolor}
  % Allows cells to be colored. Required by kableExtra

\usepackage[font=small,labelfont=bf]{caption} 
	% Nicer captions

\usepackage{multicol,multirow,array,xparse,pdflscape} 
	% Used to make multiple columns for the indices and for creating columns that span multiple rows in tables

\usepackage{rotating} 
	% To allow tables in landscape mode

\usepackage{booktabs} 
	% For better looking tables

\usepackage{pdfpages} 
	% Allows multi-page pdfs to be inserted as graphics
	
\usepackage{graphicx}
\graphicspath{{figures/}{_bookdown_files/}}

\usepackage{float} % Use the 'float' package for table and figure alignment
\floatplacement{figure}{H} % Make every figure with caption = h

\usepackage{blkarray} % Adding annotations to border for matrices

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ACTUAL DOCUMENT HERE  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

%%%%%%%%%%%%%%%%%
% TITLEPAGES
%%%%%%%%%%%%%%%%%

% TITLEPAGE 1
\begin{titlepage}
\hspace*{-1.25cm}
\includegraphics[width=0.4\linewidth]{iheid.png}

\maketitle

\vspace*{1cm}

$if(phd)$
\sffamily{Thesis No. $thesisno$}
$endif$

\vspace*{1cm}
\sffamily\textbf{Supervisor: $supervisor$}\\
\sffamily\textbf{Second Reader: $secondreader$}
$if(phd)$
\\\sffamily\textbf{External Examiner: $examiner$}
$endif$

\vspace*{1cm}
\sffamily\textbf{Geneva}\\
\sffamily\textbf{\the\year}

\thispagestyle{firstpages} % this line is needed to set a different header/footer than for the rest of the document
\end{titlepage}

% TITLEPAGE 2 (empty page)
$if(phd)$
\newpage\null\thispagestyle{empty}\newpage

% TITLEPAGE 3
\begin{titlepage}
\vspace*{5cm}

\centering\Large\sffamily\textbf{$doctitle$}

\vspace*{10cm}

\large\raisebox{0.1em}{\textcopyright} \sffamily\textbf{\the\year} \hspace{1em} \sffamily\textbf{\MakeUppercase{$lastnames$}}

\thispagestyle{firstpages}
\end{titlepage}

% TITLEPAGE 4
\begin{titlepage}
\centering\sffamily{
INSTITUT DE HAUTES ETUDES INTERNATIONALES ET DU DEVELOPPEMENT\\
GENEVA GRADUATE INSTITUTE
}

\maketitle

\vspace*{1cm}

\sffamily{Thesis No. $thesisno$}

\vspace*{1cm}
\sffamily\textbf{Geneva}\\
\sffamily\textbf{\the\year}

\thispagestyle{firstpages}
\end{titlepage}

% TITLEPAGE 5
\begin{titlepage}

\includepdf{SecretariatDocument.pdf}

\thispagestyle{firstpages}
\end{titlepage}
$endif$

% TITLEPAGE 6
\begin{titlepage}
\centering\sffamily{
INSTITUT DE HAUTES ETUDES INTERNATIONALES ET DU DEVELOPPEMENT\\
GENEVA GRADUATE INSTITUTE
}

\vspace*{1cm}

\begin{center}
\sffamily\large\textbf{RESUME / ABSTRACT}

\vspace*{1cm}

\sffamily\normalsize{$resume$}
\end{center}

\thispagestyle{firstpages}
\end{titlepage}
\restoregeometry % Reset page geometry
\setcounter{page}{1} % Reset page counter

%%%%%%%%%%%%%%%%%
% END TITLE PAGES
%%%%%%%%%%%%%%%%%


%%%%% CHOOSE YOUR LINE SPACING HERE
% This is the official option.  Use it for your submission copy and library copy:
\setlength{\textbaselineskip}{22pt plus2pt}
% This is closer spacing (about 1.5-spaced) that you might prefer for your personal copies:
%\setlength{\textbaselineskip}{18pt plus2pt minus1pt}

% You can set the spacing here for the roman-numbered pages (acknowledgements, table of contents, etc.)
\setlength{\frontmatterbaselineskip}{17pt plus1pt minus1pt}

% UL: You can set the general paragraph spacing here - I've set it to 2pt (was 0) so
% it's less claustrophobic
\setlength{\parskip}{2pt plus 1pt}

% Leave this line alone; it gets things started for the real document.
\setlength{\baselineskip}{\textbaselineskip}


%%%%% CHOOSE YOUR SECTION NUMBERING DEPTH HERE
% You have two choices.  First, how far down are sections numbered?  (Below that, they're named but
% don't get numbers.)  Second, what level of section appears in the table of contents?  These don't have
% to match: you can have numbered sections that don't show up in the ToC, or unnumbered sections that
% do.  Throughout, 0 = chapter; 1 = section; 2 = subsection; 3 = subsubsection, 4 = paragraph...

% The level that gets a number:
\setcounter{secnumdepth}{2}
% The level that shows up in the ToC:
\setcounter{tocdepth}{$toc-depth$}

% JEM: Pages are roman numbered from here, though page numbers are invisible until ToC.  This is in
% keeping with most typesetting conventions.
\begin{romanpages}

%%%%% DEDICATION -- If you'd like one, un-comment the following.
$if(dedication)$
\begin{dedication}
  $dedication$
\end{dedication}
$endif$

%%%%% ACKNOWLEDGEMENTS -- Nothing to do here except comment out if you don't want it.
$if(acknowledgements)$
\begin{acknowledgements}
 	$acknowledgements$
\end{acknowledgements}
$endif$

%%%%% MINI TABLES
% This lays the groundwork for per-chapter, mini tables of contents.  Comment the following line
% (and remove \minitoc from the chapter files) if you don't want this.  Un-comment either of the
% next two lines if you want a per-chapter list of figures or tables.
% $if(mini-toc)$
%   \dominitoc % include a mini table of contents
% $endif$
% $if(mini-lof)$
%   \dominilof  % include a mini list of figures
% $endif$
% $if(mini-lot)$
%   \dominilot  % include a mini list of tables
% $endif$

% This aligns the bottom of the text of each page.  It generally makes things look better.
\flushbottom

% This is where the whole-document ToC appears:
\tableofcontents

$if(lof)$
\listoffigures
	% \mtcaddchapter
  	% \mtcaddchapter is needed when adding a non-chapter (but chapter-like) entity to avoid confusing minitoc
$endif$

% Uncomment to generate a list of tables:
$if(lot)$
\listoftables
  % \mtcaddchapter
$endif$

%%%%% LIST OF ABBREVIATIONS
% This example includes a list of abbreviations.  Look at text/abbreviations.tex to see how that file is
% formatted.  The template can handle any kind of list though, so this might be a good place for a
% glossary, etc.
$if(abbreviations)$
\include{$abbreviations$}
$endif$

% The Roman pages, like the Roman Empire, must come to its inevitable close.
\end{romanpages}

%%%%% CHAPTERS
% Add or remove any chapters you'd like here, by file name (excluding '.tex'):
\flushbottom

% all your chapters and appendices will appear here
$body$


%%%%% REFERENCES

% JEM: Quote for the top of references (just like a chapter quote if you're using them).  Comment to skip.
% \begin{savequote}[8cm]
% The first kind of intellectual and artistic personality belongs to the hedgehogs, the second to the foxes \dots
%   \qauthor{--- Sir Isaiah Berlin \cite{berlin_hedgehog_2013}}
% \end{savequote}

% \setlength{\baselineskip}{0pt} % JEM: Single-space References
% 
{\renewcommand*\MakeUppercase[1]{#1}%
\printbibliography[heading=bibintoc,title={References}]}

\end{document}