\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{template/acsthesis}[2015/05/21 Template for Student Theses at ACS]

\LoadClass[12pt, 				% Fontsize
		   a4paper,				% Paper format
		   listof=totoc,		% 'List of Tables' and 'List of Figures' in table of contents (toc)
		   numbers=noenddot, 	% Deactivate dot behind last number in toc
		   index=totoc,			% Add index to toc
		   BCOR=1cm,			% additional margin for binding
		   ]{scrbook}

%------------------------------------------------------------------------------%
% Include packages
%------------------------------------------------------------------------------%
% Footnote has to be included _before_ hyperref
\usepackage{fnpct}

\RequirePackage[table]{xcolor}
\RequirePackage[utf8]{luainputenc} 		% Enconding of the tex file
\RequirePackage{cmap} 						% Nice ligatures
\RequirePackage[T1]{fontenc} 				% Encoding of the output PDF
\RequirePackage{textcomp} 					% ????
\RequirePackage{lmodern} 					% Use modern Linux fonts
\RequirePackage{graphicx} 					% Include graphics via 'includegraphic'
\RequirePackage{makeidx} 					% Create an index if desired
\RequirePackage[binary-units]{siunitx} 		% Nice numbers with units
\RequirePackage{hyperref} 					% Links within document
\RequirePackage{xparse} 					% NewDocumentCommand etc.
\RequirePackage{etoolbox} 					% If-then-else
\RequirePackage{ifthen} 					% If-then-else
\RequirePackage{babel} 						%
\RequirePackage{booktabs} 					% nicer tables
\RequirePackage{tikz} 						% draw pictures
\RequirePackage{pgfplots} 					% use for printing diagrams
\RequirePackage{listings}
\RequirePackage{relsize}
\RequirePackage{enumitem}

%------------------------------------------------------------------------------%
% Command and environment definitions
%------------------------------------------------------------------------------%
\newcommand{\addinstitute}[3]{}                 % Institute hinzufuegen
                                                %  #1 Kuerzel fuer Referenz, z.B. lfbs
                                                %  #2 Titel des Instituts, z.B. Chair for OS, RWTH Aachen University
                                                %  #3 Referenzzeichen, z.B. (*) oder irgendein Symbol


\newcommand{\printinstitutes}{}
\newcommand{\printsupervisors}{}
\newcommand{\printsecondreferees}{}

% ----- Abstract (english)
\addto\captionsenglish{%
	\def\@keywordName{Keywords}
	\def\@keywords{\@keywordsEN}
}

\NewDocumentEnvironment{abstract}{}%
{
	\addchap*{\abstractname}
}%
{%end-defs
	\bigskip\par
	\textbf{\@keywordName:} \@keywords
}

