%! TeX program = lualatex
% (for vimtex)
\documentclass[11pt, a4paper]{article}
% \usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
% ENVIRONMENTS etc
\usepackage{adjustbox}
\usepackage{colortbl}   % color table
\usepackage{tabularx}   % bravais table
\usepackage{multirow}   % for superconducting qubit table
\usepackage{hhline}     % for superconducting qubit table
% TOOLING
\usepackage{graphicx}
\usepackage{etoolbox}
% \usepackage{luacode}
\usepackage{expl3}      % switch case and other stuff
\usepackage{substr}
\usepackage{xcolor}
% FORMATING
\usepackage{float}                  % float barrier
\usepackage{subcaption}             % subfigures
\usepackage[hidelinks]{hyperref}    % hyperrefs for \fqEqRef, \qtyRef, etc
\usepackage[shortlabels]{enumitem}  % easily change enum symbols to i), a. etc
\usepackage{titlesec}   % colored titles
\usepackage{array}      % more array options
\newcolumntype{C}{>{$}c<{$}} % math-mode version of "c" column type
% \usepackage{sectsty}
% TRANSLATION
\usepackage{translations}
\input{util/translation.tex}
\input{util/colorscheme.tex}
% GRAPHICS
\usepackage{tikz}       % drawings
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{calc}
% speed up compilation by externalizing figures
% \usetikzlibrary{external}
% \tikzexternalize[prefix=tikz_figures]
% \tikzexternalize
\usepackage{circuitikz} % electrical circuits with tikz
% SCIENCE PACKAGES
\usepackage{mathtools}
% set display math skips
\AtBeginDocument{
    \abovedisplayskip=0pt
    \abovedisplayshortskip=0pt
    \belowdisplayskip=0pt   
    \belowdisplayshortskip=0pt
}
\usepackage{MnSymbol}   % for >>> \ggg sign
\usepackage[version=4,arrows=pgf-filled]{mhchem}
\usepackage{upgreek}    % upright greek letters for chemmacros
\usepackage{chemmacros} % for orbitals images
% \usepackage{esdiff} % derivatives
% esdiff breaks when taking \dot{q} has argument
\usepackage{derivative} % \odv, \pdv
\usepackage{bbold}      % \mathbb font
\usepackage{braket}     % <bra|ket>
\usepackage{siunitx}    % \si \SI units
\sisetup{output-decimal-marker = {,}}
\sisetup{separate-uncertainty}
\sisetup{per-mode = power}
\sisetup{exponent-product=\ensuremath{\cdot}}
% DEBUG
% \usepackage{lua-visual-debug}
% DUMB STUFF
% \usepackage{emoji}
% \newcommand\temoji[1]{\text{\emoji{#1}}}
% \def\sigma{\temoji{shark}}
% \def\lambda{\temoji{sheep}}
% \def\psi{\temoji{pickup-truck}}
% \def\pi{\temoji{birthday-cake}}
% \def\Pi{\temoji{hospital}}
% \def\rho{\temoji{rhino}}
% \def\nu{\temoji{unicorn}}
% \def\mu{\temoji{mouse}}



\newcommand{\TODO}[1]{{\color{bright_red}TODO:#1}}
\newcommand{\ts}{\textsuperscript}

% "automate" sectioning
% start <section>, get heading from translation, set label
% fqname is the fully qualified name: the keys of all previous sections joined with a ':'
% [1]: code to run after setting \fqname, but before the \part, \section etc
% 2: key
\newcommand{\Part}[2][desc]{
    \newpage
    \def\partName{#2}
    \def\sectionName{}
    \def\subsectionName{}
    \def\subsubsectionName{}
    \edef\fqname{\partName}
    #1
    \edef\fqnameText{\expandafter\GetTranslation\expandafter{\fqname}}
    \part{\fqnameText} 
    \label{sec:\fqname}
}
\newcommand{\Section}[2][]{
    \def\sectionName{#2}
    \def\subsectionName{}
    \def\subsubsectionName{}
    \edef\fqname{\partName:\sectionName}
    #1
    % this is necessary so that \section takes the fully expanded string. Otherwise the pdf toc will have just the fqname
    \edef\fqnameText{\expandafter\GetTranslation\expandafter{\fqname}}
    \section{\fqnameText}
    \label{sec:\fqname}
}
% \newcommand{\Subsection}[1]{\Subsection{#1}{}}
\newcommand{\Subsection}[2][]{
    \def\subsectionName{#2}
    \def\subsubsectionName{}
    \edef\fqname{\partName:\sectionName:\subsectionName}
    #1
    \edef\fqnameText{\expandafter\GetTranslation\expandafter{\fqname}}
    \subsection{\fqnameText}
    \label{sec:\fqname}
}
\newcommand{\Subsubsection}[2][]{
    \def\subsubsectionName{#2}
    \edef\fqname{\partName:\sectionName:\subsectionName:\subsubsectionName}
    #1
    \edef\fqnameText{\expandafter\GetTranslation\expandafter{\fqname}}
    \subsubsection{\fqnameText}
    \label{sec:\fqname}
}
\edef\fqname{NULL}

\newcommand\luaDoubleFieldValue[3]{%
    \directlua{
        if #1 \string~= nil and #1[#2] \string~= nil and #1[#2][#3] \string~= nil then
            tex.sprint(#1[#2][#3])
            return
        end
        luatexbase.module_warning('luaDoubleFieldValue', 'Invalid indices to `#1`: `#2` and `#3`');
        tex.sprint("???")
    }%
}
% REFERENCES
% All xyzRef commands link to the key using the translated name
% Uppercase (XyzRef) commands have different link texts, but the same link target
% 1: key/fully qualified name (without qty/eq/sec/const/el... prefix)
% Equations/Formulas
% <name>
\newrobustcmd{\fqEqRef}[1]{%
    % \edef\fqeqrefname{\GT{#1}}
    % \hyperref[eq:#1]{\fqeqrefname}
    \hyperref[f:#1]{\GT{#1}}%
}
% Section
% <name>
\newrobustcmd{\fqSecRef}[1]{%
    \hyperref[sec:#1]{\GT{#1}}%
}
% Quantities
% <symbol>
\newrobustcmd{\qtyRef}[1]{%
    \edef\tempname{\luaDoubleFieldValue{quantities}{"#1"}{"fqname"}}%
    \hyperref[qty:#1]{\expandafter\GT\expandafter{\tempname:#1}}%
}
% <symbol> <name>
\newrobustcmd{\QtyRef}[1]{%
    $\luaDoubleFieldValue{quantities}{"#1"}{"symbol"}$ \qtyRef{#1}%
}
% Constants
% <name>
\newrobustcmd{\constRef}[1]{%
    \edef\tempname{\luaDoubleFieldValue{constants}{"#1"}{"fqname"}}%
    \hyperref[const:#1]{\expandafter\GT\expandafter{\tempname:#1}}%
}
% <symbol> <name>
\newrobustcmd{\ConstRef}[1]{%
    $\luaDoubleFieldValue{constants}{"#1"}{"symbol"}$ \constRef{#1}%
}
% Element from periodic table
% <symbol>
\newrobustcmd{\elRef}[1]{%
    \hyperref[el:#1]{{\color{dark0_hard}#1}}%
}
% <name>
\newrobustcmd{\ElRef}[1]{%
    \hyperref[el:#1]{\GT{el:#1}}%
}

% \usepackage{xstring}

% LUA sutff
\newcommand\luavar[1]{\directlua{tex.sprint(#1)}}
\directlua{
    function string.startswith(s, start)
        return string.sub(s,1,string.len(start)) == start
    end
}
% Write directlua command to aux and run it as well
% This one expands the argument in the aux file:
\newcommand\directLuaAuxExpand[1]{
    \immediate\write\luaauxfile{\noexpand\directlua{#1}}
    \directlua{#1}
}
% This one does not:
\newcommand\directLuaAux[1]{
    \immediate\write\luaauxfile{\noexpand\directlua{\detokenize{#1}}}
    \directlua{#1}
}
% read
\IfFileExists{\jobname.lua.aux}{%
  \input{\jobname.lua.aux}%
}{%
    % \@latex@warning@no@line{"Lua aux not loaded!"}
}
\def\luaAuxLoaded{False}
% write
\newwrite\luaauxfile
\immediate\openout\luaauxfile=\jobname.lua.aux
\immediate\write\luaauxfile{\noexpand\def\noexpand\luaAuxLoaded{True}}%
\AtEndDocument{\immediate\closeout\luaauxfile}

\input{circuit.tex}
\input{util/macros.tex}
\input{util/environments.tex}   % requires util/translation.tex to be loaded first
\input{util/periodic_table.tex} % requires util/translation.tex to be loaded first



% INPUT
% 1: starting pattern of files to input using the Input command. All other files are ignored 
\newcommand\InputOnly[1]{\edef\inputOnlyFile{#1}}
\edef\inputOnlyFile{all}
\newcommand\Input[1]{
    % yes this could surely be done in tex
    \directlua{
        if '\luaescapestring{\inputOnlyFile}' == 'all' or string.startswith('\luaescapestring{#1}', '\luaescapestring{\inputOnlyFile}') then
            tex.print("\\input{\luaescapestring{#1}}")
        end
    }
}


\title{Formelsammlung}
\author{Matthias Quintern}
\date{\today}
\begin{document}
\IfFileExists{\jobname.translations.aux}{%
  \input{\jobname.translations.aux}
}{}

\def\translationsAuxLoaded{False}
\newwrite\translationsaux
\immediate\openout\translationsaux=\jobname.translations.aux
\immediate\write\translationsaux{\noexpand\def\noexpand\translationsAuxLoaded{True}}%
\AtEndDocument{\immediate\closeout\translationsaux}

\makeatletter\let\percentchar\@percentchar\makeatother

\maketitle
\tableofcontents
\newpage
\setcounter{page}{1}

\input{util/translations.tex}

% \InputOnly{math}

\Input{math/math}
\Input{math/linalg}
\Input{math/geometry}
\Input{math/calculus}
\Input{math/probability_theory}

\Input{mechanics}
\Input{statistical_mechanics}

\Input{ed/ed}
\Input{ed/el}
\Input{ed/mag}
\Input{ed/em}

\Input{quantum_mechanics}
\Input{atom}

\Input{cm/cm}
\Input{cm/crystal}
\Input{cm/egas}
\Input{cm/charge_transport}
\Input{cm/low_temp}
\Input{cm/semiconductors}
\Input{cm/other}
\Input{cm/techniques}

\Input{topo}

\Input{quantum_computing}

\Input{computational}

\Input{quantities}
\Input{constants}

\Input{ch/periodic_table}  % only definitions
\Input{ch/ch}


% \newpage
% \Input{test}

\newpage
\Part[
    \eng{Appendix}
    \ger{Anhang}
]{appendix}
% \listofmyenv
% \listofquantities
\listoffigures
\listoftables
\Section[
    \eng{List of elements}
    \ger{Liste der Elemente}
]{elements}
    \printAllElements

% \bibliographystyle{plain}
% \bibliography{ref}

\end{document}
