\DeclareSIUnit{\baud}{Bd}
\DeclareSIUnit{\rpm}{rpm}

\DeclarePairedDelimiter{\ceil}{\lceil}{\rceil}
\DeclarePairedDelimiter{\paren}{(}{)}

\usepackage[
    backend=biber,
    style=numeric,
    natbib=true,
    url=false, 
    doi=true,
    eprint=false,
    refsegment=chapter,
    ]{biblatex}
\addbibresource{main.bib}
\DeclareSourcemap{
    \maps[datatype=bibtex]{
        \map{
            \step[fieldsource=doi,final]
            \step[fieldset=isbn,null]
            \step[fieldset=issn,null]
            \step[fieldset=url,null]
        }
        \map{
            \step[fieldsource=isbn,final]
            \step[fieldset=issn,null]
            \step[fieldset=url,null]
        }
    }
}

\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}}

% Re-define heading formats to force single line spacing
%\titleformat{\section}{\normalfont\large\bfseries\singlespacing}{\thesection}{1em}{}
%\titleformat{\subsection}{\normalfont\large\bfseries\singlespacing}{\thesubsection}{1em}{}
%\titleformat{\subsubsection}{\normalfont\large\bfseries\singlespacing}{\thesubsubsection}{1em}{}

\dominitoc

\newcommand{\degree}{\ensuremath{^\circ}}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\definecolor{todoboxcolor}{RGB}{251 224 252}

\pagestyle{fancy}

\fancyhead[C]{}
\fancyhead[ER]{\footnotesize%
    \ifdefined\thesispreviewmode %
        (draft \texttt{\input{version.tex}\unskip}) %
    \fi %
\leftmark}
\fancyhead[OL]{\footnotesize\rightmark}
\fancyhead[EL,OR]{\thepage}

\fancyfoot[LCR]{}

\fancypagestyle{plain}{%
    \fancyhf{}%
    \renewcommand{\headrulewidth}{0pt}%
    \renewcommand{\footrulewidth}{0pt}%
}

\raggedbottom

\renewcommand{\chaptermark}[1]{\markboth{Chapter \thechapter: #1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
\addtolength{\headwidth}{-1cm}

\newcommand{\chapterquote}[2]{\def\chapterquoteattribution{#1}\def\chapterquotecontent{#2}}
\newcommand{\printchapterquote}{
    \ifdefined\chapterquoteattribution
        \begin{quote}
            \chapterquotecontent
            \begin{flushright}
                \textit{-- \chapterquoteattribution}
            \end{flushright}
        \end{quote}
        \let\chapterquotecontent\undefined
        \let\chapterquoteattribution\undefined
    \fi
}

\newcommand{\todo}[1]{
    \ifdefined\thesispreviewmode
    \noindent%
    \marginpar{
        \setlength{\fboxsep}{2mm}
        \shadowbox{
            \parbox{3cm}{
                \singlespacing
                \raggedright
                \textsf{
                    \small\textbf{To do}\\
                    \footnotesize#1
                }
            }
        }
    }
    \fi
}

\newcommand{\sourceattrib}[1]{%
    \noindent%
    \marginpar{%
        \raggedleft%
        \footnotesize%
        \textit{#1}%
    }%
    \ignorespaces%
}

\newcommand{\todoplaceholder}[1]{\textbf{TODO}\todo{#1}}

% https://tex.stackexchange.com/questions/30720/footnote-without-a-marker
\newcommand\blfootnote[1]{%
  \begingroup
  \renewcommand\thefootnote{}\footnote{#1}%
  \addtocounter{footnote}{-1}%
  \endgroup
}

\newcommand{\figureattrib}[1]{%
    \input{\figurepath/#1.latex_meta} %
    \scriptsize
        \ifdefined\thesispreviewmode\resourcestate\ \resourcescale\\\fi%
        Resource: %
        \texttt{\resourcerepo/\resourcepath} %
        rev \texttt{\resourcerev} %
        (\underline{\href{\resourceurl}{link}})%
}

\newcommand{\draftgraphics}{\ifdefined\thesispreviewmode\textcolor{red}{\bfseries Not final graphics. }\fi}
\newcommand{\camerareadygraphics}{\ifdefined\thesispreviewmode Camera-ready graphics. \fi}
\newcommand{\scaledgraphics}[1]{\ifdefined\thesispreviewmode scaled-#1\else#1\fi}
\newcommand{\partno}[1]{\textsf{\small#1}}
\newcommand{\price}[2]{#1 #2}

\newcommand{\imgsource}[4]{\scriptsize%
    Image source: #1, #2 (\underline{\href{#4}{link}}). %
    Licensed #3.}

\DeclareRefcontext{webref}{labelprefix=W}
\DeclareRefcontext{patref}{labelprefix=P}
\DeclareRefcontext{defref}{}
\DeclareFieldFormat{labelprefix}{\textsuperscript{\sffamily#1}}

\newcommand{\chapterbibliography}{
    \FloatBarrier
    \addcontentsline{toc}{section}{References}
    \newrefcontext{webref}
    \printbibliography[type={online},title={Web sources},heading=subbibliography,resetnumbers=false,segment=\therefsegment]
    \newrefcontext{patref}
    \printbibliography[type={patent},title={Patent References},heading=subbibliography,resetnumbers=false,segment=\therefsegment]
    \newrefcontext{defref}
    \printbibliography[nottype={online},nottype={patent},heading=subbibliography,resetnumbers=false,segment=\therefsegment]
}

\newrefcontext{defref}

\hyphenation{a-me-na-ble}
\hyphenation{da-ta-cen-ter}

\setstretch{1.3}
