\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{cursuspresentatie}[2022/08/30 cursuspresentatie]

\RequirePackage{etoolbox}
\RequirePackage{xkeyval}
\RequirePackage{../../acrn}

\define@boolkey{cursuspresentatie}{everyoneauthor}[true]{
    \ifbool{KV@cursuspresentatie@everyoneauthor}{
        \acrnAllAuthorsTrue
    }{
        \acrnAllAuthorsFalse
    }
}

\define@boolkey{cursuspresentatie}{slidenames}[true]{}

\DeclareOptionX{allauthors}{
    \acrnAllAuthorsTrue
}

% \DeclareOptionX{everyoneauthor}{
%     \acrnAllAuthorsTrue
% }

\newbool{english}
\newbool{aestheme}
\newbool{ishandout}
\newbool{darktheme}

\booltrue{english}

\def\defaultSlideCollection{.}

\DeclareOptionX{english}{
    \booltrue{english}
}

\DeclareOptionX{dutch}{
    \boolfalse{english}
}

\DeclareOptionX{aes}{
    \booltrue{aestheme}
}

\DeclareOptionX{handout}{
    %\boolfalse{darktheme}
    \booltrue{ishandout}
}

\DeclareOptionX{darktheme}{
    %\ifbool{ishandout}{}{
        \booltrue{darktheme}
    %}
}

