\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mythesis}

%% -------------------------------------
%% Packages
%% -------------------------------------
%$ Page layout
\RequirePackage[top=1in, bottom=1in, 
%inner=1.25in, outer=1in]{geometry}
inner=1.25in, outer=1in, twoside]{geometry}
\RequirePackage{amsmath}

%$ Hyper-link ..
\RequirePackage[%
%colorlinks=false,% color links instead of using boxes
%linkcolor=red,% color for internal (intra-document) links
%citecolor=green,% color for bibliographic links
%urlcolor=blue,% color for URL links
hidelinks,
hyperfootnotes=false,% disable links to footnotes (because feature broken)
hyperindex,% make page numbers in index into hyperlinks
pdfstartview={FitH},% startup page view: fit width of page to window
bookmarks
]{hyperref}
\RequirePackage[all]{hypcap}

\AtBeginDocument{\thispagestyle{plain}}

%% Custom header, no upper case, two sided 
\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\nouppercase{\leftmark}}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\setlength{\headheight}{15pt}
\RequirePackage{emptypage}
%% Others
\RequirePackage{booktabs}
\RequirePackage{caption}
\RequirePackage{pgfplotstable}

\RequirePackage{cite}
%\RequirePackage{morefloats}
\RequirePackage{mathrsfs} % script font
\RequirePackage{afterpage}
%% Using Babel allows other languages to be used and mixed-in easily
\RequirePackage[ngerman,english]{babel}
\selectlanguage{english}
\RequirePackage[babel]{csquotes}

\RequirePackage{subfig}
\RequirePackage{tikz}
\RequirePackage{fixltx2e}
\RequirePackage{setspace}
\RequirePackage{verbatim}
\RequirePackage{lipsum}

\RequirePackage[final]{listings}
\RequirePackage{xfrac}
%% Units
%\RequirePackage[]{siunitx}
\RequirePackage[detect-weight=true, detect-family=true]{siunitx}
\RequirePackage{hepnames}
\RequirePackage{array}
%% Various fonts ...
%\RequirePackage[T1]{fontenc}
%\RequirePackage{charter}
%\RequirePackage[expert]{mathdesign}


%\usepackage[T1]{fontenc}
%\usepackage[bitstream-charter]{mathdesign}


%\RequirePackage{lmodern}
%\RequirePackage{libertine} 
%\RequirePackage[libertine]{newtxmath}

% this works
%\usepackage[]{mathpazo}   % With old-style figures and real smallcaps.
%\linespread{1.025}              % Palatino leads a little more leading
%\usepackage[small]{eulervm}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{tabularx}
\RequirePackage{color}
\RequirePackage{pifont}
\RequirePackage{soul}
\RequirePackage[]{dcolumn}
\RequirePackage[]{sidecap}
\RequirePackage[]{wrapfig}
\RequirePackage{feynmp}
\RequirePackage[]{afterpage}
\RequirePackage[]{multirow}

%% provide adjustwidth
\RequirePackage{changepage}
\RequirePackage{comment}

%% -------------------------------------
%%  More settings ...
%% -------------------------------------
%% Treat forced blank pages in the same way as chapter title pages
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else%
  \hbox{}%
  \thispagestyle{empty}%
  %\thispagestyle{plain}%
  \newpage%
  \if@twocolumn\hbox{}\newpage\fi\fi\fi}%


%% Provide mixed-case versions of the Table of contents,
%% List of tables, List of figures, Bibliography etc. header names
\DeclareRobustCommand{\contentsname}{Contents}
\DeclareRobustCommand{\bibname}{Bibliography}
\DeclareRobustCommand{\listfigurename}{List of figures}
\DeclareRobustCommand{\listtablename}{List of tables}

%% -------------------------------------
%%  Extra environments, stole from hepthesis.cls
%% -------------------------------------
%% Extra margins for various sections
\newlength{\@mainmatterextramargin}%
\setlength{\@mainmatterextramargin}{0cm}%
\newlength{\@backmatterextramargin}%
\setlength{\@backmatterextramargin}{0cm}%
\newlength{\@frontmatterextramargin}%
\setlength{\@frontmatterextramargin}{0cm}%
\newlength{\@appendixextramargin}%
\setlength{\@appendixextramargin}{0cm}%
\newlength{\@abstractextramargin}%
\setlength{\@abstractextramargin}{1.5cm}%
\newlength{\@declarationextramargin}%
\setlength{\@declarationextramargin}{1cm}%
\newlength{\@acknowledgementsextramargin}%
\setlength{\@acknowledgementsextramargin}{0cm}%
\newlength{\@prefaceextramargin}%
\setlength{\@prefaceextramargin}{0cm}%
\newlength{\@pagequoteextramargin}%
\setlength{\@pagequoteextramargin}{2cm}%
%% Top vertical spacing in the front matter
\newlength{\@frontmattertopskip}%
\newlength{\@frontmattertitletopskip}%
\newlength{\frontmattertitleskip}%
\addtolength{\frontmattertitleskip}{2cm}%
\addtolength{\@frontmattertitletopskip}{0cm}%
\setlength{\@frontmattertopskip}{\frontmattertitleskip}%
\addtolength{\@frontmattertopskip}{\@frontmattertitletopskip}%

%% Figure widths
\newlength{\smallfigwidth}
\newlength{\mediumfigwidth}
\newlength{\largefigwidth}
\newlength{\hugefigwidth}
\setlength{\smallfigwidth}{0.45\textwidth}
\setlength{\mediumfigwidth}{0.6\textwidth}
\setlength{\largefigwidth}{0.75\textwidth}
\setlength{\hugefigwidth}{0.9\textwidth}
%% Environment for thesis abstract in frontmatter
%% (argument is the thesis title)

