%%% Partnership for Advanced Computing in Europe 
%%%   www.prace-ri.eu
%%%
%%% LaTeX document class for a PRACE Project Access Applications
%%%
%%% (c) PRACE aisbl - Partnership for Advanced Computing in Europe
%%%     authors: Dirk Brömmel - Forschungszentrum Jülich GmbH
%%%              Florian Berberich - PRACE aisbl
%%%
%%% Requires: LaTeX2e
%%%           geometry, helvet, xcolor, siunitx, graphicx, ifthen, colortbl,
%%%           lastpage, booktabs, fancyhdr, titlesec, verbatim
%%%
\NeedsTeXFormat{LaTeX2e}
%%% UPDATE THE VERSION STRING TWICE
\ProvidesClass{prace}[2021/08/06 PRACE Project Access Application class]
\newcommand{\version}{V1.1: 06.08.2021}

\DeclareOption{hints}{\newcommand{\showhints}{true}}
\DeclareOption{onecolumn}{\OptionNotUsed}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax

% Default font-size for main body text of the current template is 10pt
\LoadClass[a4paper,11pt]{article}

% A4: 210x297mm - 25mm margins top, left, right, 20mm on bottom
\RequirePackage{geometry}
\geometry{a4paper,
   left=18mm, top=45mm, right=18mm, bottom=27mm,
   footskip=8mm
}

% Select a font close to Arial (this will depend on the TeX interpreter, more to come)
% LaTeX
\RequirePackage{helvet}
\renewcommand{\familydefault}{\sfdefault}
% XeLaTeX
%%\usepackage{fontspec}
%%\setmainfont{Arial}

% Define PRACE colours
\RequirePackage{xcolor}
\definecolor{prace-dark-blue}{RGB}{ 25,  49, 123}
\definecolor{prace-light-blue}{RGB}{132, 206, 239}
\definecolor{prace-orange}{RGB}{255, 107,  25}
\definecolor{prace-table}{RGB}{218,229,239}
% Default text colour
\color{prace-dark-blue}

% Support packages
\RequirePackage[binary-units]{siunitx}
\RequirePackage{graphicx}
\RequirePackage{ifthen}
\RequirePackage{colortbl}
\RequirePackage{lastpage}
\RequirePackage{booktabs}

% Spacing between paragraphs and indents
\setlength{\parskip}{0.5ex plus 0.5ex minus 0.2ex}
\setlength{\parindent}{0pt}

% Page numbering
\pagenumbering{arabic}

% Set default length for picture placement
\setlength{\unitlength}{1cm}

% Headers an footers setup
\RequirePackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\renewcommand{\headrulewidth}{0pt}
% Old-school picture environment to more easily place the PRACE graphics
\setlength{\headheight}{14pt}
\fancyhead[C]{%
  \begin{picture}(0,0)%
    \put(-10.5, 0.7){\includegraphics[width=\paperwidth]{prace-header.jpg}}%
  \end{picture}%
}
\fancyfoot[C]{%
  \footnotesize
  \color{prace-dark-blue}{\textbf{PRACE} $|$ \scriptsize Application form for Project Access}
}
\fancyfoot[L]{\scriptsize\color{prace-dark-blue}Page \thepage\/ of \pageref{endofPSP}}
\renewcommand{\footrulewidth}{2pt}
\renewcommand{\footrule}{%
  \vskip-\footruleskip\vskip-\footrulewidth%
  {\color{prace-orange}\rule{\linewidth}{\footrulewidth}\vskip\footruleskip}%
}

% Define 'project' box
\newcommand{\project}[2]{
  {\large\vspace*{1cm}
\renewcommand{\arraystretch}{1.5}
\begin{tabular}{|p{.25\linewidth}|p{.695\linewidth}|}
  \hline
  \rowcolor{prace-table}\textbf{Project name} & #1\\
  \hline
  \rowcolor{prace-table}Research field & #2\\
  \hline
\end{tabular}
}}

