% Adapted from the LaTeX class tutorial at
% https://de.overleaf.com/learn/latex/Writing_your_own_class

%% Identification
%% The class identifies itself and the LaTeX version needed
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{chemreport}[1930/01/01 Chemistry report class]

%% Required class settings

%% This setting is overriden by KomaScript, but still needs to be in
%% the main class file to avoid spurious LaTeX warnings.
\renewcommand{\normalsize}{}

%% Fonts across major engines
\usepackage{iftex}
\ifPDFTeX
  \usepackage{mathptmx}
\else
  \usepackage{fontspec}
  \IfFontExistsTF{Times New Roman}{
    \setmainfont{Times New Roman}
  }{
    \setmainfont{Free Serif}
  }
\fi

%% KomaScript Setup
\LoadClass[12pt,a4paper]{scrartcl}
\usepackage[
headwidth=\textwidth+8em:-2.5em,
headsepline,
automark
]{scrlayer-scrpage}

%% Styling
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{enumitem}
\usepackage{parskip}
\usepackage{booktabs}

\usepackage[font=small,labelfont=bf]{caption}
%\captionsetup{justification=raggedright,singlelinecheck=false,hypcap=false}
\usepackage{subcaption}

%% Symbols
\usepackage{pifont}

%% Chemistry/Science
\usepackage{siunitx}
\usepackage[version=3]{mhchem}
\usepackage{longtable}

\DeclareSIUnit\liter{l}

%% Graphics
\usepackage{graphicx}
\usepackage{wrapfig}

%% Internationalization
\usepackage[german]{babel}

%% Misc
\usepackage{lipsum}
\usepackage{filecontents}
\usepackage[stable,bottom]{footmisc}
\usepackage{underscore}
\usepackage{cclicenses}

%% Hyperref (last package)
\usepackage[hidelinks]{hyperref}

%% Simple Macros
\newcommand{\xmark}{\ding{55}}%
\newcommand{\cmark}{\ding{51}}%

\newcommand{\ditto}{%
    \tikz{
        \draw [line width=0.12ex] (-0.2ex,0) -- +(0,0.8ex)
            (0.2ex,0) -- +(0,0.8ex);
        \draw [line width=0.08ex] (-0.6ex,0.4ex) -- +(-1.5em,0)
            (0.6ex,0.4ex) -- +(1.5em,0);
    }%
}

%% Geometry
\geometry{left=2.0cm, right=2.0cm, top=2.5cm, bottom=2cm}
\setlist{topsep=2pt,itemsep=1pt,partopsep=1pt, parsep=1pt}
\setlength{\parindent}{0pt}
\setlength{\intextsep}{20pt plus 10.0pt minus 20.0pt}

%% Sections

\newcommand{\colonafter}[1]{#1:}

\newcommand{\multisections}{
  \titleformat{\section}
  {\huge\bfseries}
  {\Alph{section}:}
  {1em}
  {}
  
  \titleformat{\subsection}
  {\LARGE\bfseries}
  {}
  {0em}
  {}
  
  \titleformat{\subsubsection}
  {\bfseries}
  {\arabic{subsubsection}.}
  {0.5em}
  {}
}

\newcommand{\singlesections}{
  \titleformat{\section}
  {\LARGE\bfseries}
  {}
  {0em}
  {\colonafter}
  
  \titleformat{\subsection}
  {\large\bfseries}
  {}
  {0em}
  {}
  
  \titleformat{\subsubsection}
  {\bfseries}
  {\arabic{subsubsection}.}
  {0.5em}
  {}
}

%% Title
\renewcommand{\maketitle}{
  \begin{center}
    \makeatletter{}
    {\Huge\textbf\@title}
    \makeatother{}
  \end{center}
}

\newcommand{\theunit}{No unit defined}
\newcommand{\theclass}{No class defined}
\newcommand{\unit}[1]{\renewcommand{\theunit}{#1}}
\newcommand{\class}[1]{\renewcommand{\theclass}{#1}}

\AtBeginDocument{\maketitle{}}

%% Headers and Footers
\renewcommand{\sectionmark}[1]{\markright{#1}}
\renewcommand{\subsectionmark}[1]{}
\renewcommand{\subsubsectionmark}[1]{}

\newcommand{\multiheader}{
  \ohead{\rightmark{}, \pagemark{}}
}

\newcommand{\singleheader}{
  \ohead{
    \makeatletter
    {\@title, \pagemark{}}
    \makeatother
  }
}

\clearpairofpagestyles{}
\ihead{{\theunit}. Einheit -- {\makeatletter\@author\makeatother} -- {\theclass}}
\chead{\makeatletter\@date\makeatother}

\setkomafont{pageheadfoot}{\normalcolor\footnotesize}

% \renewcommand{\headrulewidth}{0.4pt}
% \renewcommand{\footrulewidth}{0.4pt}

%% Execute Options
\DeclareOption{single}{
  \singlesections{}
  \singleheader{}
}

\DeclareOption{multi}{
  \multisections{}
  \multiheader{}
}

\DeclareOption*{\ClassWarning{chemreport}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax
