% Manuscript styling
\usepackage{upgreek}
\captionsetup{font=singlespacing,justification=justified}

% Table formatting
\usepackage{longtable}
\usepackage{lscape}
% \usepackage[counterclockwise]{rotating}   % Landscape page setup for large tables
\usepackage{multirow}		% Table styling
\usepackage{tabularx}		% Control Column width
\usepackage[flushleft]{threeparttable}	% Allows for three part tables with a specified notes section
\usepackage{threeparttablex}            % Lets threeparttable work with longtable

% Create new environments so endfloat can handle them
% \newenvironment{ltable}
%   {\begin{landscape}\centering\begin{threeparttable}}
%   {\end{threeparttable}\end{landscape}}
\newenvironment{lltable}{\begin{landscape}\centering\begin{ThreePartTable}}{\end{ThreePartTable}\end{landscape}}

% Enables adjusting longtable caption width to table width
% Solution found at http://golatex.de/longtable-mit-caption-so-breit-wie-die-tabelle-t15767.html
\makeatletter
\newcommand\LastLTentrywidth{1em}
\newlength\longtablewidth
\setlength{\longtablewidth}{1in}
\newcommand{\getlongtablewidth}{\begingroup \ifcsname LT@\roman{LT@tables}\endcsname \global\longtablewidth=0pt \renewcommand{\LT@entry}[2]{\global\advance\longtablewidth by ##2\relax\gdef\LastLTentrywidth{##2}}\@nameuse{LT@\roman{LT@tables}} \fi \endgroup}
\makeatother

% \setlength{\parindent}{0.5in}
% \setlength{\parskip}{0pt plus 0pt minus 0pt}

% Overwrite redefinition of paragraph and subparagraph by the default LaTeX template
% See https://github.com/crsh/papaja/issues/292
\makeatletter
\renewcommand{\paragraph}{\@startsection{paragraph}{4}{\parindent}%
  {0\baselineskip \@plus 0.2ex \@minus 0.2ex}%
  {-1em}%
  {\normalfont\normalsize\bfseries\itshape\typesectitle}}

\renewcommand{\subparagraph}[1]{\@startsection{subparagraph}{5}{1em}%
  {0\baselineskip \@plus 0.2ex \@minus 0.2ex}%
  {-\z@\relax}%
  {\normalfont\normalsize\itshape\hspace{\parindent}{#1}\textit{\addperi}}{\relax}}
\makeatother

% \makeatletter
% \usepackage{etoolbox}
% \patchcmd{\maketitle}
%   {\section{\normalfont\normalsize\abstractname}}
%   {\section*{\normalfont\normalsize\abstractname}}
%   {}{\typeout{Failed to patch abstract.}}
% \patchcmd{\maketitle}
%   {\section{\protect\normalfont{\@title}}}
%   {\section*{\protect\normalfont{\@title}}}
%   {}{\typeout{Failed to patch title.}}
% \makeatother

\usepackage{xpatch}
\makeatletter
\xapptocmd\appendix
  {\xapptocmd\section
    {\addcontentsline{toc}{section}{\appendixname\ifoneappendix\else~\theappendix\fi: #1}}
    {}{\InnerPatchFailed}%
  }
{}{\PatchFailed}
\makeatother

% % Add page break after TOC
% \let\tableofcontentsORIG\tableofcontents
% \renewcommand\tableofcontents{\tableofcontentsORIG\clearpage}

% Running head settings so page numbers are displayed on the left/ right side of the page. I am not sure all settings are neccesary here but this worked for me.
\fancyheadoffset[L]{0pt}  
% \fancyhf{}
\fancyhead[RE]{}
\fancyhead[LO]{\theshorttitle}
\fancyhead[RO,LE]{\small\thepage} 

% Don't break footnotes across pages
\interfootnotelinepenalty=10000

% Load to adjust linespacing
\usepackage{setspace}

% Custom title page
\usepackage{pbox}

\makeatletter
\renewcommand{\maketitle}{
  \@ifundefined{hrm}{}{\hrm}
  \@ifundefined{@author}{\let\theauthor{}}{\let\theauthor\@author}
  \@ifundefined{@shorttitle}{\let\theshorttitle{}}{\let\theshorttitle\@shorttitle}
  \thispagestyle{empty}


  \noindent \pbox{0.25\textwidth}{%
    \@ifundefined{logofile}{}{\includegraphics*[scale=\logoscale]{\logofile}}%
  } \hfill %
  \pbox{0.5\textwidth}{%
    \setstretch{1.15}
    \semester\\
    \degree\\
    \module\\
    \course\\
    \@ifundefined{advisor}{}{\advisor}
  }

  \begin{center}
  \vspace*{5cm}
  \textbf{\huge \@title} \\
  \vspace{1cm}
  \textbf{\LARGE \@author} \\
  \vspace{0.5cm}
  \studentsemester \\
  \studentid \\
  \smail \vfill

  \@ifundefined{@note}
    {}
    {\@note \vfill}
 
  \pbox{0.45\textwidth}{Tag der Abgabe: \@ifundefined{thedate}{\today}{\thedate}} \hfill%
  \pbox{0.45\textwidth}{\place, den \@ifundefined{thedate}{\today}{\thedate}}
  \end{center}

  \clearpage\mbox{}\thispagestyle{empty}\clearpage
  \setcounter{page}{3}

  \@ifundefined{@acks}
    {}
    {%
      % \vfill%
      % \begin{center}%
      %     \acksname%
      % \end{center}%
      % \protect\raggedright
      % \setlength{\parindent}{0.4in}
      \vspace*{4cm}
      \noindent\@acks\par%
    }

  \clearpage
  
  %BDB\hyphenpenalty 10000
  \fussy
  \@ifundefined{@abstract}{}{%
    \section{\abstractname}% BDB
    \noindent\@abstract\par% BDB
    \@ifundefined{charcount}{}{%
      \setlength{\parindent}{0.4in}% BDB
      \indent Zeichenanzahl: \charcount%
    }%
    \newpage
  }

  {
  \setcounter{tocdepth}{2}
  \tableofcontents
  }
  \clearpage

  \section[Einleitung]{\@title}
  \raggedright%
  \setlength{\parindent}{0.4in}%
  % \noindent%
}
\makeatother

% No indentation of first paragraph
\makeatletter
\b@level@one@skip=-2.5ex plus -1ex minus -.2ex
\b@level@two@skip=-2.5ex plus -1ex minus -.2ex
\makeatother