% ----- User commands
\def\@titleEN{\@title}
\def\@thesisType{Unknown Thesis Type}
\NewDocumentCommand \titleDE         { m } {\def\@titleDE{#1}}
\NewDocumentCommand \titleEN         { m } {\def\@titleEN{#1}}
\NewDocumentCommand \matrNr          { m } {\def\@matrNr{#1}}
\NewDocumentCommand \submissionDate  { m } {\def\@submissionDate{#1}}
\NewDocumentCommand \addSupervisor   { m } {\edef\@supervisors{#1}}
\NewDocumentCommand \keywordsDE      { m } {\def\@keywordsDE{#1}}
\NewDocumentCommand \keywordsEN      { m } {\def\@keywordsEN{#1}}
\NewDocumentCommand \thesisType      { m } {%

\def\@thesisType{Master Thesis}
}
\newtoggle{printSignatures}
\togglefalse{printSignatures}
\NewDocumentCommand \printSignatures      {   } {%
	\toggletrue{printSignatures}
}


%
% Institut hinzufuegen (falls mehrere Institute/Firmen involviert)
%  #1 Kuerzel (zur Verwendung mit \instituteref)
%  #2 Kompletter Name des Instituts
%  #3 Symbol zur Kennzeichnung von Betreuer/Institut
%
\newtoggle{printInstitutes}
\togglefalse{printInstitutes}
\renewcommand{\printinstitutes}{}
\newcommand{\instref}{}
\renewcommand{\addinstitute}[3]{%
	\expandafter\def\csname instref#1\endcsname{#3}%          % Definiere z.B. \instreflfbs als #3
	\expandafter\def\csname instname#1\endcsname{#2}%         % Definiere z.B. \instnamelfbs als #2
	% Neues Institut an "Tabelle" anhaengen
	\protected@edef\printinstitutes{\printinstitutes#3 #2\\}% % \printinstitutes expandieren und mit Zusatz
	                                                          %   wieder speichern
	\toggletrue{printInstitutes}%                       % Wenn wir ein Inst. definiert haben, auch drucken
}
% Anwenderbefehl zum referenzieren des Instituts
\NewDocumentCommand \instituteref { m }{                            % Definiere z.B. \instituteref{lfbs} als \instreflfbs
	\expandafter\csname instref#1\endcsname
%	\csdef{instref#1}{}
}


%
% Betreuer hinzufuegen
%
\newlength{\@templength}
\newlength{\@supervisorlength}                        % Laenge des laengsten Betreuernamens
\newlength{\@supervisorlabellength}                   % Laenge des laengsten Betreuerlabels (Betreuer, Zweitgutachter etc.)
\newlength{\@supervisornamelength}                    % Laenge des Betreuerlabels (z.B. Betreuer)
\newlength{\@secondrefereenamelength}                 % Laenge des Zweitgutachterlabels (z.B. Zweitgutachter)


\NewDocumentCommand \addsupervisor { O{} m }{%
	\ifcsundef{instref#1}{% \instrefxyz not defined
  		\ClassError{template/acsthesis}
  		           {Institute "#1" not defined. Institute must be defined before using a reference to it}
  		           {Please define institute before using a reference to it.}
	}{%else
		\settowidth{\@templength}{#2 \instituteref{#1}}
		\ifnumcomp{\@templength}{>}{\@supervisorlength}{%
			\setlength{\@supervisorlength}{\@templength}
		}{%else
		}
		% Neuen Betreuer an "Tabelle" anhaengen
		\ifthenelse{\equal{\printsupervisors}{}}
			{\g@addto@macro\printsupervisors{#2 \instituteref{#1}}}    %1. Eintrag
			{\g@addto@macro\printsupervisors{\\ #2 \instituteref{#1}}} %Folgende Eintraege
	}
}

%
% Zweitgutachter hinzufuegen
%
\newtoggle{secondReferee}
\togglefalse{secondReferee}
\NewDocumentCommand \addsecondreferee { O{} m }{%
	\ifcsundef{instref#1}{% \instrefxyz not defined
  		\ClassError{template/acsthesis}
  		           {Institute "#1" not defined. Institute must be defined before using a reference to it}
  		           {Please define institute before using a reference to it.}
	}{%else
		\settowidth{\@templength}{#2 \instituteref{#1}}
		\ifthenelse{\@templength>\@supervisorlength}
			{\setlength{\@supervisorlength}{\@templength}}%true
			{}%false
		% Neuen Zweitgutachter an "Tabelle" anhaengen
		\ifthenelse{\equal{\printsecondreferees}{}}
			{\g@addto@macro\printsecondreferees{#2 \instituteref{#1}}}    %1. Eintrag
			{\g@addto@macro\printsecondreferees{\\ #2 \instituteref{#1}}} %Folgende Eintraege
		\toggletrue{secondReferee}
	}
}

\NewDocumentCommand \copyrightpage { } {%
	\thispagestyle{empty}

	\vspace*{9.0cm}
    {\footnotesize
	\@statement
    }
	\vspace*{-1.5cm}
}


\def\@statement{%
	\noindent
	The present's work \LaTeX{} source code, its images, all used datasets, and all scripts that were used to create graphs are publicly available on \url{https://git.dennispotter.eu/Dennis/masters-thesis}. The aforementioned Git repository also provides a compiled version of the present work.
	
	\vspace*{0.5cm}
	
	\begin{description}[leftmargin=2cm]
	        \item [\textbf{Author}] Dennis Potter \href{mailto:dennis@:dennispotter.eu}{\texttt{<dennis@dennispotter.eu>}}
	        \item [\textbf{Supervisors}] Lukas Razik \href{mailto:lrazik@eonerc.rwth-aachen.de}{\texttt{<lrazik@eonerc.rwth-aachen.de>}}\\
										Steffen Vogel \href{mailto:svogel2@eonerc.rwth-aachen.de}{\texttt{<svogel2@eonerc.rwth-aachen.de>}}
	\end{description}
	
	\vspace*{0.5cm}
	
	\noindent
	Submitted to RWTH Aachen University on November 9, 2018.
	
	\vspace*{0.5cm}

    \subsection*{Copyright}
    The present work is published under the 
    
    \begin{center}
        \vspace*{-1mm}
        \begin{minipage}{0.24\textwidth}
        \end{minipage}
        \begin{minipage}{0.025\textwidth}
                \vspace{-0.75mm}
            	\includegraphics[width=4mm, keepaspectratio]{template/images/cc.png}
        \end{minipage}
        \begin{minipage}{0.030\textwidth}
                \vspace{-0.75mm}
            	\includegraphics[width=4mm, keepaspectratio]{template/images/by.png}
        \end{minipage}
        \begin{minipage}{0.73\textwidth}
            \textbf{Creative Commons Attribution 4.0 International (CC BY 4.0)}.
        \end{minipage}\hfill
    \end{center}
    \vspace*{-1mm}
    Below, a human-readable summary of (and not a substitute for) the license:

    \begin{center}{\vspace*{1mm}\normalsize\textbf{You are free to:}}\end{center}\vspace*{-4mm}
    \begin{description}
        \item [\textbf{Share}] copy and redistribute the material in any medium or format;
        \item [\textbf{Adapt}] remix, transform, and build upon the material for any purpose, even commercially.
    \end{itemize}

    \begin{center}\vspace*{1mm}{\normalsize\textbf{Under the following terms:}}\end{center}\vspace*{-4mm}
    \begin{description}
        \item [\textbf{Attribution}] You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. 
        \item [\textbf{No additional restrictions}] You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. 
    \end{description}


	\vspace{.5cm}
}


% ----- Language dependent variables
	\renewcommand\lstlistlistingname{List of Listings}
%
	\def\@tpName{Title Page}
	\def\@tocName{Table of Contents}
	\def\@mainTitle{\@titleEN}
	\def\@matrNrName{Matriculation Number}
	\def\@supervisorName{Supervisors}
	\def\@secondRefereeName{Second Referee}
	\def\@firstExaminer{1\textsuperscript{st} Examiner}
	\def\@secondExaminer{2\textsuperscript{nd} Examiner}
	\def\@aachenDateName{Aachen, \@submissionDate}
	\def\@datesignature{ \makebox[6cm]{\hrulefill} \hfill \makebox[7cm]{\hrulefill} \makebox[6cm]{Ort, Datum} \hfill \makebox[7cm]{Unterschrift}}
	\def\@rwthAcsName{%
		The present work was submitted to \\
		RWTH Aachen University\\
		Faculty of Electrical Engineering and Information Technology\\
		Institute for Automation of Complex Power Systems\\
		Univ.-Prof. Antonello Monti, Ph.\,D.
	}

\input{template/titlepage}

%------------------------------------------------------------------------------%
% Create cover, table of contents, etc.
%------------------------------------------------------------------------------%
\AtBeginDocument{%
% Everything before the first chapter with roman page numbers
	\frontmatter
    \clearpage
    \phantomsection
    \pdfbookmark[0]{Title Page}{title}
	\maketitle
	\cleardoublepage
    \copyrightpage
	\cleardoublepage
	\phantomsection\pdfbookmark{\abstractname}{abstract}
	\input{abstract}
	\cleardoublepage
	\phantomsection\pdfbookmark{\@tocName}{toc}
	\tableofcontents

% Here comes the students code
\mainmatter
}

\AtEndDocument{%
\cleardoublepage
	\phantomsection
	\addcontentsline{toc}{chapter}{Bibliography}
	\printbibliography
	\printindex
}
