% This file requires quite a few texlive packages.
% texlive-lang-german / texlive-lang-european
% texlive-science

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{cel-thesis/cel-thesis}
\LoadClass[11pt,	% 11pt
	    BCOR8mm, 		% Bindekorrektur
	    DIV12,		% Satzspiegel
	    twoside,		% Doppelseitige Ausgabe
	    a4paper,		% DIN A4
	    openright,		% Neue Kapitel auf rechter Seite
%	    dvips,		% Source Specials ins DVI/PS
%	    draft,		% Overfull H-Boxen markieren
	    final,		% Engueltige Version mit Bildern
]{scrreprt}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% new commands
\newcommand{\thesisTitle}[1]{\def \theThesisTitle{#1}}
\newcommand{\thesisType}[1]{\def \theThesisType{#1}}
\newcommand{\thesisAuthor}[1]{\def \theThesisAuthor{#1}}
\newcommand{\thesisAdvisor}[1]{\def \theThesisAdvisor{#1}}
\newcommand{\thesisSupervisor}[1]{\def \theThesisSupervisor{#1}}
\newcommand{\thesisStartDate}[1]{\def \theThesisStartDate{#1}}
\newcommand{\thesisEndDate}[1]{\def \theThesisEndDate{#1}}
\newcommand{\thesisSignatureDate}[1]{\def \theThesisSignatureDate{#1}}
\newcommand{\thesisLanguage}[1]{\def \theThesisLanguage{#1}}

% Defaults
\thesisTitle{Random Thesis Title}
\thesisType{Master Thesis Type}
\thesisAuthor{John Doe}
\thesisAdvisor{Univ.-Prof. Dr.rer.nat. Friedrich K. Jondral}
\thesisSupervisor{Super-Assi Deluxe M.Sc.}
\thesisStartDate{dd.mm.yyyy}
\thesisEndDate{dd.mm.yyyy}
\thesisSignatureDate{dd.mm.yyyy}
\thesisLanguage{english}

\def\dictThesisLanguageGerman{ngerman}
\def\dictThesisLanguageEnglish{english}

% Gerneral %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[T1]{fontenc}  % encoding for target files.
\usepackage[utf8]{inputenc} % UTF-8 encoding for source files
\usepackage[\theThesisLanguage]{babel}
\usepackage{pythontex}

% Math and Symbols %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amssymb, amsfonts, latexsym}
\usepackage[fleqn]{amsmath}
\usepackage[right]{eurosym}
\usepackage{siunitx}
\sisetup{%
    binary-units = true,
    %  load-configurations=binary,
    %  load-configurations=abbreviations,
    %  per-mode=fraction,
    %  fraction-function=\sfrac,    % braucht Paket »xfrac«
    output-decimal-marker={,}
}
% fft symbol
\usepackage{trfsigns}
\def\korrts{\quad \laplace \quad }
% (Matrixnotation aus NT1-/WT-Buch)
\def\matrixnue#1{%
    \setbox200=\hbox{#1}%
    \setbox201=\vbox{#1}%
    \setbox202=\vsplit201 to .5\ht201
    \vbox{\normalbaselines%
        {\hrule width 1.2\wd200
            \hbox{\vrule height.6\ht202 \kern1.15\wd200 \vrule height.6\ht202}}%
        \kern-1.7\ht202%
        \hbox{\kern.1\wd200 #1}}%
}
\def\matrixnue#1{%
    \ifmeasuring@#1\else
        \sbox0{$\displaystyle#1$}\rlap{%
            \rule[\dimexpr\ht0+0.5pt\relax]{0.5pt}{.4ex}%
            \rule[\dimexpr\ht0+.4ex\relax]{1.1\wd0}{0.5pt}%
            \rule[\dimexpr\ht0+0.5pt\relax]{0.5pt}{.4ex}%
        }\box0
    \fi
}
\newcommand{\mybinom}[2]{%
    \Big(\hspace*{-1ex}\begin{array}{c}%
        #1\\[-1ex]#2%
    \end{array}\hspace*{-1ex}\Big)\,%
}
% Graphics %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ueberschriften fett
\setkomafont{sectioning}{\normalcolor\bfseries}
% Bild- und Tabellen-Unterschriften formatieren
\renewcommand{\caplabelfont}{\bfseries}
\renewcommand{\capfont}{\small}

\usepackage{graphicx}
\usepackage{color}
\usepackage{pgf, tikz, pgfplots}
\pgfplotsset{compat=1.9}

% Layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{marginnote}
\RequirePackage{ifthen}
\RequirePackage[hidelinks, final=true]{hyperref}
\RequirePackage[ddmmyyyy]{datetime}
\renewcommand{\dateseparator}{.}
\RequirePackage[export]{adjustbox}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Schalter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seitentrennung
\newboolean{onePerSheet}
\setboolean{onePerSheet}{false}
\ifdefined\argOnePerSheet\setboolean{onePerSheet}{true}\fi
\newcommand{\onePerSheet}{\ifdefined\argForce\else\setboolean{onePerSheet}{true}\fi}


% Anzeige der Korrektoren
\newboolean{showNames}
\setboolean{showNames}{false}
\ifdefined\argShowNames\setboolean{showNames}{true}\fi
\newcommand{\showNames}{\ifdefined\argForce\else\setboolean{showNames}{true}\fi}

% Anzeige der Lösung
\newboolean{showSolution}
\setboolean{showSolution}{false}
\ifdefined\argShowSolution\setboolean{showSolution}{true}\fi
\newcommand{\showSolution}{\ifdefined\argForce\else\setboolean{showSolution}{true}\fi}

% Anzeige der Punkte
\newboolean{showPoints}
\setboolean{showPoints}{false}
\ifdefined\argShowPoints\setboolean{showPoints}{true}\fi
\newcommand{\showPoints}{\ifdefined\argForce\else\setboolean{showPoints}{true}\fi}

% Anzeige des Einsicht Titelblatts
\newboolean{showReview}
\setboolean{showReview}{false}
\ifdefined\argShowReview
    \setboolean{showReview}{true}
    \setboolean{showSolution}{true}
    \setboolean{showPoints}{true}
\fi
\newcommand{\showReview}{\ifdefined\argForce\else
    \setboolean{showReview}{true}
    \setboolean{showSolution}{true}
    \setboolean{showPoints}{true}
\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Titel
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\header}{%
    \includegraphics[width=40mm,valign=c]{cel-thesis/logos/kitlogo_de_rgb}%
    \hfill%
    \begin{minipage}{8.26cm}\centering%
        \textbf{Karlsruhe Institute of Technology}\\
        Communications Engineering Lab\\
        \theThesisAdvisor%
    \end{minipage}%
    \hfill%
    \includegraphics[width=25mm,valign=c]{cel-thesis/logos/cel_logo}%
}
\newcommand{\ssection}[1]{\vspace*{0.8em}{\textbf{#1}}\\}


\def\maketitle{
    \hypersetup{
        pdfauthor = \theThesisAuthor,
        pdftitle = {\theThesisType{ }\theThesisTitle{ } \theThesisAuthor},
        pdfsubject = {Institut für Nachrichtentechnik (CEL)},
        pdfcreator = {PDFLaTeX with hyperref package},
        pdfproducer = {PDFLaTeX}
    }
    \thispagestyle{empty}
    \header  % institute banner, etc.
    \vspace*{3.0cm}%
    % Title
    \begin{center}
        {\Large \textbf{\theThesisTitle}} \\[1cm]
        {\large \theThesisType} \\[2.5cm]
        {\textbf{\theThesisAuthor}}\\[2.5cm]
        \begin{tabular}{lll}
        \ifx\theThesisLanguage\dictThesisLanguageEnglish  % default is german. But one may use english.
	  Advisor 	& : & \theThesisAdvisor \\
	  Supervisor 	& : & \theThesisSupervisor\\[2cm]
	  Start date 	& : & \theThesisStartDate \\
	  End date  	& : & \theThesisEndDate \\
	\else
	  Hauptreferent & : & \theThesisAdvisor \\
	  Betreuer      & : & \theThesisSupervisor\\[2cm]
	  Beginn        & : & \theThesisStartDate \\
	  Abgabe        & : & \theThesisEndDate \\
	\fi
	\end{tabular}
        
    \end{center}
    \hspace*{\fill}
    \newpage
    \include{cel-thesis/eidesstattliche_erklaerung}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Aufgaben
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % \newcommand{\points}[1]{\def \thePointsMax {#1}}
% % \titleformat{\section}[runin]{\Large\bfseries\vspace{1em}}{}{0pt}{#1}
% % \newcommand{\aufgabe}[1][]%
% % {%
% %     \ifthenelse{\boolean{onePerSheet}}{%
% %         \clearpage%
% %         \ifthenelse{\isodd{\thepage}}{}{\null\newpage}%
% %     }{%
% %         \ifthenelse{\boolean{showSolution}}{\clearpage}{}%
% %     }%
% %     \section{Aufgabe \thesection}%
% %     \ifshowNames%
% %         \hspace{1em}\textmd{\normalsize\color{red} (#1)}%
% %     \else%
% %         \mbox{}%
% %     \fi%
% %     \\[1.2ex]%
% %     \setcounter{teilaufgabenInAufgabe}{0}%
% %     \points{\thePointsPerExercise}
% % }
%
%
%
\makeatletter
% The command writes onto the aux file
\def\setTeilaufgabenCount#1#2{\@bsphack
  \protected@write\@mainaux{}%
         {\string\updateTeilaufgabenCount{#1}{#2}}%
  \@esphack}
\def\updateTeilaufgabenCount#1#2{%
  \expandafter\xdef\csname tafc@#1\endcsname{#2}}
\def\totalTeilaufgabenCount#1{%
  \expandafter\ifx\csname tafc@#1\endcsname\relax0\else
  \csname tafc@#1\endcsname\fi}
%\AtEndDocument{\def\newreference#1#2{%
%    \edef\reserved@a{#2}%
%    \expandafter\ifnum\csname tafc@#1\endcsname>\reserved@a\else
%    \@tempswatrue\fi}}
\makeatother
%
% 
%
\newcounter{punkteinAufgabe}
\newcounter{teilaufgabenInAufgabe}
%
\newenvironment{teilaufgaben}{
    \begin{enumerate}
    %\addtocounter{Aufgabe\thesection}{\letzterwert}
    \setcounter{enumi}{\value{teilaufgabenInAufgabe}}
    \setlength{\itemsep}{0pt}
    %\setlength{\parskip}{0pt}
    %\setlength{\parsep}{0pt}
}{
    \setcounter{teilaufgabenInAufgabe}{\value{enumi}}
    \setTeilaufgabenCount{Aufgabe\thesection}{\the\value{enumi}}
    \ifnum \the\value{enumi}=0\totalTeilaufgabenCount{Aufgabe\thesection}
        \checkPunkte
    \fi
    \end{enumerate}
}
%
% Item command mit Punktezählen und bei Bedarf Randnotiz
\newcommand{\labelpunkte}{%
    \ifnum\thePunkte>0%
        \foreach \n in {1,...,\thePunkte}{\textbullet}%
    \fi%
}
\newcommand{\teil}[1][0]{%
    \item%
    \def\thePunkte{#1}%
    \ifshowPoints\marginnote{\color{red}\labelpunkte}\fi%
    \addtocounter{punkteinAufgabe}{#1}%
}
%
% Zähler für enumerate environment neu definieren.
\renewcommand{\labelenumi}{\alph{enumi})}
\renewcommand{\labelenumii}{\alph{enumii})}
%
\newcommand{\checkPunkte}{%
    \ifnum 0<0\thesection 
        \ifnum \value{punkteinAufgabe}=0%
            \ClassWarning{cel-klausur}{%
               Aufgabe \thesection\space hat (noch) keine Punkteverteilung%
            }
        \else
        \ifnum \value{punkteinAufgabe}=1%
            \ClassWarning{cel-klausur}{%
               Aufgabe \thesection\space hat nur 1 Punkt%
            }
        \else
        \ifnum \value{punkteinAufgabe}<\thePointsMax%
            \ClassWarning{cel-klausur}{%
               Aufgabe \thesection\space hat nur \thepunkteinAufgabe\space anstatt \thePointsMax\space Punkte
            }
        \fi\fi\fi
        \ifnum \value{punkteinAufgabe}>\thePointsMax%
            \ClassWarning{cel-klausur}{%
               Aufgabe \thesection\space hat \thepunkteinAufgabe\space anstatt \thePointsMax\space Punkte%
            }
        \fi
    \fi
    \setcounter{punkteinAufgabe}{0}%
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loesung
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newenvironment{loesung}%
  {%
    \ifthenelse{\boolean{showSolution}}{%
      \begingroup%
      \vspace*{1em}
      \subsection*{Lösung}
      \bgroup%
    }{%
      \setbox0\vbox\bgroup%
    }%
  }{%
    \ifthenelse{\boolean{showSolution}}{%
      \egroup%
      \endgroup%
    }{%
      \egroup%
    }%
  }%

\newenvironment{loesungen}%
  {%
    \begingroup%
    \begin{loesung}%
    \begin{enumerate}%
  }{%
    \end{enumerate}%
    \end{loesung}%
    \endgroup%
  }%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Anhang
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newcommand{\anhang}[1][]{
    \setcounter{punkteinAufgabe}{0}
    \clearpage
    \ifonePerSheet%
        \ifthenelse{\isodd{\thepage}}{}{\null\newpage}%
    \fi%
    \appendix
    \def\thesubsection{\@Alph\c@subsection}
    \section{\ifx&#1&{Formelsammlung und Tabellen}\else{#1}\fi}
}
\makeatother