\DeclareOptionX{defaultSlideCollection}[.]{%
    \def\defaultSlideCollection{#1}%
}

% \DeclareOptionX*{%
%     %\typeout{CurrentOption: \meaning\CurrentOption}
%     \def\procCursusPresentatieOption#1{
%         \PackageWarning{debug}{Option is #1}
%         \setkeys{cursuspresentatie}{#1}
%     }
%     \expandafter\procCursusPresentatieOption\expandafter{\CurrentOption}%
% }

\ProcessOptionsX<cursuspresentatie>
\ProcessOptionsX

\acrnDefName\copyrightThomas{Thomas van Maaren}{2022--2024}
\acrnDefName\copyrightHanneke{Hanneke Schroten}{2022--2024}
\acrnDefName\copyrightTim{Tim Weijers}{2022--2024}
\acrnDefName\copyrightVincent{Vincent Kuhlmann}{2021--2024}
\acrnDefName\copyrightJesse{Jesse Straat}{2024--2024}

% Detail 5: rehearsal
% Detail 10: crash course
% Detail 20: default
% Detail 30: detailed

\newcounter{detaillevel}
\setcounter{detaillevel}{20}

\def\setDetail#1{\setcounter{detaillevel}{#1}}

\let\endDetail\fi

\def\beginDetail#1{%
    % \unless\ifnum\value{detaillevel}<#1\relax
    % 	#2%
    % \fi
    \unless\ifnum\value{detaillevel}<#1\relax
}

\newcommand\detailedinline[2]{%
    \detailStart{#1}#2\detailEnd
}

\def\@beginFrameSelection@i#1{%
    \let\frameSelection\relax
    \begin{frame}<#1>%
}

\def\beginFrameWithSelection#1{%
    \expandafter\@beginFrameSelection@i\expandafter{#1}%
}

% \newenvironment{detailed}[1]{}{}


% skipt animaties voor pdf files voor studenten pdf geven, de juiste boolean uncommenten
% booleans in latex zijn 
%\setbool{ishandout}{true}

% aspectratio=169: aspectratio 16:9
% aspectratio=32: aspectratio 3:2

\PassOptionsToPackage{usenames,dvipsnames}{xcolor}

\ifbool{ishandout}{
    \LoadClass[handout,aspectratio=169,usenames,dvipsnames]{beamer}
}{
    \LoadClass[aspectratio=169]{beamer}
}

\RequirePackage[tabsize=4]{highlightlatex}

\let\currSlideImportName\relax

\ifbool{KV@cursuspresentatie@slidenames}{
    \PackageWarning{debug}{Showing slidenames}
    \setbeamertemplate{page number in head/foot}{%
        \currSlideImportName
    }
}{

}

\setbeamertemplate{caption}[numbered] % voor het nummeren van figuren in beamer

\definecolor{red1}{RGB}{185, 70, 70}
\definecolor{red2}{RGB}{152, 58, 58}
\definecolor{red3}{RGB}{122, 46, 46}
\definecolor{red4}{RGB}{142, 26, 26}
\definecolor{blue1}{rgb}{0.2,0.2,0.7}

%hsla(0, 55%, 45%, 1)
\definecolor{red5}{RGB}{178, 52, 52}

%hsla(0, 55%, 32%, 1)
\definecolor{red6}{RGB}{126, 37, 37}

%hsla(0, 68%, 45%, 1)
\definecolor{red7}{RGB}{193, 37, 37}

%hsla(0, 68%, 38%, 1)
\definecolor{red8}{RGB}{163, 31, 31}

%hsla(0, 45%, 67%, 1)
\definecolor{red9}{RGB}{209, 133, 133}

%hsla(0, 100%, 45%, 1)
\definecolor{red10}{RGB}{230, 0, 0}

%hsla(0, 73%, 45%, 1)
\definecolor{red11}{RGB}{199, 31, 31}

\definecolor{themepurple}{RGB}{95, 31, 199}

% \definecolor{themegreen}{RGB}{31, 199, 31}
% \definecolor{themegreen}{RGB}{25, 159, 25}
\definecolor{themegreen}{RGB}{32, 131, 32}

\definecolor{darkmodebackground}{RGB}{30, 30, 30}
% \definecolor{darkmodeforeground}{RGB}{219, 221, 170}
\definecolor{darkmodeforeground}{RGB}{212, 212, 212}

\ifbool{aestheme}{
    \usetheme{aes2}

    \usecolortheme{rose}
    %\useinnertheme[shadow]{rounded}
    \useinnertheme{rounded}

    \setbeamertemplate{footline}{}
}{
    \usetheme{Dresden}
    %\usecolortheme{dolphin}
    \useoutertheme{miniframes}

    %\PackageWarning{debug}{\expandafter\meaning\csname XC@clr@rgb@s}

    % BEGIN Based on beamer/beamercolorthemedolphin.sty
    \mode<presentation>

    \def\themeRed{
        \setbeamercolor*{structure}{fg=red11}

        \ifbool{darktheme}{
            % BEGIN Based on https://tex.stackexchange.com/questions/57477/beamer-dark-theme
            % by https://tex.stackexchange.com/users/188576/j-xu
            \setbeamercolor{frametitle}{fg=darkmodeforeground}
            \setbeamercolor{background canvas}{bg=darkmodebackground}
            \setbeamercolor{normal text}{fg=darkmodeforeground}
            % END Based on
        }{}

        \setbeamercolor*{palette primary}{use=structure,fg=black,bg=structure.fg!40!orange}
        \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.fg!60!orange}%,bg=red9}
        \setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.fg!90!orange}%,bg=red1}
        \setbeamercolor*{palette quaternary}{fg=white,bg=black}

        \setbeamercolor*{sidebar}{use=structure,bg=structure.fg}
        
        \setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.fg!10}
        \setbeamercolor*{palette sidebar secondary}{fg=white}
        \setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.fg!50}
        \setbeamercolor*{palette sidebar quaternary}{fg=white}

        \setbeamercolor*{titlelike}{use=structure,fg=structure.fg}

        \setbeamercolor*{separation line}{}
        \setbeamercolor*{fine separation line}{}


        \colorlet{navigationTileOutline}{red!40!orange}
        \colorlet{exerciseTileOutline}{red!20!orange}
        % \colorlet{exerciseTileBackground}{bg!80!orange}
        %\colorlet{exerciseTileBackground}{structure.bg!80!orange}
        \colorlet{exerciseTileBackground}{red11!80!orange}
        %structure.bg!80!orange

        \colorlet{underlineColor}{red}
        \colorlet{navigationUnderlineColor}{yellow}
    }
    \def\themeRedTwo{
        \setbeamercolor*{structure}{fg=red11}

        \ifbool{darktheme}{
            % BEGIN Based on https://tex.stackexchange.com/questions/57477/beamer-dark-theme
            % by https://tex.stackexchange.com/users/188576/j-xu
            \setbeamercolor{frametitle}{fg=darkmodeforeground}
            \setbeamercolor{background canvas}{bg=darkmodebackground}
            \setbeamercolor{normal text}{fg=darkmodeforeground}
            % END Based on
        }{}

        \setbeamercolor*{palette primary}{use=structure,fg=black,bg=structure.fg!50!orange}
        \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.fg!80!orange}%,bg=red9}
        \setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.fg!95!orange}%,bg=red1}
        \setbeamercolor*{palette quaternary}{fg=white,bg=black}

        \setbeamercolor*{sidebar}{use=structure,bg=structure.fg}
        
        \setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.fg!10}
        \setbeamercolor*{palette sidebar secondary}{fg=white}
        \setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.fg!50}
        \setbeamercolor*{palette sidebar quaternary}{fg=white}

        \setbeamercolor*{titlelike}{use=structure,fg=structure.fg}

        \setbeamercolor*{separation line}{}
        \setbeamercolor*{fine separation line}{}


        \colorlet{navigationTileOutline}{red!40!orange}
        \colorlet{exerciseTileOutline}{red!20!orange}
        % \colorlet{exerciseTileBackground}{bg!80!orange}
        %\colorlet{exerciseTileBackground}{structure.bg!80!orange}
        \colorlet{exerciseTileBackground}{red11!80!orange}
        %structure.bg!80!orange

        \colorlet{underlineColor}{red}
        \colorlet{navigationUnderlineColor}{yellow}
    }
    \def\themePurple{
        \setbeamercolor*{structure}{fg=themepurple,bg=themepurple}

        \ifbool{darktheme}{
            % BEGIN Based on https://tex.stackexchange.com/questions/57477/beamer-dark-theme
            % by https://tex.stackexchange.com/users/188576/j-xu
            \setbeamercolor{frametitle}{fg=darkmodeforeground}
            \setbeamercolor{background canvas}{bg=darkmodebackground}
            \setbeamercolor{normal text}{fg=darkmodeforeground}
            \setbeamercolor{structure}{fg=themepurple!50!CornflowerBlue}
            % END Based on
        }{}

        \setbeamercolor*{palette primary}{use=structure,fg=black,bg=structure.bg!40!CornflowerBlue}
        \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.bg!60!CornflowerBlue}%,bg=red9}
        \setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.bg!90!CornflowerBlue}%,bg=red1}
        \setbeamercolor*{palette quaternary}{fg=white,bg=black}

        \setbeamercolor*{sidebar}{use=structure,bg=structure.fg}
        
        \setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.bg!10}
        \setbeamercolor*{palette sidebar secondary}{fg=white}
        \setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.bg!50}
        \setbeamercolor*{palette sidebar quaternary}{fg=white}

        \setbeamercolor*{titlelike}{use=structure,fg=structure.fg}

        \setbeamercolor*{separation line}{}
        \setbeamercolor*{fine separation line}{}

        \colorlet{navigationTileOutline}{themepurple!40!CornflowerBlue}
        \colorlet{exerciseTileOutline}{themepurple!20!CornflowerBlue}
        \colorlet{exerciseTileBackground}{themepurple!80!CornflowerBlue}

        \colorlet{underlineColor}{blue}
        \colorlet{navigationUnderlineColor}{yellow}
    }
    \def\themePurpleTwo{
        \setbeamercolor*{structure}{fg=themepurple}

        \ifbool{darktheme}{
            % BEGIN Based on https://tex.stackexchange.com/questions/57477/beamer-dark-theme
            % by https://tex.stackexchange.com/users/188576/j-xu
            \setbeamercolor{frametitle}{fg=darkmodeforeground}
            \setbeamercolor{background canvas}{bg=darkmodebackground}
            \setbeamercolor{normal text}{fg=darkmodeforeground}
            % END Based on
        }{}

        \setbeamercolor*{palette primary}{use=structure,fg=black,bg=structure.fg!40!Cyan}
        \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.fg!60!Cyan}%,bg=red9}
        \setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.fg!90!Cyan}%,bg=red1}
        \setbeamercolor*{palette quaternary}{fg=white,bg=black}

        \setbeamercolor*{sidebar}{use=structure,bg=structure.fg}
        
        \setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.fg!10}
        \setbeamercolor*{palette sidebar secondary}{fg=white}
        \setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.fg!50}
        \setbeamercolor*{palette sidebar quaternary}{fg=white}

        \setbeamercolor*{titlelike}{use=structure,fg=structure.fg}

        \setbeamercolor*{separation line}{}
        \setbeamercolor*{fine separation line}{}

        \colorlet{navigationTileOutline}{themepurple!40!Cyan}
        \colorlet{exerciseTileOutline}{themepurple!20!Cyan}
        \colorlet{exerciseTileBackground}{themepurple!80!Cyan}

        \colorlet{underlineColor}{blue}
    }
    \def\themeGreen{
        \setbeamercolor*{structure}{fg=themegreen,bg=themegreen}

        \ifbool{darktheme}{
            % BEGIN Based on https://tex.stackexchange.com/questions/57477/beamer-dark-theme
            % by https://tex.stackexchange.com/users/188576/j-xu
            \setbeamercolor{frametitle}{fg=darkmodeforeground}
            \setbeamercolor{background canvas}{bg=darkmodebackground}
            \setbeamercolor{normal text}{fg=darkmodeforeground}
            \setbeamercolor{structure}{fg=themegreen!50!green}
            % END Based on
        }{}

        \setbeamercolor*{palette primary}{use=structure,fg=black,bg=structure.bg!40!green}
        \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=structure.bg!60!green}%,bg=red9}
        \setbeamercolor*{palette tertiary}{use=structure,fg=white,bg=structure.bg!90!green}%,bg=red1}
        \setbeamercolor*{palette quaternary}{fg=white,bg=black}

        \setbeamercolor*{sidebar}{use=structure,bg=structure.fg}
        
        \setbeamercolor*{palette sidebar primary}{use=structure,fg=structure.bg!10}
        \setbeamercolor*{palette sidebar secondary}{fg=white}
        \setbeamercolor*{palette sidebar tertiary}{use=structure,fg=structure.bg!50}
        \setbeamercolor*{palette sidebar quaternary}{fg=white}

        \setbeamercolor*{titlelike}{use=structure,fg=structure.fg}

        \setbeamercolor*{separation line}{}
        \setbeamercolor*{fine separation line}{}

        \colorlet{navigationTileOutline}{themegreen!40!green}
        \colorlet{exerciseTileOutline}{themegreen!20!green}
        \colorlet{exerciseTileBackground}{themegreen!80!green}

        \colorlet{underlineColor}{blue}
        \colorlet{navigationUnderlineColor}{yellow!80!blue}
    }

    % \themeRed
    % \themeRedTwo
    % \themePurple
    \themeGreen

    \mode
    <all>
    % END Based on
}

