\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ifiseries}
\RequirePackage{kvoptions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Options and their defaults

\DeclareBoolOption[false]{legacytexlive} % for the ancient TeX Live shipped with Ubuntu
\DeclareBoolOption[false]{natbib}
\DeclareBoolOption[false]{babelbib}
\DeclareBoolOption[true]{biblatex}
\DeclareStringOption[sort,numbers]{natbibOptions}
\DeclareStringOption[alphabetic]{biblatexstyle}
\DeclareStringOption[maxcitenames=4,maxbibnames=10,babel=other]{biblatexOptions}
\DeclareStringOption[]{bibresource}
\DeclareStringOption[ngerman,english]{babelOptions}
\DeclareStringOption[]{variorefOptions}
\DeclareStringOption[linesnumbered,ruled,vlined]{algorithmOptions}
\DeclareStringOption[alphaurl]{bibliographystyle} % only for bibtex, not biblatex
\DeclareStringOption[utf8]{inputencOptions}
\DeclareBoolOption[true]{tikz}
\DeclareBoolOption[true]{algorithmForwardCompatibility}
\DeclareBoolOption[true]{algorithmBackwardCompatibility}
\DeclareStringOption[smaller,nohyperlinks,printonlyused]{acronymOptions}
\DeclareStringOption[caption=false,font=footnotesize]{subfigOptions}
\DeclareStringOption[]{theorems}
\DeclareStringOption[Bibliography]{refname}
\DeclareStringOption[black]{hypercolor}
\DeclareBoolOption[false]{dottednumbers}
\DeclareStringOption[english]{language}
\DeclareBoolOption[true]{runningtitle}

\DeclareBoolOption[true]{layout}
\DeclareBoolOption[true]{pagelayout}
\DeclareStringOption[ustrade]{paper}
\DeclareStringOption[]{paperwidth}
\DeclareStringOption[]{paperheight}
\DeclareStringOption[0mm]{bindingoffset}
\DeclareStringOption[.25]{halfparskipfill}
\DeclareBoolOption[false]{halfparskip}
\DeclareStringOption[Palatino]{font}
\DeclareStringOption[12]{marginfrac}
\DeclareStringOption[floatrow]{figure}
\DeclareStringOption[]{compact}
\DeclareStringOption[1.5]{headsepmult}
\DeclareStringOption[3]{footskipmult}
\DeclareBoolOption[false]{leftmarkright}
\if@twoside
\DeclareBoolOption[false]{hcenter}
\else
\DeclareBoolOption[true]{hcenter}
\fi
\@ifclassloaded{book}%
{\DeclareStringOption[section]{theoremswithin}}%
{\DeclareStringOption[subsection]{theoremswithin}}

\DeclareBoolOption[false]{largepaper}

\ProcessKeyvalOptions*

\RequirePackage{etoolbox}
\RequirePackage{xifthen}
\RequirePackage{expl3}

\ifthenelse{\boolean{ifiseries@legacytexlive}}{% FIXME why can't i use \ififiseries@legacytexlive?
  \let\ifiseries@natbibtrue\@undefined%
  \let\ifiseries@natbibfalse\@undefined%
  \let\ififiseries@natbib\@undefined%
  \let\ifiseries@biblatextrue\@undefined%
  \let\ifiseries@biblatexfalse\@undefined%
  \let\ififiseries@biblatex\@undefined%
  \DeclareBoolOption[true]{natbib}%
  \DeclareBoolOption[false]{biblatex}%
}{}

\ififiseries@largepaper
  \let\ifiseries@marginfrac\@undefined%
  \let\ifiseries@headsepmult\@undefined%
  \let\ifiseries@footskipmult\@undefined%
  \DeclareStringOption[9]{marginfrac}%
  \DeclareStringOption[2]{headsepmult}%
  \DeclareStringOption[3.5]{footskipmult}%
\fi

\ExplSyntaxOn
% TeX Live 2009 (still shipped with Ubuntu in 2012) does not provide the 'onn' version.
% So we have to use a little trick to get the argument expanded.
% This will occur frequently in this code.
\newcommand{\ifiseries@temp}[1]{
\prg_case_str:nnn{#1}{
	{english} {\relax}
	{german} {
      \let\ifiseries@babelOptions\@undefined%
      \let\ifiseries@variorefOptions\@undefined%
      \let\ifiseries@refname\@undefined%
      \let\ifiseries@dottednumberstrue\@undefined%
      \let\ifiseries@dottednumbersfalse\@undefined%
      \let\ififiseries@dottednumbers\@undefined%
      \DeclareStringOption[english,ngerman]{babelOptions}%
      \DeclareStringOption[ngerman]{variorefOptions}%
      \DeclareStringOption[Bibliografie]{refname}%
      \DeclareBoolOption[true]{dottednumbers}%
      \ifthenelse{\boolean{ifiseries@legacytexlive}}{%
        \let\ifiseries@babelbibtrue\@undefined%
        \let\ifiseries@babelbibfalse\@undefined%
        \let\ififiseries@babelbib\@undefined%
        \let\ifiseries@bibliographystyle\@undefined%
        \DeclareBoolOption[true]{babelbib}%
        \DeclareStringOption[babalpha]{bibliographystyle}%
      }{}
      }
}{\PackageError{ifiseries}{Unsupported~language:~#1}\@ehc}}
\ExplSyntaxOff
\expandafter\ifiseries@temp\expandafter{\ifiseries@language}

\ProcessKeyvalOptions*

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Packages with no or minimal influence on the main layout

\RequirePackage{etex}
\RequirePackage[\ifiseries@inputencOptions]{inputenc}
\RequirePackage[T1]{fontenc}
\@ifclassloaded{beamer}{}{\RequirePackage[rgb,svgnames]{xcolor}}
\RequirePackage[\ifiseries@babelOptions]{babel}

\RequirePackage{amsmath}
\RequirePackage[matha]{mathabx}
\RequirePackage{amssymb}
\RequirePackage{amsfonts}
\RequirePackage{amstext}
\RequirePackage{mathtools}
\RequirePackage{nicefrac}
\RequirePackage{graphicx}
\RequirePackage{grffile}
\RequirePackage{eso-pic}
\RequirePackage{listings}
\RequirePackage{booktabs}
\RequirePackage{multicol}
\RequirePackage{multirow}
\RequirePackage[autolanguage]{numprint}
\RequirePackage{units}
\RequirePackage[babel=true]{csquotes}
\ififiseries@natbib\RequirePackage[\ifiseries@natbibOptions]{natbib}\fi
\ififiseries@babelbib\RequirePackage{babelbib}\fi
\ififiseries@biblatex
  \RequirePackage[style=\ifiseries@biblatexstyle,\ifiseries@biblatexOptions]{biblatex}%
  \ifdefempty{\ifiseries@bibresource}{}{\addbibresource{\ifiseries@bibresource}}%
  \DeclareFieldFormat[article,unpublished,inproceedings]{realtitlecase}{\MakeSentenceCase*{#1}}%
  %% cf. texmf-dist/tex/latex/biblatex/biblatex.def
  \renewbibmacro*{title}{%
    \ifboolexpr{
      test {\iffieldundef{title}}
      and
      test {\iffieldundef{subtitle}}
    }
      {}
      {\printtext[title]{%
         \printfield[realtitlecase]{title}%
         \setunit{\subtitlepunct}%
         \printfield[realtitlecase]{subtitle}}%
       \newunit}%
    \printfield{titleaddon}}%
    %%
\else
	\bibliographystyle{\ifiseries@bibliographystyle}%
	\RequirePackage{bibentry}%
\fi
\RequirePackage[\ifiseries@variorefOptions]{varioref}
\RequirePackage{url}
\RequirePackage{dsfont}
\RequirePackage{bbding}
\RequirePackage{pifont}
\RequirePackage{calc}
\RequirePackage{blindtext}
\RequirePackage{setspace}
\RequirePackage{relsize}
\RequirePackage[\ifiseries@algorithmOptions]{algorithm2e}
\ififiseries@algorithmForwardCompatibility
  \ifthenelse{\isundefined{\DontPrintSemicolon}}{\newcommand*{\DontPrintSemicolon}{\dontprintsemicolon}}{}
  \ifthenelse{\isundefined{\SetAlgoVlined}}{\newcommand*{\SetAlgoVlined}{\SetVline}}{}
  \ifthenelse{\isundefined{\SetAlgoNoLine}}{\newcommand*{\SetAlgoNoLine}{\SetNoline}}{}
\fi
\ififiseries@algorithmBackwardCompatibility
  \ifthenelse{\isundefined{\RestyleAlgo}}{\let\RestyleAlgo\restylealgo}
\fi
\RequirePackage[compatibility=false]{caption} % compatibility switched off for hyperref
\RequirePackage{bookmark}
\RequirePackage{hyperref}
\hypersetup{%
  final=true,%
  colorlinks=true,%
  linkcolor=\ifiseries@hypercolor,%
  citecolor=\ifiseries@hypercolor,%
  urlcolor=\ifiseries@hypercolor,%
}
\RequirePackage[strict]{changepage}
\ififiseries@tikz
  \RequirePackage{tikz}
  \usetikzlibrary{positioning}
  \usetikzlibrary{arrows}
  \usetikzlibrary{shapes}
  \usetikzlibrary{shadows}
  \usetikzlibrary{decorations.text}
  \usetikzlibrary{decorations.markings}
  \usetikzlibrary{decorations.pathmorphing}
  \usetikzlibrary{petri}
  \usetikzlibrary{shapes.symbols}
  \usetikzlibrary{shapes.arrows}
  \usetikzlibrary{decorations}
  \usetikzlibrary{decorations.pathreplacing}
  \usetikzlibrary{decorations.shapes}
  \usetikzlibrary{calc}
  \usetikzlibrary{chains}
  \usetikzlibrary{patterns}
  \usetikzlibrary{matrix}
  \usetikzlibrary{backgrounds}
  \usetikzlibrary{mindmap}
  \usetikzlibrary{topaths}
  \usetikzlibrary{automata}
\fi
\RequirePackage{microtype}
\RequirePackage{enumitem} %%% add 'inline' option once it is supported everywhere
\newlist{compactitemize}{itemize}{3}
\setlist[compactitemize]{label=\raisebox{.1em}{\smaller\textbullet}}
\newlist{compactenumerate}{enumerate}{2}
\setlist[compactenumerate]{label=\arabic*.}
\newlist{compactdescription}{description}{3}
\RequirePackage{placeins}

\ExplSyntaxOn
\renewcommand{\ifiseries@temp}[1]{
\prg_case_str:nnn{#1}{
{} {\relax}
{floatrow} {\RequirePackage{floatrow}\DeclareCaptionSubType[alph]{figure}}
{subfig} {\RequirePackage[\ifiseries@subfigOptions]{subfig}\RequirePackage{sidecap}\sidecaptionvpos{figure}{t}}
}{\PackageError{ifiseries}{Unknown~figure~system:~#1}\@ehc}}
\expandafter\ifiseries@temp\expandafter{\ifiseries@figure}
\ExplSyntaxOff

\RequirePackage[\ifiseries@acronymOptions]{acronym}
\RequirePackage{xspace}
\RequirePackage{hyphenat}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Useful commands

%% Remove margins for screen viewing.
%% This command is used by the code below if 'compact' is set.
%% It is the same as the '\nomargin' command in 'localKit'.
\newcommand*{\nomargin}{
	\setlength{\oddsidemargin}{.02\textwidth-1in}
	\setlength{\evensidemargin}{.02\textwidth-1in}
	\setlength{\paperwidth}{1.04\textwidth}
	\setlength{\paperheight}{\headheight+\headsep+\textheight+\footskip}
	\setlength{\paperheight}{1.04\paperheight}
	\setlength{\topmargin}{.02\paperheight-1in}
}

%% Abbreviations
\newcommand*{\abbrdot}{\protect\@ifnextchar.{}{.\@\xspace}}
\newcommand*{\abbrcomma}{\protect\@ifnextchar,{}{,\xspace}}
\newcommand*{\eg}{e.\@\,g.\@\abbrcomma}
\newcommand*{\ie}{i.\@\,e.\@\abbrcomma}
\newcommand*{\cf}{cf.\@\abbrcomma}
\newcommand*{\Eg}{E.\@\,g.\@\abbrcomma}
\newcommand*{\Ie}{I.\@\,e.\@\abbrcomma}
\newcommand*{\Cf}{Cf.\@\abbrcomma}
\newcommand*{\etal}{et~al\abbrdot}
\newcommand*{\etc}{etc\abbrdot}
\newcommand*{\vs}{vs\abbrdot}
\newcommand*{\Wlg}{W.l.o.g\abbrdot}
\newcommand*{\wlg}{w.l.o.g\abbrdot}
\newcommand*{\OBdA}{O.B.d.A\abbrdot}
\newcommand*{\oBdA}{o.B.d.A\abbrdot}
\newcommand*{\wrt}{w.r.t.\@\xspace}
\newcommand*{\rv}{r.v.\@\xspace}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Theorems. If 'theorems' is empty, nothing happens here.

\ExplSyntaxOn
\renewcommand{\ifiseries@temp}[1]{
\prg_case_str:nnn{#1}{
{} {\relax}
{numbersfirst} {
  \ififiseries@dottednumbers% FIXME how can I pass through boolean options?
    \RequirePackage[within=\ifiseries@theoremswithin,likeequations,language=\ifiseries@language,dottednumbers=true]{localTheorem}%
  \else%
    \RequirePackage[within=\ifiseries@theoremswithin,likeequations,language=\ifiseries@language,dottednumbers=false]{localTheorem}%
  \fi%
  \let\@subsection\subsection%
  \renewcommand*\subsection{\@ifnextchar*{\@subsection}{\@subsection*}}%
  \let\@subsubsection\subsubsection%
  \renewcommand*\subsubsection{\@ifnextchar*{\@subsubsection}{\@subsubsection*}}%
}
{numberslast} {
  \ififiseries@dottednumbers%
    \RequirePackage[numbersfirst=false,language=\ifiseries@language,dottednumbers=true]{localTheorem}%
  \else%
    \RequirePackage[numbersfirst=false,language=\ifiseries@language,dottednumbers=false]{localTheorem}%
  \fi%
}
}{\PackageError{ifiseries}{Unknown~theorem~system:~#1}\@ehc}}
\ExplSyntaxOff
\expandafter\ifiseries@temp\expandafter{\ifiseries@theorems}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% Packages and settings with influence on the main layout

\ififiseries@layout

\newcommand*{\ifiseries@titlefontfamily}{}
\newcommand*{\ifiseries@sectionfontweight}{\bfseries}
\newcommand*{\ifiseries@chapterfontweight}{\bfseries}

%% Font and linespread.
\ExplSyntaxOn
\renewcommand{\ifiseries@temp}[1]{
\prg_case_str:nnn{#1}{
{} {\relax}
{Bera} {
  \RequirePackage[scaled]{beraserif}%
  \RequirePackage[scaled=.90]{berasans}%
  \RequirePackage[scaled=.90]{beramono}%
  \linespread{1.05}%
}
{Charter} { %% sometimes problem: "Too many math alphabets used in version normal."
  \usepackage[bitstream-charter]{mathdesign}%
  \RequirePackage[scaled]{berasans}%
  \RequirePackage[scaled]{beramono}%
  \RequirePackage{eucal}%
  \linespread{1.05}%
}
{ConcreteEuler} {
  \RequirePackage{lmodern}%
  \RequirePackage[boldsans]{concmath}%
  \RequirePackage{eulervm}%
  \RequirePackage[scaled=.84]{beramono}%
  \linespread{1.05}%
}
{DejaVu} {
  \RequirePackage[scaled]{DejaVuSerif}
  \RequirePackage{eulervm}
  \RequirePackage[scaled=.95]{beramono}
  \linespread{1.10}
}
{DejaVuCondensed} {
  \RequirePackage[scaled]{DejaVuSerifCondensed}
  \RequirePackage{eulervm}
  \RequirePackage[scaled=.95]{beramono}
  \linespread{1.10}
}
{Kerkis} { %% some problems in math mode, e.g., tildas extend into symbols
  \usepackage{kmath,kerkis}%
  \let\openbox\@undefined%
  \RequirePackage[scaled=.81]{berasans}%
  \RequirePackage[scaled=.81]{beramono}%
  \renewcommand*{\bfdefault}{sb}%
  \linespread{1.10}%
}
{KP} {
  \RequirePackage{kpfonts}%
  \linespread{1.05}%
}
{LatinModern} {
  \RequirePackage{lmodern}%
  \RequirePackage[scaled=.80]{beramono}%
  \let\@bfseries\bfseries%
  \renewcommand*{\bfseries}{\sffamily\@bfseries\fontseries{sbc}\selectfont}%
  \linespread{1.05}%
}
{NewCenturySchoolbook} {
  \RequirePackage{fouriernc}%
  \RequirePackage[scaled=.85]{beramono}%
  \linespread{1.10}%
}
{Palatino} {
  \RequirePackage[sc]{mathpazo}%
  \@ifclassloaded{beamer}{%
    \RequirePackage[scaled=.90]{helvet}%
    \RequirePackage[scaled=.80]{beramono}%
  }{%
    \RequirePackage[scaled=.80]{berasans}%
    \RequirePackage[scaled=.80]{beramono}%
  }%
  \linespread{1.05}%
}
{Schola} {
  \RequirePackage{fouriernc}% only for the math
  \RequirePackage{tgschola}%
  \RequirePackage[scaled=.88]{beramono}%
  \linespread{1.05}%
}
{Times} {
  \RequirePackage{eucal}%
  \RequirePackage{mathptmx}%
  \RequirePackage[scaled=.87]{helvet}%
  \RequirePackage[scaled=.82]{beramono}%
  \linespread{1.05}%
}
{Termes} {
  \RequirePackage{tgtermes}%
  \RequirePackage[scaled=.86]{berasans}%
  \RequirePackage[scaled=.86]{beramono}%
  \linespread{1.05}%
}
{TX} {
  \RequirePackage{txfonts}%
  \RequirePackage[scaled=.86]{berasans}%
  \RequirePackage[scaled=.86]{beramono}%
  \linespread{1.05}%
}
{Utopia} {
  \RequirePackage[widespace]{fourier}%
  \RequirePackage{eucal}%
  \RequirePackage[scaled=.82]{berasans}%
  \RequirePackage[scaled=.82]{beramono}%
  \linespread{1.05}%
}
}{\PackageError{ifiseries}{Unknown~font:~#1}\@ehc}}
\ExplSyntaxOff
\expandafter\ifiseries@temp\expandafter{\ifiseries@font}

%% \prg_case_str cannot occur inside an argument.
%% So we have to prepare several commands here to be called later
%% inside the \@ifclassloaded.
\ExplSyntaxOn
%
\newcommand{\ifiseries@temp@paper}[1]{
\prg_case_str:nnn{#1} {
{} {\relax}
{ustrade} {\geometry{paperwidth=15.24cm, paperheight=22.86cm}}
{15522} {\geometry{paperwidth=15.50cm, paperheight=22.00cm}}
{royal} {\geometry{paperwidth=15.57cm, paperheight=23.39cm}}
{crownquarto} {\geometry{paperwidth=18.91cm, paperheight=24.589cm}}
}{\geometry{paper=\ifiseries@paper}}}
%
\newcommand{\ifiseries@temp@compact}[1]{
\prg_case_str:nnn{#1}{
{} {\relax}
{moderate} {
  \setlength{\headsep}{.5\baselineskip}%
  \setlength{\footskip}{\baselineskip}%
  \nomargin%
}
{strong} {
  %% This does the same as the '\ebook' command in 'localKit'.
  \fancyhf{}%
  \fancyfoot[R]{\thepage}%
  \fancypagestyle{plain}{\fancyhf{}\fancyfoot[R]{\thepage}}%
  \setlength{\headsep}{0pt}%
  \setlength{\headheight}{0pt}%
  %\setlength{\topskip}{0pt}% affects height of textblock in ways difficult to determine!
  \setlength{\footskip}{1.5\baselineskip}%
	\setlength{\oddsidemargin}{.02\textwidth-1in}%
	\setlength{\evensidemargin}{.02\textwidth-1in}%
	\setlength{\paperwidth}{1.04\textwidth}%
  \setlength{\paperheight}{\textheight+\footskip}%
	\setlength{\topmargin}{.005\paperheight-1in}% CHANGE
  \setlength{\paperheight}{1.02\paperheight}%
}
}{\PackageError{ifiseries}{Unknown~compact~system:~#1}\@ehc}}
\ExplSyntaxOff
%% End of preparations.

\@ifclassloaded{beamer}{}{%
%% Page layout.
\ififiseries@pagelayout
\RequirePackage{geometry}
\expandafter\ifiseries@temp@paper\expandafter{\ifiseries@paper}
\ifdefempty{\ifiseries@paperheight}{}{\ifdefempty{\ifiseries@paperwidth}{}%
  {\geometry{paperwidth=\ifiseries@paperwidth,paperheight=\ifiseries@paperheight}}}
%
\ifthenelse
  {\equal{\@ptsize}{1}}
  {\setlength{\headheight}{13.59999pt}}
  {}
%
\ififiseries@hcenter
\geometry{%
bindingoffset=\ifiseries@bindingoffset, %
heightrounded, %
headsep=\ifiseries@headsepmult\baselineskip, %
footskip=\ifiseries@footskipmult\baselineskip, %
left=3\paperwidth/(2*\ifiseries@marginfrac), hmarginratio=1:1, %
includehead, top=\paperheight/\ifiseries@marginfrac, vmarginratio=1:2}
\else
\geometry{%
bindingoffset=\ifiseries@bindingoffset, %
heightrounded, %
headsep=\ifiseries@headsepmult\baselineskip, %
footskip=\ifiseries@footskipmult\baselineskip, %
left=\paperwidth/\ifiseries@marginfrac, hmarginratio=1:2, %
includehead, top=\paperheight/\ifiseries@marginfrac, vmarginratio=1:2}
\fi
\fi % pagelayout

%% Folios and running titles (and others, see below).
\ififiseries@leftmarkright\let\rightmark\leftmark\fi
\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\@ifclassloaded{book}{%
\let\@frontmatter\frontmatter
\renewcommand*{\frontmatter}{\@frontmatter\renewcommand*{\chaptermark}[1]{\markboth{##1}{##1}}}
\let\@mainmatter\mainmatter
\renewcommand*{\mainmatter}{\@mainmatter\renewcommand*{\chaptermark}[1]{\markboth{\thechapter. \ ##1}{}}}
\let\@backmatter\backmatter
\renewcommand*{\backmatter}{\@backmatter\bookmarksetup{startatroot}}
\let\@appendix\appendix
\renewcommand*{\appendix}{\bookmarksetup{startatroot}\@appendix}
\fancyhead[LE]{\nouppercase{\leftmark}}
}{ %%% else ifclassloaded book
\ififiseries@runningtitle\fancyhead[LE]{\nouppercase{\rightmark}}\fi
} %%% end ifclassloaded book
\ififiseries@runningtitle\fancyhead[RO]{\nouppercase{\rightmark}}\fi
\fancyfoot[RO]{\thepage}
\fancyfoot[LE]{\thepage}
\renewcommand*{\headrulewidth}{0pt}
\fancypagestyle{plain}{\fancyhf{}\fancyfoot[LE]{\thepage}\fancyfoot[RO]{\thepage}}

%% Compact system: reduce margins.
\expandafter\ifiseries@temp@compact\expandafter{\ifiseries@compact}

%% Empty clearpages (i.e., pages left blank before a chapter start).
\renewcommand*{\cleardoublepage}{%
  \clearpage
  \if@twoside
    \ifodd
      \c@page
    \else
      \thispagestyle{empty}
      \hbox{}
      \newpage
      \if@twocolumn
        \hbox{}
        \newpage
      \fi
    \fi
  \fi
}

%% Section headings.
\RequirePackage{sectsty}
\sectionfont{\normalfont\Large\ifiseries@sectionfontweight\ifiseries@titlefontfamily}
\subsectionfont{\normalfont\large\ifiseries@sectionfontweight\ifiseries@titlefontfamily}
\subsubsectionfont{\normalfont\normalsize\ifiseries@sectionfontweight\ifiseries@titlefontfamily}
\ififiseries@dottednumbers
  \def\@seccntformat#1{\csname the#1\endcsname.\quad}
  \def\@subseccntformat#1{\csname the#1\endcsname.\quad}
  \def\@subsubseccntformat#1{\csname the#1\endcsname.\quad}
\fi

%% Part title page (package 'sectsty' must be loaded first).
\newenvironment{centerpage}%
	{\begin{adjustwidth*}%
		{(\paperwidth-\textwidth)/2-1in-\oddsidemargin+\ifiseries@bindingoffset}%
		{(\paperwidth-\textwidth)/2-1in-\evensidemargin}%
	\centering}%
	{\end{adjustwidth*}}
\newcommand*{\typesetpart}[1]{{%
	\begin{centerpage}
	\null
	\vspace{.20\textheight}
	\interlinepenalty \@M
	\normalfont
	\ifnum \c@secnumdepth >-2\relax
		\LARGE\sc\bfseries\ifiseries@titlefontfamily \partname\nobreakspace\thepart
		\par\normalsize\normalfont
		\vskip 20\p@
	\fi
	\Huge \bfseries \ifiseries@titlefontfamily #1\par
	\end{centerpage}
}}
\renewcommand*{\part}{%
	\if@openright
		\FloatBarrier
		\newpage\thispagestyle{empty}\hbox{}
		\cleardoublepage
	\else
		\clearpage
	\fi
	\thispagestyle{empty}%
	\if@twocolumn
		\onecolumn
		\@tempswatrue
	\else
		\@tempswafalse
	\fi
	\secdef\@part\@spart%
}
\renewcommand*{\@part}[2][]{%
	\ifnum \c@secnumdepth >-2\relax
		\refstepcounter{part}%
		\addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
	\else
		\addcontentsline{toc}{part}{#1}%
	\fi
	\markboth{}{}%
	\typesetpart{#2}%
	\@endpart%
}

%% Chapter titles
%% cf. texlive/2011/texmf-dist/tex/latex/base/book.cls
\@ifclassloaded{book}{%
\renewcommand*{\@makechapterhead}[1]{%
  %\vspace*{20\p@}%
  {\parindent \z@ \flushright \normalfont
    \ifnum \c@secnumdepth >\m@ne
      \if@mainmatter
		\large\sc\ifiseries@chapterfontweight\ifiseries@titlefontfamily \@chapapp\space \thechapter
        \par\normalsize\normalfont\nobreak
        \vskip 20\p@
      \fi
    \fi
    \interlinepenalty\@M
    \huge \ifiseries@chapterfontweight \ifiseries@titlefontfamily #1\par\nobreak
    \vskip 50\p@
  }}
\renewcommand*{\@makeschapterhead}[1]{%
  %\vspace*{50\p@}%
  {\parindent \z@ \flushright
    \normalfont
    \interlinepenalty\@M
	{\huge\bfseries  #1\par\nobreak}
    \vskip 50\p@
  }}
}{} %%% end ifclassloaded book
} %%% end ifclassloaded not beamer

%% Bibliography.
\@ifclassloaded{book}{%
\newcommand*{\preparebibliography}{%
\renewcommand*{\refname}{\ifiseries@refname}%
\cleardoublepage
\phantomsection
\addcontentsline{toc}{chapter}{\refname}
}%
\newcommand*{\tocbibliography}{%
\preparebibliography%
\ififiseries@biblatex\printbibliography\else\bibliography{\ifiseries@bibresource}\fi}}{%
\newcommand*{\preparebibliography}{%
\renewcommand*{\refname}{\ifiseries@refname}%
\phantomsection
\addcontentsline{toc}{section}{\refname}
}%
\newcommand*{\tocbibliography}{%
\preparebibliography%
\ififiseries@biblatex\printbibliography\else\bibliography{\ifiseries@bibresource}\fi}}

%% For multiple authors.
\newcommand*{\andauthor}{\\[.5\baselineskip]}

%% Command to create generic cover pages.
\ifthenelse{\equal{\ifiseries@compact}{}}{\newcommand*{\ifiseries@titlepagetopskip}{}}%
	{\newcommand*{\ifiseries@titlepagetopskip}{\vspace*{5ex}}}
\newcommand{\gentitlepage}[4]{%
\newpage
\thispagestyle{empty}
\begin{centerpage}
\ifiseries@titlepagetopskip%
\Huge #1\\
\normalsize\vspace{5ex}
\Large \ifthenelse{\equal{#2}{}}{\mbox{ }}{#2}\\
\normalsize\vspace{5ex}
\Large #3\\
\normalsize\vfill
#4
\end{centerpage}}

%% Command to create the cover page for dissertations.
\newcommand{\disstitlepage}[5]{%
\gentitlepage{#1}{#2}{#3}{%
Dissertation\\
zur Erlangung des akademischen Grades\\
%
\ifstrequal{#4}{nat}{%
  Doktor der Naturwissenschaften\\%
  (Dr.~rer.~nat.)\\%
  }{\ifstrequal{#4}{ing}{%
  Doktor der Ingenieurwissenschaften\\%
  (Dr.-Ing.)\\%
  }{}}%
%
der Technischen Fakult\"at\\
der Christian-Albrechts-Universit\"at zu Kiel\\
eingereicht im Jahr #5}}

%% Command to create the cover page for student's thesis.
\newcommand{\studtitlepage}[7]{%
\gentitlepage{#1}{#2}{#3}{%
#4\\
eingereicht im Jahr #5\\
\vspace{1ex}
Christian-Albrechts-Universit\"at zu Kiel\\
#6\\
\vspace{1ex}
Betreut durch: #7}}

%% Command to create the reviewer page for dissertations.
\newcommand{\dissreviewerpage}[4]{%
\newpage
\thispagestyle{plain}
\vspace*{.5\textheight}
\noindent
\begin{tabular}{@{}p{.20\textwidth}@{\hspace{.05\textwidth}}p{.75\textwidth}@{}}
  1.~Gutachter: & \ifthenelse{\equal{#1}{_}}{\rule{12em}{.3pt}}{#1} \\[1em]
  2.~Gutachter: & \ifthenelse{\equal{#2}{_}}{\rule{12em}{.3pt}}{#2}
  \ifthenelse{\equal{#3}{}}{}{\\[1em] 3.~Gutachter: & \ifthenelse{\equal{#3}{_}}{\rule{12em}{.3pt}}{#3}}
\end{tabular}
\par
\vspace*{2em}
\noindent
Datum der m\"undlichen Pr\"ufung:~\ifthenelse{\equal{#4}{_}}{\rule{12em}{.3pt}}{#4}}

%% Eidesstattliche Erklaerung
\newcommand*{\eidesstatt}{%
\newpage
\thispagestyle{plain}
\hskip 0mm
\vfill
\noindent
\begin{otherlanguage}{ngerman}
\textbf{Eidesstattliche Erkl{\"a}rung}\par
\bigskip\noindent Hiermit erkl{\"a}re ich an Eides statt, dass
ich die vorliegende Arbeit selbst\-st{\"a}n\-dig verfasst und keine
anderen als die angegebenen Quellen und Hilfsmittel verwendet habe.\par
\bigskip\noindent Kiel,
\vskip 10mm
\hfill\rule{18em}{.3pt}%
\end{otherlanguage}}

%% Command to create a page with meta data.
\newcommand{\metapage}[7]{%
\newpage%
\thispagestyle{plain}%
\footnotesize%
\mbox{}\vfill%
\begin{itemize}[label=]%
  \item Kiel Computer Science Series (KCSS) #1%
  \item 
	ISSN 2193-6781 (print version)\\
	ISSN 2194-6639 (electronic version)
  \item Electronic version, updates, errata available via
       \url{https://www.informatik.uni-kiel.de/kcss}
  \ifthenelse{\equal{#2}{}}{}{\item The author can be contacted via #2}%
  \item Published by the Department of Computer Science, Christian-Albrechts-Universit\"at zu Kiel%
  \item #3%
  \item Please cite as:%
       \begin{itemize}%
         \item #4%
       \end{itemize}%
  \item \lstinputlisting[%
    nolol=true,frame=none,numbers=none,language={},style={},keywordsprefix={},basicstyle=\tt,%
    aboveskip=\medskipamount,belowskip=\medskipamount,lineskip=0pt,boxpos=c,print=true,%
    showlines=false,gobble=0,tabsize=8,showtabs=false,showspaces=false,%
    linewidth=\linewidth,xleftmargin=0pt,xrightmargin=0pt,resetmargins=false,breaklines=false,%
    columns={[c]fixed},flexiblecolumns=false,keepspaces=false,basewidth={0.6em,0.45em},fontadjust=false,%
    mathescape=false,escapechar={},escapeinside={},%
    ]{#5}%
  \item \textcopyright\ #6%
  \ifthenelse{\equal{#7}{}}{}{\item #7}%
\end{itemize}%
\normalsize}

%% Paragraph skip instead of paragraph indentation.
\ififiseries@halfparskip
    \setlength{\parskip}{.5\baselineskip plus .5\baselineskip}
    \setlength{\parindent}{0em}
    \setlength{\parfillskip}{\ifiseries@halfparskipfill\textwidth plus 1fil}
\fi

%% Enumeration environments.
%
\ififiseries@halfparskip
%
% For halfparskip, we set topsep to 0 everywhere.
% This seems to be necessary to prevent excessive whitespace.
%
\setitemize{leftmargin=*, label=$\triangleright$, topsep=0em}
\setenumerate{leftmargin=*, topsep=0em}
\setdescription{leftmargin=\parindent, style=sameline, font=\it, topsep=0em}
\setlist[compactitemize]{%
  label=\raisebox{.1em}{\smaller\textbullet},%
  leftmargin=*,%
  topsep=.0em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
\setlist[compactitemize,2,3]{%
  topsep=.0em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
%
\setlist[compactenumerate]{%
  label=\arabic*.,%
  leftmargin=*,%
  topsep=.0em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
% FIXME: third level does not work for compactenumerate
\setlist[compactenumerate,2]{%
  label=(\alph*),%
  topsep=.0em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
%
\setlist[compactdescription]{%
  style=sameline,%
  font=\it,%
  leftmargin=\parindent,%
  topsep=.0em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
\setlist[compactdescription,2,3]{%
  topsep=.0em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
\else
%
% Settings for non-halfparskip (the normal settings).
%
\@ifclassloaded{beamer}{\setitemize{leftmargin=*,%
  label={\usebeamercolor[fg]{itemize item}%
  \scriptsize\raise1.25pt\hbox{\donotcoloroutermaths$\blacktriangleright$}}}%
  \setenumerate{leftmargin=*}%
  \setdescription{font={\normalfont\usebeamercolor[fg]{itemize item}}}}%
  {\setitemize{leftmargin=*, label=$\triangleright$}%
   \setenumerate{leftmargin=*}%
   \setdescription{leftmargin=\parindent, style=sameline, font=\it}}
\setlist[compactitemize]{%
  label=\raisebox{.1em}{\smaller\textbullet},%
  leftmargin=*,%
  topsep=.4em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
\setlist[compactitemize,2,3]{%
  topsep=.1em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
%
\setlist[compactenumerate]{%
  label=\arabic*.,%
  leftmargin=*,%
  topsep=.4em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
\setlist[compactenumerate,2,3]{%
  topsep=.1em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
%
\setlist[compactdescription]{%
  style=sameline,%
  font=\it,%
  leftmargin=\parindent,%
  topsep=.4em plus .1em,%
  parsep=.2em plus .1em,%
  itemsep=.2em plus .1em,%
}
\setlist[compactdescription,2,3]{%
  topsep=.1em,%
  partopsep=.1em,%
  parsep=.1em,%
  itemsep=.1em,%
}
\fi

%% Figures and captions.
\captionsetup{font=small,labelfont=bf,labelsep=period}
\ifdefstring{\ifiseries@figure}{floatrow}{%
  \captionsetup[subfigure]{labelformat=parens,labelsep=space}%
  \floatsetup[table]{style=plaintop}%
  \floatsetup[figure]{capbesideposition={center,inside},facing=yes}}{}
% Alter some defaults for better treatment of figures.
% Taken from http://mintaka.sdsu.edu/GF/bibliog/latex/floats.html
% See also p.105 of "TeX Unbound" for suggested values.
% General parameters, for ALL pages:
\renewcommand*{\topfraction}{0.9} % max fraction of floats at top
\renewcommand*{\bottomfraction}{0.8}	% max fraction of floats at bottom
% Parameters for TEXT pages (not float pages):
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4} % 2 may work better
\setcounter{dbltopnumber}{2} % for 2-column pages
\renewcommand*{\dbltopfraction}{0.9}	% fit big float above 2-col. text
\renewcommand*{\textfraction}{0.07} % allow minimal text w. figs
% Parameters for FLOAT pages (not text pages):
\renewcommand*{\floatpagefraction}{0.7} % require fuller float pages
% N.B.: floatpagefraction MUST be less than topfraction
\renewcommand*{\dblfloatpagefraction}{0.7} % require fuller float pages

%% Hyperref's \autoref feature.
\@ifclassloaded{beamer}{}{%
\providecommand{\subfigureautorefname}{\figureautorefname}
% \extrasenglish seem to be invoked twice, so need \renew.. there, hence all \...refname must be defined
\ifthenelse{\isundefined{\algorithmautorefname}}{\newcommand*{\algorithmautorefname}{Algorithm}}{}
\addto\extrasenglish{
  \renewcommand*{\chapterautorefname}{Chapter}
  \renewcommand*{\sectionautorefname}{Section}
  \renewcommand*{\subsectionautorefname}{Section}
  \renewcommand*{\algorithmautorefname}{Algorithm}}
} %%% end ifclassloaded not beamer

%% Configure listings package.
\lstset{basicstyle=\tt}

%% Configure the url package, and adjust \href command.
\ifthenelse{\isundefined{\DeclareUrlCommand}}{\renewcommand*{\path}[1]{\url{#1}}}{\DeclareUrlCommand\path{}} %%% workaround
\renewcommand*{\UrlFont}{\smaller\tt}
\let\@href\href
\renewcommand*{\href}[3][]{\@href[#1]{#2}{\UrlFont #3}}

%% Display settings.
\allowdisplaybreaks
\AtBeginDocument{
  \setlength{\abovedisplayshortskip}{-.5\baselineskip plus .5\baselineskip}
  \setlength{\abovedisplayskip}{\medskipamount}
  \setlength{\belowdisplayshortskip}{\medskipamount}
  \setlength{\belowdisplayskip}{\medskipamount}
}

\fi % ifiseries@layout
