% Equations
\NewEnvironmentCopy{oldequation}{equation}
\RenewDocumentEnvironment{equation}{m}{%
  \begin{oldequation}%
    \label{\labelprefix:eq:#1}%
  }{%
  \end{oldequation}%
}

% Contents, figures and tables
\NewDocumentEnvironment{listof}{}{
  \hypersetup{linkcolor=black}
  \singlespacing
}{}

% Floatings (images and tables) : less restrictive constraints (https://robjhyndman.com/hyndsight/latex-floats/index.html)
\setcounter{topnumber}{2}
\setcounter{bottomnumber}{2}
\setcounter{totalnumber}{4}
\renewcommand{\topfraction}{0.85}
\renewcommand{\bottomfraction}{0.85}
\renewcommand{\textfraction}{0.15}
\renewcommand{\floatpagefraction}{0.7}

% Images
% example: \fig[1]{filename}{Caption}[Additional caption text, not shown in the list of figures]
% reference later with: \figref{filename}
% 1 = \linewidth = 150 mm
\RenewDocumentCommand{\fig}{O{1} O{htbp} m m O{}}{% #1 = width, #2 = position, #3 = filename, #4 = caption, #5 = additional caption
  \begin{figure}[#2]%
    \centering%
    \includegraphics[width=#1\linewidth]{#3}%
    \caption[#4]{#4#5}
    \label{fig:#3}%
  \end{figure}%
}

\RenewDocumentCommand{\figwide}{O{1} m m O{}}{% #1 = width, #2 = filename, #3 = caption, #4 = additional caption
  \afterpage{%
    \begin{landscape}%
      \fig[#1][p]{#2}{#3}[#4]%
    \end{landscape}%
  }%
}

% example:
% \begin{subfigs}{label}{Caption}[Additional caption text, not shown in the list of figures][
%   \item Subfig title 1.
%   \item Subfig title 2.
%   ]
%   \subfig{filename1}%
%   \subfig[1][htbp]{filename2}%
% \end{subfigs}
% reference later with: \figref{label}
\RenewDocumentEnvironment{subfigs}{O{htbp} m m O{} o}{% #1 = position, #2 = label, #3 = caption, #4 = additional caption, #5 = subfig titles
  \begin{figure}[#1]%
    \centering%
  }{%
    \caption[#3]{%
      #3#4%
      \IfValueTF{#5}{%
        \begin{enumerate*}[label=\textbf{(\alph*)}]%
          #5%
        \end{enumerate*}%
      }%
    }%
    \label{fig:#2}%
  \end{figure}%
}

\NewDocumentCommand{\subfigsheight}{m}{% Set the height of the subfigures
  \setkeys{Gin}{height=#1}%
}

\RenewDocumentCommand{\subfig}{o O{b} m}{% #1 = width, #2 = valign, #3 = filename
  \hfill%
  \subfloat[\label{fig:#3}]{%
    \IfValueTF{#1}{%
      \includegraphics[width=#1\linewidth, valign=#2, keepaspectratio]{#3}%
    }{%
      \includegraphics[valign=#2, keepaspectratio]{#3}%
    }%
  }%
  \hfill%
}

\NewDocumentCommand{\subfigbox}{o O{b} m}{% #1 = width, #2 = valign, #3 = filename
  \hfill%
  \subfloat[\label{fig:#3}]{%
    \IfValueTF{#1}{%
      \includegraphics[width=#1\linewidth, valign=#2, keepaspectratio, frame]{#3}%
    }{%
      \includegraphics[valign=#2, keepaspectratio, frame]{#3}%
    }%
  }%
  \hfill%
}

\captionsetup{%
  labelfont={sf,bf,up}, % sans-serif, bold, upright
}

% Labels and references
% Add chapter label as prefix to all other sub-labels
\NewCommandCopy{\oldlabel}{\label}
\newcommand{\labelprefix}{main}
\newcommand{\mainlabel}[1]{%
  \renewcommand{\labelprefix}{#1}%
  \oldlabel{\labelprefix}%
}
\renewcommand{\label}[1]{\oldlabel{\labelprefix:#1}}

\newcommand{\chapref}[1]{Chapter~\ref{#1}}
\renewcommand{\eqref}[2][\labelprefix]{Equation~\ref{#1:eq:#2}}
\renewcommand{\figref}[2][\labelprefix]{Figure~\ref{#1:fig:#2}}
\newcommand{\partref}[1]{Part~\ref{#1}}
\renewcommand{\secref}[2][\labelprefix]{Section~\ref{#1:#2}}
\renewcommand{\tabref}[2][\labelprefix]{Table~\ref{#1:tab:#2}}

% Misc
\newcommand{\noindentskip}{\bigskip\noindent} % Skip a line and remove the indent

% Tables
\RenewDocumentEnvironment{tab}{O{htbp} m m O{}}{% #1 = position, #2 = label, #3 = title, #4 = additional caption
  \begin{table}[#1]%
    \centering%
    \caption[#3]{#3#4}%
    \label{tab:#2}%
  }{%
  \end{table}%
}

\RenewDocumentEnvironment{tabwide}{m m O{}}{% #1 = label, #2 = title, #3 = additional caption
  \begin{landscape}
    \begin{tab}[p]{#1}{#2}[#3]%
    }{%
    \end{tab}%
  \end{landscape}%
}

% Local table of contents
\usepackage{titletoc}
\newcommand{\chaptertoc}{% Print the table of contents for the chapter
  %\hypersetup{linkcolor=black}%

  \section*{Contents}% Add a section title
  \vspace{-1.5em}%
  \horizontalrule%

  \startcontents% Start referencing the contents
  \renewcommand{\cftsecfont}{\bfseries}% Bold sections in local ToC
  \printcontents{}{1}{}% Print the contents

  \vspace{-0.5em}%
  \horizontalrule%
  \vspace{2em}%
}
