\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{templates/cel-klausur}
\LoadClass[11pt,a4paper,oneside]{article}

% Gerneral %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % UTF-8
\usepackage[ngerman]{babel}
\usepackage{pythontex}

% Math and Symbols %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amssymb, amsfonts, latexsym}
\usepackage[fleqn]{amsmath}
\usepackage[right]{eurosym}
\usepackage{MnSymbol}
\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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{graphics}
\usepackage{color}
\usepackage{pgf, tikz, pgfplots}
\pgfplotsset{compat=1.9}

% Layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{marginnote}
\RequirePackage[compact,explicit]{titlesec}% http://ctan.org/pkg/titlesec
\RequirePackage{fancyhdr, lastpage}
\RequirePackage{ifthen}
\RequirePackage[hidelinks, final=true]{hyperref}
\RequirePackage[ddmmyyyy]{datetime}
\renewcommand{\dateseparator}{.}
\RequirePackage[export]{adjustbox}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Page format
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\topmargin        -15mm
\oddsidemargin      0mm
\evensidemargin     0mm
\textheight     250.5mm
\textwidth      160.0mm
\columnsep        8.1mm
\parindent        0.0em
\headsep          6.3mm
\headheight        12pt
\lineskip           1pt
\normallineskip     1pt
\marginparsep       6mm

% Seitenzahlen
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
\fancyhf{}
\cfoot{\tiny \thepage\ / \pageref{LastPage}}
\IfFileExists{.gitversion}{%
    \lfoot{\color{red} \tiny \today{ }\currenttime}%
    \rfoot{\color{red} \tiny \input{.gitversion}}%
}{%
%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Variablen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Befehle
\newcommand{\examType}[1]{\def \theExamType{#1}}
\newcommand{\examName}[1]{\def\theExamName{#1}}
\newcommand{\examDate}[1]{\def \theExamDate{#1}}
\newcommand{\examAuthors}[1]{\def \theExamAuthors {#1}}
\newcommand{\examDuration}[1]{\def \theExamDuration {#1}}
\newcommand{\exerciseCount}[1]{\def \theExerciseCount {#1}}
\newcommand{\pointsPerExercise}[1]{\def \thePointsPerExercise {#1}}

\newcommand{\gradeDate}[1]{\def \theGradeDate {#1}}
\newcommand{\reviewDate}[1]{\def \theReviewDate {#1}}
\newcommand{\reviewMode}[1]{\def \theReviewMode {#1}}

\newcommand{\pointsToPass}[1]{\def\thePointsToPass{#1}}
\newcommand{\gradeStep}[1]{\def\theGradeStep{#1}}

% Defaults
\examType{Schriftliche Prüfung im Fach}
\examName{CEL Klausur}
\examDate{DD.MM.YYYY}
\examAuthors{Max Mustermann; Friedrich K. Jondral}
\examDuration{xxx Stunden}
\exerciseCount{xxx}
\pointsPerExercise{10}

\gradeDate{DD.MM.YYYY}
\reviewDate{DD.MM.YYYY}
\reviewMode{}

\pointsToPass{0}
\gradeStep{0}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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]{templates/logos/kitlogo_de_rgb}%
    \hfill%
    \begin{minipage}{9cm}\centering%
        \textbf{Karlsruhe Institute of Technology}\\
        Communications Engineering Lab\\
        Univ.-Prof. Dr. rer.nat. Friedrich Jondral%
    \end{minipage}%
    \hfill%
    \includegraphics[width=25mm,valign=c]{templates/logos/cel_logo}%
}
\newcommand{\ssection}[1]{\vspace*{0.8em}{\textbf{#1}}\\}

\def\maketitle{
    \hypersetup{
        pdfauthor = \theExamAuthors,
        pdftitle = {\theExamType{ }\theExamName{ }\theExamDate},
        pdfsubject = {Institut für Nachrichtentechnik (CEL)},
        pdfcreator = {PDFLaTeX with hyperref package},
        pdfproducer = {PDFLaTeX}
    }    
    \vspace*{-0.8cm} % align the top of the kit logo with the text
    \header\vspace{2.5cm}%
    % Überschrift:
    \begin{center}
        \Large \bf \theExamType \\[.1em]
        \Huge \bf \theExamName \\[.3em]
        \Large \bf \theExamDate
        \ifshowSolution \\[2cm] \Huge \bf Musterlösung \fi
        \ifshowReview \\[.3em]\Large für die Klausureinsicht \fi%
    \end{center}
    \vfill
    \ifshowReview%
        \input{templates/title.review.tex}%
    \else%
        \input{templates/title.exam.tex}%
    \fi
    \newpage
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
\titlespacing*{\subsection}{0ex}{2ex}{1ex} 
% \subsection{...} can be used for structuring