% (Co)PI commands -- this is not nice and should better be added as titlepage
\newcommand{\PI}[7]{%
{\large\vspace*{1cm}
\renewcommand{\arraystretch}{1.5}
\textbf{Principal Investigator (PI)}\\
\begin{tabular}{|p{.25\linewidth}|p{.695\linewidth}|}
  \hline
  \rowcolor{prace-table}Title (Dr., Prof., etc.) & #1\\
  \hline
  \rowcolor{prace-table}Last name & #2\\
  \hline
  \rowcolor{prace-table}First name & #3\\
  \hline
  \rowcolor{prace-table}Organisation name & #4\\
  \hline
  \rowcolor{prace-table}Department & #5\\
  \hline
  \rowcolor{prace-table}Group & #6\\
  \hline
  \rowcolor{prace-table}Country & #7\\
  \hline
\end{tabular}
}
}
\newcommand{\CoPI}[7]{%
{\large\vspace*{1cm}
\renewcommand{\arraystretch}{1.5}
\textbf{Co-Principal Investigator (Co-PI)}\\
\begin{tabular}{|p{.25\linewidth}|p{.695\linewidth}|}
  \hline
  \rowcolor{prace-table}Title (Dr., Prof., etc.) & #1\\
  \hline
  \rowcolor{prace-table}Last name & #2\\
  \hline
  \rowcolor{prace-table}First name & #3\\
  \hline
  \rowcolor{prace-table}Organisation name & #4\\
  \hline
  \rowcolor{prace-table}Department & #5\\
  \hline
  \rowcolor{prace-table}Group & #6\\
  \hline
  \rowcolor{prace-table}Country & #7\\
  \hline
\end{tabular}
}
}

% Title block
\renewcommand{\maketitle}{%
  \newpage
  \noindent\begin{minipage}{3cm}
    \includegraphics[width=3cm]{prace-logo}
  \end{minipage}
  \begin{minipage}{10.5cm}
    \centering%
    Available on-line at www.prace-ri.eu\\\vspace{1em}
    {\large\bfseries Partnership for Advanced Computing in Europe}
  \end{minipage}
  \begin{minipage}{3cm}
  \end{minipage}\\
  \null
  \vskip 2em%
  \begin{center}%
    \let \footnote \thanks
    {\LARGE \@title \par}%
    \vskip 1.5em%
    {\large
    \lineskip .5em%
    \begin{tabular}[t]{c}%
      \authorlist
    \end{tabular}\par}%
    \vskip 1em%
    \affiliationblock
    \if@hascorresponding
    \authorcontact
    \else
    \@latex@warning{Corresponding author is undefined}
    \fi
  \end{center}%
  \par
  \vskip 1.5em%
  }