% Removes beamer navigation symbols
\setbeamertemplate{navigation symbols}{}

%\RequirePackage{minted}
\RequirePackage{subfiles}
\RequirePackage{amsmath,amssymb,amsthm,commath,mathtools}
\RequirePackage{esint}
%\RequirePackage{enumerate}
\RequirePackage{subcaption}
\RequirePackage{graphicx}
\PassOptionsToPackage{usenames,dvipsnames}{xcolor}
\RequirePackage{xcolor}
\RequirePackage{adjustbox}
\RequirePackage{soul}
\RequirePackage{booktabs}
\RequirePackage{tabularx}
\RequirePackage{environ}
\RequirePackage{etoolbox}
\RequirePackage{yfonts}

\ifbool{english}{
    \RequirePackage[english]{babel}
}{
    \RequirePackage[dutch]{babel}
}
\RequirePackage[utf8]{inputenc}
\RequirePackage{fancyvrb}
\RequirePackage{marvosym}
\RequirePackage[style=numeric]{biblatex}
\RequirePackage{textcomp}
\RequirePackage{enumitem}
\RequirePackage{hyperref}
\RequirePackage{xkeyval}
\RequirePackage[T1]{fontenc}
\RequirePackage{textgreek}
\RequirePackage{lmodern}
\RequirePackage[version=4]{mhchem}