\newenvironment{abstract}[1][Abstract]{%
  \adjustwidth{\@abstractextramargin}{\@abstractextramargin}%
  \cleardoublepage%
  \vspace*{\@frontmattertopskip}%
  \begin{center}%
    \begingroup
      \ifx\@sftitles\@empty\else\sf\fi
      {\LARGE\textbf{#1}}%
    \endgroup
  \end{center}%
  \vspace*{1cm}%
}{ \endadjustwidth \ignorespacesafterend }

%% Environment for declaration rubrik page in frontmatter
\newenvironment{declaration}{%
  \cleardoublepage%
  \adjustwidth{\@declarationextramargin}{\@declarationextramargin}%
  \vspace*{\@frontmattertopskip}%
  \begin{center}%
    \begingroup
    \ifx\@sftitles\@empty\else\sf\fi
    {\LARGE\textbf{Declaration}}%
    \endgroup
  \end{center}%
  \vspace*{1cm}%
}{%
  %\newline \newline \newline%
  %\begin{flushright}
  %  \thesisauthor\newline
  %  \today\newline
  %\end{flushright}
  \endadjustwidth%
  \ignorespacesafterend%
}

%% Change the spacing of lines
\DeclareRobustCommand{\setspacing}[1]{%
  \setfrontmatterspacing{#1}%
  \setmainmatterspacing{#1}%
  \setappendixspacing{#1}%
  \setbackmatterspacing{#1}%
}
\DeclareRobustCommand{\setfrontmatterspacing}[1]{\def\@frontmatterspacing{#1spacing}}
\DeclareRobustCommand{\setmainmatterspacing}[1]{\def\@mainmatterspacing{#1spacing}}
\DeclareRobustCommand{\setappendixspacing}[1]{\def\@appendixspacing{#1spacing}}
\DeclareRobustCommand{\setbackmatterspacing}[1]{\def\@backmatterspacing{#1spacing}}


%% Change the extra margin size
\DeclareRobustCommand{\setextramargins}[1]{%
  \setfrontmatterextramargins{#1}%
  \setmainmatterextramargins{#1}%
  \setappendixextramargins{#1}%
  \setbackmatterextramargins{#1}%
}
\DeclareRobustCommand{\setfrontmatterextramarginsg}[1]{\setlength{\@frontmatterextramargin}{#1}}
\DeclareRobustCommand{\setmainmatterextramargins}[1]{\setlength{\@mainmatterextramargin}{#1}}
\DeclareRobustCommand{\setappendixextramargins}[1]{\setlength{\@appendixextramargin}{#1}}
\DeclareRobustCommand{\setbackmatterextramargins}[1]{\setlength{\@backmatterextramargin}{#1}}
\DeclareRobustCommand{\setabstractextramargins}[1]{\setlength{\@abstractextramargin}{#1}}
\DeclareRobustCommand{\setdeclarationextramargins}[1]{\setlength{\@declarationextramargin}{#1}}
\DeclareRobustCommand{\setacknowledgementsextramargins}[1]{\setlength{\@acknowledgementsextramargin}{#1}}
\DeclareRobustCommand{\setprefaceextramargins}[1]{\setlength{\@prefaceextramargin}{#1}}

%% Part-wise spacing commands and definitions
\def\@frontmatterspacing{onehalfspacing}
\def\@mainmatterspacing{onehalfspacing}
\def\@appendixspacing{onehalfspacing}
\def\@backmatterspacing{onehalfspacing}
%% An environment for the rubbish at the front of a typical thesis
\let\@oldfrontmatter\frontmatter
\specialcomment{frontmatter}{%
  \setcounter{tocdepth}{2}% show down to subsections in contents
  \setcounter{secnumdepth}{2}% number down to subsections
  %\newcounter{iterlist}%
  \@oldfrontmatter%
  \adjustwidth{\@frontmatterextramargin}{\@frontmatterextramargin}%
  \begin{\@frontmatterspacing}%
}{%
  \end{\@frontmatterspacing}%
  \endadjustwidth%
  \ignorespacesafterend%
}


%% Title page

%% Use the \title and \author arguments to build the front page etc.
\ifx\@requiretitling\@empty%
%% Re-define \title and \author to store their args in \thetitle, \theauthor
%variables
\renewcommand*\title[1]{\gdef\@title{#1}\global\let\thetitle\@title}%
\renewcommand*\author[1]{\gdef\@author{#1}\global\let\theauthor\@author}%
\else%
\RequirePackage{titling}%
\fi
%% Makes a thesis title page
\let\@oldtitlepage\titlepage
\let\@oldendtitlepage\endtitlepage
\DeclareRobustCommand{\titlepage}[2][]{%
  \thispagestyle{empty}%
  \begingroup%
  \ifx\@sftitles\@empty\else\sf\fi%
  \begin{center}%
    \vspace*{\frontmattertitleskip}%
    \begin{doublespace}%
      {\Huge\textbf{\thetitle}}\\%
    \end{doublespace}%
    \vspace*{3cm}%
    %{\Large{{\theauthor} \\ {#1}}}\\%
    {\Large{{\theauthor}\\}} 
    \vspace*{8cm}%
    {#2}%
  \end{center}%
  \endgroup%
}
\renewcommand{\maketitle}[1]{\titlepage{}}

%% Cleveref should be the last package to not be messed up by others
\RequirePackage[noabbrev]{cleveref}
