% \iffalse meta-comment
%
% Copyright 2025 Michael E. Hammer
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Michael E. Hammer.
%
% This file is part of the "fhj-script bundle" and all files in that
% bundle must be distributed together.
%
% \fi
% \iffalse
%<*driver>
\ProvidesFile{fhjpaper.dtx}
%</driver>
%<class>\NeedsTeXFormat{LaTeX2e}[2023-11-01]
%<class>\ProvidesClass{fhjpaper}
%<*class>
    [2026-02-25 v4.2c A class for formatting papers in the FHJ style]
%</class>
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription % We only want the description
\begin{document}
    \DocInput{fhjpaper.dtx}
\end{document}
%</driver>
% \fi
%
% \GetFileInfo{fhjpaper.dtx}
% \newcommand{\classname}{fhjpaper}
%
% \changes{v4.0}{2025/08/03}{Initial package version}
%
% \title{The \textsf{\classname} class\thanks{This document
% corresponds to \textsf{\classname}~\fileversion,
% dated \filedate.}}
% \author{Michael E. Hammer \\ \texttt{michael.hammer@fh-joanneum.at}}
%
% \maketitle
% 
% \begin{abstract}
% This class is used to format papers in the FH Joanneum style.
% It is based on the \textsf{scrartcl} class and provides a set of
% commands to define the title, author, and other information.
% It is mainly used to create the expose for a master thesis.
% \end{abstract}
%
% \tableofcontents
% 
% \section{Configuration and styling}
% 
% This class is based on the \textsf{scrartcl} class and we support forwarding 
% options to the base class (except the hardcoded options from below).
%
%    \begin{macrocode}
\newcommand{\@ClassName}{fhjpaper}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{scrartcl}}
\ProcessOptions\relax
%    \end{macrocode}
%
% We load the \textsf{scrartcl} class with the following options:
%
% \begin{itemize}
%     \item \texttt{a4paper}: We want to use A4 paper size
%     \item \texttt{twocolumn}: The document should be typeset in two columns
%     \item \texttt{fontsize=9pt}: The font size should be 9pt to increase density for 
%           shorter papers and abstracts
%     \item \texttt{DIV=16}: The DIV parameter should be set to 16 to ensure a good
%           balance between text width and height
% \end{itemize}
%    
%    \begin{macrocode}
\LoadClass[a4paper,twocolumn,fontsize=9pt,DIV=16]{scrartcl}
%    \end{macrocode}
%
% \subsection{Used packages}
% 
% For the styling of the document we use the following packages and therefore they are 
% preloaded and you might not load them again in your document:
%
%    \begin{macrocode}
\RequirePackage{fhjcommon}
\RequirePackage{etoolbox}
\PassOptionsToPackage{pagestyleset=standard}{scrlayer-scrpage}
\RequirePackage{scrlayer-scrpage}
\PassOptionsToPackage{hidelinks}{hyperref}
\RequirePackage{hyperref}
%    \end{macrocode}
% 
% \section{Title}
% 
% To support in the creation of the title and abstract on the first page we provide a
% set of macros.To have the correct name for the study program typeset on the document, you
% need to define it with the macro from \texttt{fhjcommon} package (see the according 
% documentation):
% 
% \begin{quote}
%  |\FHJProgram{DAT}|
% \end{quote}
% 
% \DescribeMacro{\title}
% This command defines the title of your document and is placed on top of the first page.
% on the according position. It is supported to use line breaks in the title, so you can
% have multiple lines but we clearly recommend to limit it to three lines maximum.
%
% \DescribeMacro{\shorttitle}
% This command defines the short title of your document which is used in the footer. 
% This supports the reader if multiple documents are printed together in a single
% collection.
% 
%    \begin{macrocode}
\renewcommand*{\title}[1]{\renewcommand*{\@title}{{#1}}}
\newcommand{\shorttitle}[1]{\newcommand{\@shorttitle}{{#1}}}
%    \end{macrocode}
% 
% \subsection{Authors and Affiliations}
%  
% This code is based on APA 7th edition style guide without including the deprecated 
% commands. \par
% 
% \begin{itemize}
% \item \DescribeMacro{\authorsnames}|\authorsnames|
%       \oarg{comma separated list of superscripts}
%       \marg{comma separated list of authors name(s)}: Authors names
% \end{itemize}
%
% Where \marg{authors names} is a comma separated list of authors in display order
% and \oarg{superscripts} is an optional comma separated list of affiliation superscripts. 
% If there are multiple superscripts for one author, put the set of superscripts within |{ }|.
%
% \begin{itemize}
% \item
%   \DescribeMacro{\authorsaffiliations}|\authorsaffiliations|\marg{comma separated list
%   of affiliation(s)}: Author affiliation(s). Put each affiliation within |{ }| and 
%   separate each affiliation with a comma.
% \end{itemize}
%
% \noindent \textbf{Examples:} \par
% \noindent One author, one affiliation \par
% |\authorsnames{Maggie C. Leonard}| \par
% |\authorsaffiliations{{Department of Psychology,%| \par
% \hspace*{2em}|George Mason University}}| \par
% \noindent One author, two affiliations \par
% |\authorsnames{Andrew K. Jones-Willoughby}| \par
% |\authorsaffiliations{{School of Psychology,%| \par
% \hspace*{2em}|University of Sydney},| \par
% \hspace*{2em}|{Center for Behavioral Neuroscience, American University}}| \par
% \noindent Two authors, shared affiliation \par
% |\authorsnames{Mackenzie J. Clement,Talia R. Cummings}| \par
% |\authorsaffiliations{{College of Nursing,%| \par
% \hspace*{2em}|Michigan State University}}| \par
% \noindent Two authors, different affiliations \par
% |\authorsnames[1,2]{Wilhelm T. Webber,Latasha P. Jackson}| \par
% |\authorsaffiliations{%| \par
% \hspace*{2em}|{Max Planck Institute for Human Development, Berlin, Germany},%| \par
% \hspace*{2em}|{College of Education, University of Georgia}}| \par
% \noindent Three or more authors, different affiliations \par
% |\authorsnames[1,{2,3},1]{%| \par
% \hspace{2em}|Savannah C. St. John,%| \par
% \hspace{2em}|Fen-Lei Chang,%| \par
% \hspace{2em}|Carlos O. Vásquez III}| \par
% |\authorsaffiliations{%| \par
% \hspace{2em}|{Education Testing Service, Princeton, New Jersey, ...},| \par
% \hspace{2em}|{MRC Cognition and Brain Science Unit, Cambridge, England},| \par
% \hspace{2em}|{Department of Psychology, University of Cambridge}}| \par
%
% \subsection{Page style}
% 
% The footer contains the short title of the document and the header contains the
% long title of the study program. The section counter is limited to two levels 
% (|subsection| maximum).
%
%    \begin{macrocode}
\ofoot{\@shorttitle}
\ifoot{\FHJProgramLong}
\setcounter{secnumdepth}{2}
%    \end{macrocode}
% 
% \MaybeStop{\PrintChanges}
% 
% \section{Implementation}
% 
%    \begin{macrocode}

%%-------------------------------------------------------------------------------------
%% authors handling
%% Taken von apa7 class --> See: https://ctan.org/tex-archive/macros/latex/contrib/apa7

%% German and English!
\newcommand*{\lastauthorseparator}{UNDEF}
\iflanguage{english}{\renewcommand*{\lastauthorseparator}{and}}{}
\iflanguage{ngerman}{\renewcommand*{\lastauthorseparator}{und}}{}


\newcommand*\listauthors{}
\newcommand*\listsuperscripts{}
\newcommand*\listaffiliations{}
\newcounter{NumberOfAuthors}
\newcounter{NumberOfSuperscripts}

\newcommand*{\authorsnames}[2][]{%
  \def\def@multipleauthors{\@multipleauthorsmode} %
  \renewcommand*\listauthors{}
  \renewcommand*\listsuperscripts{}
  \setcounter{NumberOfAuthors}{0}
  \setcounter{NumberOfSuperscripts}{0}
  \forcsvlist{\stepcounter{NumberOfAuthors}\listadd\listauthors}{#2}
  \forcsvlist{\stepcounter{NumberOfSuperscripts}\listadd\listsuperscripts}{#1}
}

\newcounter{NumberOfAffiliations}
\newcommand*{\authorsaffiliations}[1]{%
  \def\def@multipleaffils{\@multipleaffilsmode} %
  \renewcommand*\listaffiliations{}
  \setcounter{NumberOfAffiliations}{0}
  \forcsvlist{\stepcounter{NumberOfAffiliations}\listadd\listaffiliations}{#1}
}

%% \makeatletter
%%   \catcode`\|=3
  
\def\looptwo#1#2{%
    \edef\tmp{\noexpand\xtwo%
      \unexpanded\expandafter{#1}\relax  % no added delimiter here
      \unexpanded\expandafter{#2}\relax  % no added delimiter here
    } \tmp%
  }%
  
\def\xtwo#1|#2\relax#3|#4\relax{%
    \dotwo{#1}{#3}%
    \def\tmp{#2}%
    \ifx\empty\tmp%
      \expandafter\@gobble%
    \else%
      \expandafter\@firstofone%
    \fi%
    {\xtwo#2\relax#4\relax}%
  }%

%%  \catcode`\|=12

\newcommand*{\dotwo}[2]{}

\newcommand*{\authorsep}{}%
\newcommand*{\lastauthor}{}%
\newcommand*{\prelastauthor}{}%
\newcommand*{\prelastauthorsep}{}%

\newcommand{\displayauthors}{%
  \renewcommand*{\authorsep}{}%
  \renewcommand*{\lastauthor}{}%
  \renewcommand*{\prelastauthor}{}%
  \renewcommand*{\prelastauthorsep}{}%
  \ifnum\value{NumberOfSuperscripts}=0% If no superscripts are specified, print authors without superscripts.
	  \renewcommand*{\do}[1]{%
      \authorsep%
      \lastauthor%
      \renewcommand{\lastauthor}{%
        \renewcommand{\authorsep}{, 
          \renewcommand*{\prelastauthorsep}{,}}%
        \renewcommand{\prelastauthor}{\prelastauthorsep\ \lastauthorseparator\ }% Terminated commands with \ to preserve following space
        ##1%
      }%
    }%
    \dolistloop{\listauthors}%
  \else
    \renewcommand*{\dotwo}[2]{%
      \authorsep%
      \lastauthor%
      \renewcommand{\lastauthor}{%
        \renewcommand{\authorsep}{, 
          \renewcommand*{\prelastauthorsep}{,}}%
        \renewcommand{\prelastauthor}{\prelastauthorsep\ \lastauthorseparator\ }% Terminated commands with \ to preserve following space
        ##1\textsuperscript{##2}%
      }%
    }%
    \looptwo\listauthors\listsuperscripts%
  \fi
  \prelastauthor \lastauthor%
}

\newcommand{\displayaffiliations}{%
  \ifnum\value{NumberOfSuperscripts}=0% If no superscripts are specified, print affiliations without superscripts.
    \renewcommand*{\do}[1]{%
        ##1\\%
    }%
  \else
    \newcounter{AffiliationNumber}
    \renewcommand*{\do}[1]{%
        \stepcounter{AffiliationNumber}
        \textsuperscript{\arabic{AffiliationNumber}}##1\\%
    }%
  \fi
  \dolistloop{\listaffiliations}%
}

\newcommand{\check@title}{%
  \@ifundefined{@title}{%
    \ClassWarningNoLine{fhjpaper}{Title not defined \title{Title}}}
  \@ifundefined{@shorttitle}{%
    \ClassWarningNoLine{fhjpaper}{Shorttitle not defined \shorttitle{Shorttitle}}}
}

\newcommand{\check@author}{%
  \@ifundefined{def@multipleauthors}{%
    \ClassWarningNoLine{fhjpaper}{Author not defined \authorsnames{Author}}}
  \@ifundefined{def@multipleaffils}{%
    \ClassWarningNoLine{fhjpaper}{Affiliation not defined \authorsaffiliations{Affiliation}}}
  \@ifundefined{def@multipleauthors}{}{%
	   \ifnum\value{NumberOfSuperscripts}=0%
	   	% do nothing. If no superscripts are specified, print authors and affiliations without superscripts.
	   \else%
      \ifnum\value{NumberOfAuthors}=\value{NumberOfSuperscripts}%
        % do nothing
      \else 
        \ClassError{fhjpaper}{Number of Authors does not match number of Superscripts}%
          {Please provide an equal number of authors and superscripts.}%
		  \fi
	   \fi
  }%
}

%%-------------------------------------------------------------------------------------
%% Make title
\renewcommand*{\@maketitle}{
  \cleardoublepage
  \check@author
  \check@title
  % title
  \begin{minipage}{\textwidth}\begin{center}
    \fontsize{14}{18}\selectfont
    \textbf{\@title}
    \vspace{1em}
  \end{center}\end{minipage}
  % authors
  \begin{minipage}{\textwidth}\begin{center}
    \displayauthors
    \vspace{1em}
  \end{center}\end{minipage}
  % affiliations
  \begin{minipage}{\textwidth}\begin{center}
    \displayaffiliations
    \vspace{2em}
  \end{center}\end{minipage}
}
%    \end{macrocode}
% 
% \Finale
% 
\endinput