% Redefine section, figure etc. format
\setcounter{secnumdepth}{4}
\def\thesection       {\arabic{section}}
\def\thesubsection    {\thesection.\arabic{subsection}}
\def\thesubsubsection {\thesubsection.\arabic{subsubsection}}
\def\theparagraph     {\thesubsubsection.\arabic{paragraph}}
\def\figurename{Figure}
\def\tablename{Table}
% Section heading formats
\RequirePackage{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{}[\color{prace-orange}\hrule] % approx 14pt
\titleformat{\subsection}{\normalfont\Large}{\thesubsection}{1em}{}[\color{prace-orange}\hrule] % approx 14pt
\titleformat{\subsubsection}{\normalfont\Large\itshape}{\thesubsubsection}{1em}{}[\color{prace-orange}\hrule] % approx 14pt
\titleformat{\paragraph}{\normalfont\Large\itshape\color{prace-light-blue}}{\theparagraph}{1em}{}[] % approx 14pt

% Create environmet for hints that can be disabled
% The 'original' template had dotted underlined text here. This can be done, but would be hackish.
% Apparently typesetters have to have their arms twisted to do that for paragraphs, so I leave it.
\RequirePackage{verbatim}
% \newcommand{\showhints}{true} % this is a class option
\newenvironment{hint}%
{%
  \small\itshape
  \ifthenelse{\isundefined{\showhints}}%
  {\expandafter\comment}%
  {}%
}%
{%
  \normalsize\upshape
  \ifthenelse{\isundefined{\showhints}}%
  {\expandafter\endcomment}%
  {}%
}
\DeclareRobustCommand{\titlehint}[1]{%
  \ifthenelse{\isundefined{\showhints}}%
  {}%
  {(#1)}%
}

% Create environment for the 'titlepage'
% This is a workaround to use the commands to create the boxes
\newenvironment{cover}%
{%
   \fancyfoot[L]{}
   \fancyfoot[R]{\scriptsize\color{prace-dark-blue}{\version}}
   {\huge\textbf{Project scope and plan}}\\[-1ex]
   {\color{prace-orange}\hrule}
}%
{%
   \clearpage
   \newpage
   \setcounter{page}{1}
}

% Ref.number in References
\def\@biblabel#1{\indent[#1]}
% and changed formatting of heading
\renewenvironment{thebibliography}[1]
  {\section{\refname\/ \titlehint{maximum 30}}% <- changed to match style of subsection
   \@mkboth{\MakeUppercase\refname}{\MakeUppercase\refname}%
   \list{\@biblabel{\@arabic\c@enumiv}}%
     {\settowidth\labelwidth{\@biblabel{#1}}%
      \leftmargin\labelwidth
      \advance\leftmargin\labelsep
      \@openbib@code
      \usecounter{enumiv}%
      \let\p@enumiv\@empty
      \renewcommand\theenumiv{\@arabic\c@enumiv}}%
   \sloppy
   \clubpenalty4000
   \@clubpenalty \clubpenalty
   \widowpenalty4000%
   \sfcode`\.\@m}
  {\def\@noitemerr
    {\@latex@warning{Empty `thebibliography' environment}}%
  \endlist}

% Change layout of the appendix
\renewcommand\appendix{\par
   \label{endofPSP}
   \clearpage
   \newpage
   \setcounter{secnumdepth}{1}%
   \setcounter{page}{1}%
   \setcounter{section}{0}%
   \setcounter{subsection}{0}%
   \fancyfoot[L]{\scriptsize\color{prace-dark-blue}Page \thepage\/ of \pageref{LastPage}}%
   \gdef\thesection{Appendix \@arabic\c@section:}}

% Add space either after table caption or before figure caption
\newif\if@withintable
\@withintablefalse
\newif\if@withinfigure
\@withinfigurefalse
\renewenvironment{figure}
  {\@withinfiguretrue\@float{figure}}
  {\end@float\@withinfigurefalse}
\renewenvironment{figure*}
  {\@withinfiguretrue\@dblfloat{figure}}
  {\end@dblfloat\@withinfigurefalse}
\renewenvironment{table}
  {\@withintabletrue\@float{table}}
  {\end@float\@withintablefalse}
\renewenvironment{table*}
  {\@withintabletrue\@dblfloat{table}}
  {\end@dblfloat\@withintablefalse}
\setlength\abovecaptionskip{10\p@}
\setlength\belowcaptionskip{10\p@}
\long\def\@makecaption#1#2{%
  \if@withinfigure
    \vskip\abovecaptionskip
  \fi
  {\small #1. #2\par}
  \if@withintable
    \vskip\belowcaptionskip
  \fi}

% Check if we are using pdfTeX>1.40, if so enable microtype.
% Why v1.40 and above: it might not work with any version and more importantly
% increase filesize of the pdf if <1.40 and we impose size constraints.
\def\@pdfproducer{pdfTeX}%
\ifx\eTeXversion\@undefined
\else
  \ifx\eTeXversion\relax
  \else
    \ifnum\eTeXversion>0 %
      \def\@pdfproducer{pdfeTeX}%
    \fi
  \fi
\fi
\ifx\pdftexversion\@undefined
\else
  \ifnum\pdftexversion<100 %
    \edef\@pdfproducer{%
      \@pdfproducer
      \the\pdftexversion.\pdftexrevision
    }%
  \else
    \ifnum\pdftexversion<130 %
      \edef\@pdfproducer{%
        \@pdfproducer-%
        \expandafter\@car\the\pdftexversion\@empty\@nil.%
        \expandafter\@cdr\the\pdftexversion\@empty\@nil
        \pdftexrevision
      }%
    \else
      \ifnum\pdftexversion<140 %
      \else
        \def\@pdfproducer{pdfTeX}%
        \RequirePackage{microtype}
      \fi
      \edef\@pdfproducer{%
        \@pdfproducer-%
        \expandafter\@car\the\pdftexversion\@empty\@nil.%
        \expandafter\@cdr\the\pdftexversion\@empty\@nil.%
        \pdftexrevision
      }%
    \fi
  \fi
\fi
