%% Packages required
\usepackage[caption=false]{subfig} % Good looking subfigs

%% Abbreviations
\newcommand{\etal}{\textit{et al.}}

\newcommand{\eg}{\textit{e.g.},\ }
\newcommand{\ie}{\textit{i.e.},\ }

\newcommand{\ns}{\textit{n.s.}}

\newcommand{\x}{\texttimes\ }

\newcommand{\vs}{\textit{vs.}\ }

%% Figures
% example: \fig[1]{universe}{The Universe}[Short title for list of figures]
% reference later with: \figref{universe}
\NewDocumentCommand{\fig}{O{0.8} O{htbp} m m O{#4}}{% #1 = width, #2 = position, #3 = filename, #4 = caption, #5 = short caption
  \begin{figure}[#2]
    \centering%
    \includegraphics[width=#1\linewidth]{#3}%
    \caption[#5]{#4}%
    \label{fig:#3}%
  \end{figure}%
}

\NewDocumentCommand{\figwide}{O{1} O{htbp} m m O{#4}}{% #1 = width, #2 = position, #3 = filename, #4 = caption, #5 = short caption
  \begin{figure*}[#2]
    \centering%
    \includegraphics[width=#1\linewidth]{#3}%
    \caption[#5]{#4}%
    \label{fig:#3}%
  \end{figure*}%
}

%% Sub-figures
% example:
% \begin{subfigs}{label}{Title}[Short title for list of figures]
%   \subfig{subfig1}%
%   \subfig[1][htbp]{subfig2}[caption]%
% \end{subfigs}
% reference later with: \figref{label}
\NewDocumentEnvironment{subfigs}{O{htbp} m m O{#3}}{% #1 = position, #2 = label, #3 = title, #4 = short title
  \begin{figure}[#1]%
    \centering%
  }{%
    \caption[#4]{#3}%
    \label{fig:#2}%
  \end{figure}%
}

\NewDocumentEnvironment{subfigswide}{O{htbp} m m O{#3}}{% #1 = position, #2 = label, #3 = title, #4 = short title
  \begin{figure*}[#1]%
    \centering%
  }{%
    \caption[#4]{#3}%
    \label{fig:#2}%
  \end{figure*}%
}

\NewDocumentCommand{\subfig}{O{0.2} m o}{% #1 = width, #2 = filename, #3 = caption
  \IfValueTF{#3}{%
    \subfloat[#3\label{fig:#2}]{\includegraphics[width=#1\linewidth]{#2}}%
  }{%
    \subfloat{\includegraphics[width=#1\linewidth]{#2}}%
  }%
  \hfill%
}

\newcommand{\topleftsubcaption}{%
  \captionsetup[subfigure]{
    position=top,
    justification=raggedright,
    singlelinecheck=off, % do not center single line captions
    labelfont={scriptsize,sf,md,up}, % smallest size, sans-serif, bold, upright
    %labelformat=simple, % do not add brackets
    farskip=0pt, % no space between caption and figure
    captionskip=0pt, % no space between caption and subfigure
    margin=3pt % align caption with subfigure
  }%
}

\newcommand{\hidesubcaption}{%
  \captionsetup[subfigure]{
    position=top,
    justification=raggedright,
    labelfont={scriptsize}, % smallest size
    farskip=0pt, % no space between caption and figure
    captionskip=-10pt, % no space between caption and subfigure
    margin=4pt % align caption with subfigure
  }%
}

%% Footnotes
\newcommand{\footnotemarkrepeat}{\footnotemark[\value{footnote}]} % Repeat the last footnote mark
\newcommand{\footnoteurl}[1]{\footnote{\ \url{#1}}}

%% Lists
\renewcommand{\descriptionlabel}[1]{\hspace{\labelsep}\textit{#1}} % Use italics instead of bold for descriptions

%% Maths
\newcommand{\measure}[3]{$\mathrm{#1} = \qty{#2}{#3}$}

\newcommand{\chisqr}[3]{$\chi^2(#1, N=#2) = #3$}

\newcommand{\anova}[3]{$\mathrm{F(#1, #2)} = #3$}

\newcommand{\ci}[2]{$[#1, #2]$}

\newcommand{\friedman}[1]{$\chi^2 = #1$}

\newcommand{\geomean}[2]{\measure{GM}{#1}{#2}}

\newcommand{\iqr}[2]{\measure{IQR}{#1}{#2}}

\newcommand{\kendall}[1]{$\tau = #1$}

\newcommand{\kruskal}[1]{\measure{H}{#1}{}}

\newcommand{\mean}[2]{\measure{M}{#1}{#2}}

\newcommand{\median}[2]{\measure{Mdn}{#1}{#2}}

\newcommand{\p}[2][=]{$\mathrm{p} #1 #2$}
\newcommand{\pinf}[1]{\p[<]{#1}}

\newcommand{\percent}[1]{\qty{#1}{\%}}

\newcommand{\pearson}[1]{\measure{r}{#1}{}}

\newcommand{\sd}[1]{\measure{SD}{#1}{}}

\newcommand{\spearman}[1]{\measure{r_s}{#1}{}}

\newcommand{\tabCI}[2]{{[}#1{,}\,#2{]}}

\newcommand{\ttest}[2]{$\mathrm{t}(#1) = #2$}

\newcommand{\wilcoxon}[1]{\measure{W}{#1}{}}

\newcommand{\ztest}[1]{$\mathrm{Z} = #1$}

% From https://tex.stackexchange.com/a/95842
\newenvironment{conditions}{%
  \par% maybe you want the list to start with a new line
  \vspace{\abovedisplayskip}% some space before
  \noindent% don't indent
  \tabularx{\linewidth}{>{$}l<{$} @{${}={}$} X@{}}% three columns left-aligned; first one is math-mode, the middle is an equals sign, and the last one is normal
}{%
  \endtabularx%
  \par% maybe you want the list to end with a new line
  \vspace{\belowdisplayskip}% some space after
}

%% References
\renewcommand{\eqref}[1]{Eq.~\ref{eq:#1}}
\newcommand{\figref}[1]{Fig.~\ref{fig:#1}}
\newcommand{\secref}[1]{Sec.~\ref{sec:#1}}
\newcommand{\tabref}[1]{Table~\ref{tab:#1}}

%% Structure
\newcommand{\sectionstartoc}[1]{
\section*{#1}\addcontentsline{toc}{section}{#1}}
\newcommand{\sectionstarbookmark}[1]{\pdfbookmark[section]{#1}{#1}
\section*{#1}}

\newcommand{\subsectionstartoc}[1]{
\subsection*{#1}\addcontentsline{toc}{subsection}{#1}}
\newcommand{\subsectionstarbookmark}[1]{\pdfbookmark[subsection]{#1}{#1}
\subsection*{#1}}

\newcommand{\subsubsubsection}[1]{\paragraph*{\textit{#1}}}

%% Tables
% example:
% \begin{tab}{label}{Title}
%   \begin{tabular}{cc}
%     \textbf{1} & \textbf{2} \\
%     3 & 4 \\
%   \end{tabular}
% \end{tab}
% reference later with: \tabref{label}
\NewDocumentEnvironment{tab}{O{htbp} m m}{% #1 = position, #2 = label, #3 = title
  \begin{table}[#1]%
    \centering%
    \caption{#3}%
    \label{tab:#2}%
  }{%
  \end{table}%
}

\NewDocumentEnvironment{tabwide}{O{htbp} m m}{% #1 = position, #2 = label, #3 = title
  \begin{table*}[#1]%
    \centering%
    \caption{#3}%
    \label{tab:#2}%
  }{%
  \end{table*}%
}

\newcommand{\tabrow}[1]{#1 \\ \hline}

\newcolumntype{B}[1]{>{\raggedright}b{#1}}
\newcolumntype{C}[1]{>{\raggedright}c{#1}}
\newcolumntype{M}[1]{>{\raggedright}m{#1}}
\newcolumntype{P}[1]{>{\raggedright}p{#1}}

%% Other
\newcommand{\horizontalrule}{\par\noindent\rule{\textwidth}{0.4pt}} % Horizontal line

\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1} % Convert units. Usage: \convertto{cm}{1pt}. From: https://tex.stackexchange.com/a/8337
\makeatother

%% User studies
\newcommand{\factor}[1]{\textsc{#1}}
\newcommand{\level}[1]{\textsl{#1}}
\newcommand{\response}[1]{\textit{#1}}