\RequirePackage{ragged2e}

\RequirePackage{import}
%\RequirePackage{inputslide}
\RequirePackage{multicol}
\RequirePackage{csquotes}
\RequirePackage{minted}
\RequirePackage{pbox}
\RequirePackage{tikz}

\usetikzlibrary{calc,positioning,quotes}

%\newcommand{\TeXniCie}{\TeX niCie}

% \def\importslide#1#2{%
% 	\import{../../slides/#1}{#2}
% }

\def\importslide#1#2{%
    \IfFileExists{../../slides/\defaultSlideCollection/#1/#2}{%
        \def\currSlideImportName{\defaultSlideCollection/#1/#2}%
        \import{../../slides/\defaultSlideCollection/#1}{#2}%
        \let\currSlideImportName\relax
    }{%
        \def\currSlideImportName{#1/#2}%
        \import{../../slides/#1}{#2}%
        \let\currSlideImportName\relax
    }%
}

%\newcommand\lang[2]{%

% \lang,something,iets, kijk eens
% \lang{something}{iets} kijk eens

\makeatletter
\def\lang{%
    \@ifnextchar,%
    {\lang@i}%
    {\lang@ii}%
}


\def\lang@i,#1,#2,{%
    \ifbool{english}{%
        #1%
    }{%
        #2%
    }%
}
\long\def\lang@ii#1#2{%
    \ifbool{english}{%
        #1%
    }{%
        #2%
    }%
}
\makeatother

\DeclareMathOperator{\Image}{Image}

% voor filenames met langsuffix
\def\langsuffix{}
\lang{\gdef\langsuffix{EN}}{}

% \addbibresource{\subfix{assets/fakebib.bib}}

% gekleurde underline werkt niet in beamer
% Source: https://tex.stackexchange.com/questions/41683/why-is-it-that-coloring-in-soul-in-beamer-is-not-visible
\let\UL\ul
\makeatletter
\renewcommand\ul{%
    \let\set@color\beamerorig@set@color
    \let\reset@color\beamerorig@reset@color
    \UL
}

\let\ST\st
\makeatletter
\def\st#1{
    \begingroup
    \let\set@color\beamerorig@set@color
    \let\reset@color\beamerorig@reset@color
    \def\SOUL@uleverysyllable{%
        \rlap{%
            %\color{red}
            \the\SOUL@syllable
            \SOUL@setkern\SOUL@charkern}%
        \SOUL@ulunderline{%
            \phantom{\the\SOUL@syllable}}%
    }%
    \ST{#1}%
    \endgroup
}
\makeatother
% https://tex.stackexchange.com/questions/71051/strikeout-in-different-color-appears-behind-letters-not-on-top-of-them

\setulcolor{underlineColor}
\setstcolor{underlineColor}

% Override if you want. Else you can delete it.
%\colorlet{curlyBrackets}{red!50!blue}
%\colorlet{squareBrackets}{blue!50!white}
%\colorlet{codeBackground}{gray!10!white}
%\colorlet{comment}{green!40!black}

%TODO op termijn verwijderen bij overstap naar minted
\updatehighlight{
    name = default,
    color = {blue!90!black},
    add = {
        \knowncommand, \figref, \textcolor, \maketitle, \subsubsection,
        \textasciigrave, \textasciiacute, \tag, \middle, \mathbb, \abs,
        \mathcal, \middle, \dfrac, \subfile, \autoref, \eqref, \cites,
        \tableofcontents, \printbibliography, \fullcite, \parencite,
        \addbibresource, \DeclareLanguageMapping, \textcite, \intertext,
        \sum, \dif, \norm, \text, \dod, \dpd, \int, \partial,
        \DeclareMathOperator, \textfb,
        \rhead, \lhead,\rfoot,\lfoot,\fancyhf,
        \co, \term, \genummerd, \bb, \cc, \dd, \ee,
        \ProvidesClass, \LoadClass, \RequirePackage, \geometry,
        \fancyhead, \fancyfoot, \sum, \adjustbox, \dimexpr, \NewEnviron,
        \BODY, \ce,
        \sum, \implies, \exists, \forall, \bigcup
    },
    name = structure,
    add = {
    },
}

\updatehighlight{
    name = greenDollar,
    style = {\itshape\color{green!70!black}},
    add = {
        % The dollar sign is provided an extra time just to
        % calm down TeXstudio's code highlighting.
        $, $
    },
    name = accentA,
    color = green!60!black,
    add = {
        \inAccA
    },
    %
    name = accentB,
    color = red!60!black,
    add = {
        \inAccB, \includegraphics
    },
    %
    name = accentC,
    color = orange!100!black,
    add = {
        \inAccC
    }
}

%TODO verwijderen, highlight latex gebruikt lst
\lstset{tabsize=4}

%TODO gobble hoort ook bij highlight
\def\defaultgobble{8}


\def\Zphantomconceal#1#2{%
    \only<#2->{\rlap{#1}}\phantom{#1}%
    %\only<#2->{#3}\unless\ifishandout\only<-#1>{\phantom{#3}}\fi
}

\def\phantomconceal#1#2{%
    \Zphantomconceal{#1}{#2}%
}

% toont wel de dollartekens maar de formule nog niet, verbert het wel maar zorgt wel dat het de breedte inneemt.
\newcommand\hideformula[2][2]{%
    %\hll|$| \only<2->{\hll|\\sqrt\{2\}|}\only<-1>{??} \hll|$|
    \hll|$| \phantomconceal{\hll|#2|}{#1} \hll|$|
}

\newcommand\hidelatex[2][2]{%
    \phantomconceal{\hll|#2|}{#1}
}%

% ook nodig voor hiden van formule
\newcount\showcount

% voor formules tonen en niet

%\newcommand\showformula[2]{%
%	#1 & %
%	\expandafter\hideformula\expandafter[\the\showcount]{#2}%
%}
%
%\newcommand\showformula[2]{%
%	\global\showcount=\numexpr\showcount + 1\relax
%	\showformula*{#1}{#2}%
%}

\makeatletter

\def\showformula@i#1#2{%
    #1 & %
    \expandafter\hideformula\expandafter[\the\showcount]{#2}%
}

%\def\showformula{%
%	\@ifstar{%
%		\global\showcount=\numexpr\showcount + 1\relax
%		\showformula@i
%	}{%
%		\showformula@i
%	}%
%}

\def\showformula#1#2{
    #1 & \global\showcount=\numexpr\showcount + 1\relax
    \expandafter\hideformula\expandafter[\the\showcount]{#2}%
}

\def\showformulaa#1#2{
    #1 & %
    \expandafter\hideformula\expandafter[\the\showcount]{#2}%
}

\def\showlatex#1#2{
    #1 & \global\showcount=\numexpr\showcount + 1\relax
    \expandafter\hidelatex\expandafter[\the\showcount]{#2}%
}

\def\showlatexx#1#2{
    #1 & %
    \expandafter\hidelatex\expandafter[\the\showcount]{#2}%
}

\makeatother

% elke regel heeft vaste breedte, gebruikt in citatiecommand I en II slides
\newlength{\naturalwidth}
\newlength{\minimumwidth}
\newbox\naturalsizebox
\newcommand{\atleastwidth}[2][2cm]{%
    \savebox\naturalsizebox{#2}%
    \settowidth\naturalwidth{#2}%
    \naturalwidth=\wd\naturalsizebox
    \minimumwidth=\dimexpr #1\relax
    \leavevmode%(\the\naturalwidth, \the\minimumwidth)%
    \ifdim\naturalwidth<\minimumwidth\relax
    \makebox[\minimumwidth][l]{\usebox{\naturalsizebox}}%
    \else
    \usebox{\naturalsizebox}%
    \fi
}

\newcommand{\atleastwidthr}[2][2cm]{%
    \savebox\naturalsizebox{#2}%
    \settowidth\naturalwidth{#2}%
    \naturalwidth=\wd\naturalsizebox
    \minimumwidth=\dimexpr #1\relax
    \leavevmode%(\the\naturalwidth, \the\minimumwidth)%
    \ifdim\naturalwidth<\minimumwidth\relax
    \makebox[\minimumwidth][r]{\usebox{\naturalsizebox}}%
    \else
    \usebox{\naturalsizebox}%
    \fi
}

% TODO vervangen door minted zorgt voor klein beetje marge links van codeblok
\lstset{framexleftmargin=0.25em,xleftmargin=0.25em}

% maakt een blauwe box, donkerblauw frame
\NewEnviron{bluebox}{
    \begingroup
        \adjustbox{cfbox=blue!40!white 2pt 10pt,valign=t,bgcolor=blue!5!white}{%
            \begin{minipage}[t]{\dimexpr\linewidth-24pt\relax}
                \BODY
            \end{minipage}%
        }%
    \endgroup
}

% recente geleerde keywords bovenin slide
\newcounter{maxrecentdisplay}
\setcounter{maxrecentdisplay}{27}

\newcounter{recentcount}
\setcounter{recentcount}{0}

\newcounter{recentskipremaining}

\def\vertlistsep{\hspace{2em}\textcolor{white!100!black}{\vrule width 0.5pt height 0.7\baselineskip\relax}\hspace{2em}}

\def\recentlist{}

%\newcommand{\addtorecentlist}[1]{%
%	\let\do\relax
%	\xdef\recentlist{\recentlist\do{#1}}%
%}

\newcommand{\addtorecentlist}[1]{%
    \bgroup
        \let\do\relax
        \expandafter\gdef\expandafter\recentlist\expandafter{\recentlist\do{#1}}%
        \addtocounter{recentcount}{1}%
    \egroup
    %
    %\xdef\recentlist{\recentlist\do{#1}}%
}

\newcommand{\clearrecentlist}{%
    \gdef\recentlist{}%
    \setcounter{recentcount}{0}%
}

\newif\ifisfirstrecentitem
\newcommand{\printrecentlist}{%
    \setcounter{recentskipremaining}{0}%
    \ifnum\value{recentcount}>\value{maxrecentdisplay}
        \setcounter{recentskipremaining}{\value{recentcount}-\value{maxrecentdisplay}}
    \fi
    %(\therecentskipremaining)
    %(\meaning\recentlist)
    \isfirstrecentitemtrue
    \def\do##1{%
        \ifnum\value{recentskipremaining}>0\relax
            \addtocounter{recentskipremaining}{-1}%
        \else		
            \unless\ifisfirstrecentitem
            \vertlistsep
            \fi
            \isfirstrecentitemfalse
            \textbf{##1}%
        \fi
    }%
    \recentlist
}

\newcommand{\recentpopfront}[1][1]{%
    \typeout{recentpopfront, before: \meaning\recentlist}
    \setcounter{recentskipremaining}{#1}%
    \let\origrecentlist\recentlist
    \clearrecentlist
    \def\do##1{%
        \ifnum\value{recentskipremaining}>0\relax
            \addtocounter{recentskipremaining}{-1}%
        \else		
            \addtorecentlist{##1}%
        \fi
    }%
    \origrecentlist
    \typeout{recentpopfront, after: \meaning\recentlist}
}

\newsavebox\printrecentbox
\savebox\printrecentbox{}
\newsavebox\scratchbox

% \AtBeginDocument{
% \setbox\scratchbox\printrecentbox
% }

\newcommand{\saveprintrecentbox}{%
    \bgroup
        \savebox\printrecentbox{\printrecentlist}%
        \global\setbox\printrecentbox\box\printrecentbox
    \egroup
    % \setbox\scratchbox\printrecentbox
    % \global\setbox\printrecentbox\scratchbox
    % \ifdim\wd\printrecentbox>0.9\textwidth
    % 	\savebox\printrecentbox{\adjustbox{right=0.9\textwidth}{\printrecentlist}}%
    % \else
    % 	\savebox\printrecentbox{\adjustbox{left=0.9\textwidth}{\printrecentlist}}%
    % \fi
}

\newcommand{\shrinkrecentbox}[1]{%
    {\loop
        %\clearrecentlist
        %\saveprintrecentbox
        %(SavedEmptyBox)
        %\iffalse


        \ifdim\wd\printrecentbox>\dimexpr #1\relax
        %
        \recentpopfront[1]%
        \saveprintrecentbox
    \repeat}%
}

% Based on miniframes code
\setbeamertemplate{headline}
{%
    \begin{beamercolorbox}[colsep=1.5pt]{upper separation line head}%
    \end{beamercolorbox}
    \begin{beamercolorbox}{section in head/foot}
        \vskip2pt
        \insertnavigation{\paperwidth}%
        %\strut %AAAA
        \vskip4pt
    \end{beamercolorbox}%
    %
    % \begin{beamercolorbox}[colsep=1.5pt]{middle separation line head}
    % \end{beamercolorbox}%
    % \begin{beamercolorbox}[
    % 	ht=2.5ex,
    % 	dp=1.125ex,
    % 	leftskip=.3cm,rightskip=.3cm plus1fil
    % 	]{subsection in head/foot}
    % 	\usebeamerfont{subsection in head/foot}%\insertsubsectionhead
    % 	% \savebox\printrecentbox{\printrecentlist}%
    % 	% \ifdim\wd\printrecentbox>0.9\textwidth
    % 	% 	\adjustbox{right=0.9\textwidth}{\printrecentlist}%
    % 	% \else
    % 	% 	\adjustbox{left=0.9\textwidth}{\printrecentlist}%
    % 	% \fi
    % 	% \saveprintrecentbox
    % 	% \ifdim\wd\printrecentbox>0.9\textwidth
    % 	% 	%(Shrinking box)
    % 	% 	%\PackageError{debug}{Width is \the\wd\printrecentbox}{}%
    % 	% 	\shrinkrecentbox{0.6\textwidth}%
    % 	% \else
    % 	% 	%(Not shrinking box)
    % 	% \fi
    % 	% \usebox\printrecentbox
    % 	%\textbullet\ Hey
    % \end{beamercolorbox}%
    %
    \begin{beamercolorbox}[colsep=1.5pt]{lower separation line head}%
    \end{beamercolorbox}%
}

\setbeamertemplate{footline}
{%
    \begin{beamercolorbox}[colsep=1.5pt]{upper separation line foot}
    \end{beamercolorbox}
    %
    \begin{beamercolorbox}[
        ht=2.5ex,dp=1.125ex,%
        leftskip=.3cm,rightskip=.3cm plus1fil
    ]{author in head/foot}%
        \leavevmode{\usebeamerfont{author in head/foot}%
            \rlap{\insertshortauthor}%
            %\hfill Slides op texnicie.nl%
        }%
        \hfill%
        {\usebeamerfont{institute in head/foot}\usebeamercolor[fg]{institute in head/foot}\insertshortinstitute}%
    \end{beamercolorbox}%
    %
    \begin{beamercolorbox}[
        ht=2.5ex,dp=1.125ex,%
        leftskip=.3cm,rightskip=.3cm plus1fil
    ]{title in head/foot}%
        {\usebeamerfont{title in head/foot}\rlap{\insertshorttitle}}
        \hfill
        %Slides op texnicie.nl
        % \hfill{\usebeamercolor[fg]{page number in head/foot}\usebeamerfont{page number in head/foot}\llap{\usebeamertemplate{page number in head/foot}}}%
        \thepage
    \end{beamercolorbox}%
    %
    \begin{beamercolorbox}[colsep=1.5pt]{lower separation line foot}
    \end{beamercolorbox}
}

\setbeamertemplate{mini frame}{}
\setbeamertemplate{mini frame in current subsection}{}
\setbeamertemplate{mini frame in other subsection}{}
%\setbeamertemplate{mini frame in other subsection}{}

% \setbeamertemplate{headline}
% {%
%   \begin{beamercolorbox}[colsep=1.5pt]{upper separation line head}
%   \end{beamercolorbox}
%   \begin{beamercolorbox}{section in head/foot}
%     \vskip2pt\insertnavigation{\paperwidth}\vskip2pt
%   \end{beamercolorbox}%
% %   \ifbeamer@theme@subsection%
% %     \begin{beamercolorbox}[colsep=1.5pt]{middle separation line head}
% %     \end{beamercolorbox}
% %     \begin{beamercolorbox}[ht=2.5ex,dp=1.125ex,%
% %       leftskip=.3cm,rightskip=.3cm plus1fil]{subsection in head/foot}
% %       \usebeamerfont{subsection in head/foot}\insertsubsectionhead
% %     \end{beamercolorbox}%
% %   \fi%
%   \begin{beamercolorbox}[colsep=1.5pt]{lower separation line head}
%   \end{beamercolorbox}
% }

\makeatletter

\NewEnviron{colC}[2][]{%
    \def\setpadd{}%
    \if\relax #1\relax
    \else
        %\def\setpadd{padding={0pt {\dimexpr ((#1)-\height)\relax} {0pt} {0pt}}}%
        \def\setpadd{%
            set depth={\dimexpr (#1)-\height\relax}%
        }
    \fi
    % \def\setparboxargs{}%
    % \if\relax #1\relax
    % \else
    % 	\def\setparboxargs{[t][\dimexpr #1\relax][]}%
    % \fi
    %
    \expandafter\adjustbox\expandafter{\setpadd,
        %margin=0pt,padding=0pt,
    %padding={0pt {\dimexpr (0.4\textheight-\height)/2\relax} {0pt} {\dimexpr (0.4\textheight-\height)/2\relax}},
        fbox=1pt 0pt 0pt,
        valign=M
    }%
    {%
        \parbox{\dimexpr #2-2pt\relax}{%
            \BODY
        }%
    }%
}

\NewEnviron{colT}[2][]{%
    \def\setpadd{}%
    \if\relax #1\relax
    \else
        \def\setpadd{%
            set depth={\dimexpr (#1)-\height\relax}%
        }%
    \fi
    %
    \expandafter\adjustbox\expandafter{\setpadd,
        fbox=1pt 0pt 0pt,
        valign=T
    }%
    {%
        \parbox{\dimexpr #2-2pt\relax}{%
            \BODY
        }%
    }%
}

\makeatother

\newlength\atleastlength


\newenvironment{noindentlist}{
    \begin{list}{\textbullet}{
        \leftmargin=0pt\relax
        \itemindent=0pt\relax
        \setlength{\itemsep}{2pt}
    }
}{
    \end{list}
}

\NewEnviron{demobox}{
    \begin{adjustbox}{frame=1pt 10pt}%
        \begin{minipage}{\textwidth-22pt}
            \BODY
        \end{minipage}
    \end{adjustbox}
}

\newenvironment{codebox}{
    \begin{adjustbox}{frame=1pt 5pt,valign=t}%
        \begin{minipage}{\dimexpr\textwidth - 12pt\relax}%
}{
        \end{minipage}
    \end{adjustbox}
}


\ifbool{darktheme}{
    \definecolor{codehighlight}{RGB}{44, 78, 120}
}{
    \definecolor{codehighlight}{RGB}{177, 213, 255}
}

\def\codeEmphasize#1{\ul{#1}}


\def\customhead#1#2{%
    \ifnum\value{section}=#1\relax
        \textcolor{fg!100!bg}{%
            \setul{1pt}{0.5pt}\setulcolor{navigationUnderlineColor}\ul{#2}%
        }%
    \else
        #2%
    \fi
}

\let\placetarget\relax


% \setbeamertemplate{headline}
% {%
%     \placetarget
%     \let\placetarget\relax
%     \begin{beamercolorbox}[colsep=1.5pt]{upper separation line head}%
%     \end{beamercolorbox}
%     \begin{beamercolorbox}{section in head/foot}
%         \vskip2pt
%         \usebeamerfont{section in head/foot}\usebeamercolor[fg]{section in head/foot}%
%         \color{fg!80!bg}
%         \adjustbox{
%             set depth=2pt,
%             padding={5pt 5pt 5pt 5pt},
%             % rndframe={color=navigationTileOutline,width=1pt}{3pt 0pt 3pt 0pt},margin={10pt 0pt 0pt 0pt}
%             rndframe={color=navigationTileOutline,width=1pt}{3pt 3pt 3pt 3pt},margin={10pt 0pt 0pt 0pt}
%         }{%
%             \hyperlink{recap}{\customhead{1}{Recap}}%
%             % \hspace{5pt}$ \cdot $\hspace{5pt}%
%             % \hyperlink{referenties}{\customhead{3}{Referenties}}%
%             % \hspace{5pt}$ \cdot $\hspace{5pt}%
%             % \hyperlink{theorem}{\customhead{4}{`Theorem'}}%
%         }%
%         % \adjustbox{
%         %     set depth=2pt,
%         %     padding={5pt 5pt 5pt 5pt},
%         %     bgcolor=bg!80!orange,
%         %     rndframe={color=red!20!orange}{0pt 3pt 0pt 3pt},
%         %     margin={0pt 0pt 10pt 0pt}
%         % }{%
%         %     \hyperlink{oefeningen1}{\customhead{5}{Oefeningen}}%
%         % }%
%         \adjustbox{
%             set depth=2pt,
%             padding={5pt 5pt 5pt 5pt},
%             %rndframe={color=navigationTileOutline,width=1pt}{3pt 0pt 3pt 0pt},margin={10pt 0pt 0pt 0pt}
%             rndframe={color=navigationTileOutline,width=1pt}{3pt 3pt 3pt 3pt},margin={10pt 0pt 0pt 0pt}
%         }{%
%             \hyperlink{bulk}{\customhead{2}{Bulk}}%
%             % \hspace{5pt}$ \cdot $\hspace{5pt}%
%             % \hyperlink{snippets}{\customhead{4}{Snippets}}%
%             % \hspace{5pt}$ \cdot $\hspace{5pt}%
%             % \hyperlink{theorem}{\customhead{4}{`Theorem'}}%
%         }%
%         \adjustbox{
%             set depth=2pt,
%             padding={5pt 5pt 5pt 5pt},
%             % rndframe={color=navigationTileOutline,width=1pt}{3pt 0pt 3pt 0pt},margin={10pt 0pt 0pt 0pt}
%             rndframe={color=navigationTileOutline,width=1pt}{3pt 3pt 3pt 3pt},margin={10pt 0pt 0pt 0pt}
%         }{%
%             \hyperlink{hybrid}{\customhead{3}{Hybrid}}%
%         }%
%         %\PackageWarning{debug}{\meaning}
%         % \adjustbox{
%         %     set depth=2pt,
%         %     padding={5pt 5pt 5pt 5pt},
%         %     %bgcolor=bg!80!orange,
%         %     %bgcolor=structure.bg!80!orange,
%         %     bgcolor=exerciseTileBackground,
%         %     rndframe={color=exerciseTileOutline,width=1pt}{0pt 3pt 0pt 3pt},
%         %     margin={-1pt 0pt 10pt 0pt}
%         % }{%
%         %     \hyperlink{oefeningen1}{\customhead{5}{Oefeningen}}%
%         % }%
%         % \adjustbox{
%         %     set depth=2pt,
%         %     padding={5pt 5pt 5pt 5pt},
%         %     rndframe={color=navigationTileOutline,width=1pt}{3pt 0pt 3pt 0pt},margin={10pt 0pt 0pt 0pt}
%         %     % rndcorners={3pt 0pt 3pt 0pt}
%         % }{%
%         %     \hyperlink{LaTeXDefinities}{\customhead{6}{\LaTeX-definities}}%
%         %     \hspace{5pt}$ \cdot $\hspace{5pt}%
%         %     \hyperlink{apartePreamble}{\customhead{7}{Aparte preamble}}%
%         %     % \hspace{5pt}$ \cdot $\hspace{5pt}%
%         %     % \hyperlink{tabellen}{\customhead{8}{Tabellen}}%
%         % }%
%         % \adjustbox{
%         %     set depth=2pt,    
%         %     padding={5pt 5pt 5pt 5pt},
%         %     bgcolor=exerciseTileBackground,
%         %     rndframe={color=exerciseTileOutline,width=1pt}{0pt 3pt 0pt 3pt},
%         %     margin={-1pt 0pt 10pt 0pt}
%         % }{%
%         % \hyperlink{oefeningen2}{\customhead{8}{Oefeningen}}%
%         % }
%         \hfil
%         %Slides op texnicie.nl
%         % Slides op ...
%         \hfil
%         \vskip4pt
%     \end{beamercolorbox}%
%     \begin{beamercolorbox}[colsep=1.5pt]{lower separation line head}%
%     \end{beamercolorbox}%
% }

%\newmintinline[cc]{latex}{}

\ifbool{darktheme}{
    \usemintedstyle{dracula}
    \def\codeEmphasize#1{\textcolor{white}{\ul{#1}}}
}{
    \usemintedstyle{pastie}
}

\setminted{highlightcolor=codehighlight}


\setminted{fontsize=\small, autogobble=true, linenos=false, frame=none}
\usepackage{tcolorbox}


