% \iffalse meta-comment
%% kautools.dtx
%% Copyright (c) 2011-2015 Stefan Berthold <stefan.berthold@kau.se>
%
% This file is part of the kauthesis bundle.
%
% 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 and author of this work is Stefan Berthold.
%
% This work consists of all files listed in manifest.txt.
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{\jobname.dtx}
%</driver>
%<title|meta|list|clear|environments|parts|paper|studies|crop>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<title>\ProvidesPackage{kautitle}
%<meta>\ProvidesPackage{kaumeta}
%<list>\ProvidesPackage{kaulist}
%<clear>\ProvidesPackage{kauclear}
%<environments>\ProvidesPackage{kauenvironments}
%<parts>\ProvidesPackage{kauparts}
%<paper>\ProvidesPackage{kaupaper}
%<studies>\ProvidesPackage{kaustudies}
%<language>\ProvidesPackage{kaulanguage}
%<crop>\ProvidesPackage{pgfcropmarks}
%<*title|meta|list|clear|environments|parts|paper|studies|language|crop>
    [2014/10/22 v1.15 Karlstad University kautools bundle]
%</title|meta|list|clear|environments|parts|paper|studies|language|crop>
%
%<*driver>
\documentclass[a4paper]{ltxdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{707}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2011/02/01}{SBe's licentiate version}
% \changes{v1.1}{2011/05/06}{Initial public release}
% \changes{v1.2}{2012/12/14}{Support for babel.sty,\\Documentation added}
% \changes{v1.3}{2012/12/15}{Merged the classes' dtx files}
% \changes{v1.4}{2012/12/20}{Fixed babel.sty support}
% \changes{v1.5}{2012/12/20}{Added templates for kaucollection and kaureport}
% \changes{v1.6}{2012/12/22}{Merged the styles' dtx files}
% \changes{v1.7}{2013/04/16}{Added macros for supervisor and examiner in kaumeta}
% \changes{v1.8}{2013/04/18}{Added macros pretty printing lists in kaulist}
% \changes{v1.9}{2013/05/03}{Version bump without changes}
% \changes{v1.10}{2013/05/28}{Version bump without changes}
% \changes{v1.11}{2013/09/17}{Fixed missing file in babel support}
% \changes{v1.14}{2014/10/20}{Added refnote}
% \changes{v1.15}{2014/10/22}{Added refprefix}
%
% \GetFileInfo{\jobname.dtx}
%
% \DoNotIndex{\newcommand,\renewcommand,\newenvironment,\let}
%
% \StopEventually{}
%
% \title{The \textsf{\jobname} bundle\thanks{This document
%   corresponds to \textsf{\jobname}~\fileversion, dated \filedate.}}
% \author{Stefan Berthold \\ \texttt{stefan.berthold@kau.se}}
%
% \maketitle
% \begin{abstract}
%   The \textsf{\jobname} bundle provides tools (\LaTeX{} style files) for the \textsf{kauthesis} bundle.
% \end{abstract}
% \tableofcontents
%
% \section{Introduction}
%
% \subsection{The \textsf{kautitle} package}
%
% The \textsf{kautitle} package defines an alternative title page using the logo of Karlstad University. Note that this is not an implementation of the Karlstad University Studies title page, i.\,e., this alternative title page won't be used for your thesis.
% The logo is available from
% \begin{quote}
%   http://www.kau.se/sites/default/files/image/press/kau-logo-tryck.pdf
% \end{quote}
% and must be available in \LaTeX's search path before using this package.
%
% At the moment, this package uses a manually installed font called \textsf{aun} and it's bold version \textsf{aub}. It might not be available on your system, though, and since the font is licensed, I may not distribute it. If you do not possess the font, you have to switch to another one.
%
% Load the package without options. The title page can be created using \DescribeMacro{\maketitle}|\maketitle|. Note that the previous definition of |\maketitle| is not destroyed, but preserved in the macro \DescribeMacro{\kut@maketitle}|\kut@maketitle|. You can use it in your document as follows:
% \begin{quote}\begin{verbatim}\makeatletter
%\kut@maketitle
%\makeatother\end{verbatim}\end{quote}
%
% Two fonts are used on the title page and can be changed using the macro \DescribeMacro{\setkautitlefont}|\setkautitlefont|\marg{font}. The argument \meta{font} accepts key-value pairs where keys are |normalfont| and |titlefont| and values are any sequence of \LaTeX's font commands. For switching to your standard sans-serif font, you should add these lines to your preamble: 
% \begin{quote}\begin{verbatim}\setkautitle%
%  { normalfont=\sffamily\LARGE
%  , titlefont=\sffamily\bfseries\Huge }\end{verbatim}\end{quote}
%
% \subsection{The \textsf{kaumeta} package}
%
% The \textsf{kaumeta} package defines three macros, |\subject|, |\institute|, and |\place|, for providing additional meta data about the document and it's author. This meta data can be used, e.\,g., on the title page of the document.
%
% \subsection{The \textsf{kaulist} package}
%
% The \textsf{kaulist} package defines pretty printing macros for lists. For instance, a list ``1,2,3'' is transformed to ``1, 2, and 3''.
%
% \subsection{The \textsf{kauclear} package}
%
% This package replaces the macro |\cleardoublepage|. Empty pages with odd page numbers will not print the page number. Also, the macro |\pagestyle| is redefined. The new behaviour is to stores the page style for being able to restore it later on.
%
% \subsection{The \textsf{kauenvironments} package}
%
% This package defines the environments |researchquestions| and |contributions|.
%
% \subsection{The \textsf{kauparts} package}
%
% This packages defines a new |\part| macro, |\kaupart|. It is independend of |\part|, but you should decide yourself for one alternative only. The |\kaupart| macro adds a cover page for the new part.
%
% Use the package options |vshift| and |vstep| to adjust the placement of the thumb indices on the cover pages.
%
% \DescribeMacro{\kaupart} 
% Use |\kaupart|\oarg{meta}\marg{title} to create a numbered part with the given \meta{title}. You may use the optional argument \meta{arg} for adding meta data. This includes |body| for typing additional material on the cover page and |label| for creating a label such as usually created by |\label|. In addition, the switch |tocentry| may be set to false, if the macro should not add an entry to the table of contents.
%
% \DescribeMacro{\kaupart*}
% The starred version of |\kaupart| works exactly like the unstarred version, but creates an unnumbered part.
%
% \subsection{The \textsf{kaupaper} package}
%
% \emph{Yet no description.}
%
% \subsection{The \textsf{kaustudies} package}
%
% This package sets the page dimensions so that your document does not need to be scaled for printing. It also chooses Garamond as the standard font.
%
% You can use the package option |tinyfontswitch=false| in order to avoid the switch to sans-serif fonts when using |\tiny|. The switch is recommended by Karlstad University Studies, though, it is rather debatable whether this is necessary when using \LaTeX{} and the correct page dimensions.
%
% \subsection{The \textsf{kaulanguage} package}
%
% This packages defines a macro for supporting names in English and Swedish depending on the chosen language in \textsf{babel}.
%
% \subsection{The \textsf{pgfcropmarks} package}
%
% The package \textsf{pgfcropmarks} draws crop marks on A4~paper. This is supposed to be the last step after creating all tables, indices, and glosseries. When the package is loaded, \LaTeX{} will not create or update any auxiliary files.
%
% \section{Implementation}
%
% \subsection{The \textsf{kautitle} package}
%
% No package options are accepted.
%    \begin{macrocode}
%<*title>
\ProcessOptions\relax
%    \end{macrocode}
%
% \noindent Load dependencies.
%    \begin{macrocode}
\RequirePackage{xkeyval}
\RequirePackage{tikz}
\RequirePackage{hyphenat}
\RequirePackage{kaumeta}
%    \end{macrocode}
%
% \begin{macro}{\setkautitlefont}
% The macro |\setkautitlefont| allows to choose another than the default font for the title page. The title page has only two different font settings, |titlefont| which is used for the title text and |normalfont| which is used otherwise.
%    \begin{macrocode}
\define@cmdkeys[kut]{pkg}[kut@]{normalfont,titlefont}
\setkeys[kut]{pkg}%
  { normalfont=\fontfamily{aun}\fontsize{18}{21.6}\selectfont\bfseries
  , titlefont=\fontfamily{aub}\fontsize{28}{33.6}\selectfont }
\newcommand\setkautitlefont[1]{\setkeys[kut]{pkg}{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kut@maketitle}
% Preserve the previous version of |\maketitle|.
%    \begin{macrocode}
\let\kut@maketitle\maketitle
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\maketitle}
% The title page will consist of two background images (the Karlstad University logo), title, subject, author, and author's institute. Everything will be shaded except the title. 
%    \begin{macrocode}
\renewcommand\maketitle{%
  \thispagestyle{empty}
  \begin{tikzpicture}[remember picture, overlay]%
%    \end{macrocode}
% Clipping the title page to the dimensions of the logical page. This is interesting when the physical and the logical page do not have the same dimensions.
%    \begin{macrocode}
    \path (current page.north west) +(-4pt,4pt) node (NW) {};
    \path (current page.south east) +(4pt,-4pt) node (SE) {};
    \path (current page.north west) +(-3pt,3pt) node (NWC) {};
    \path (current page.south east) +(3pt,-3pt) node (SEC) {};
    \path[clip] (NWC) rectangle (SEC);
%    \end{macrocode}
% Background images.
%    \begin{macrocode}
    \path (current page.south west) node[rotate=243.5] {\pgfimage{kau-logo-tryck}};%
    \path (current page.north east) node[rotate=175] {\pgfimage{kau-logo-tryck}};%
%    \end{macrocode}
% Subject in the upper left corner of the page.
%    \begin{macrocode}
    \path (current page.north west) node
      [shift={(10pt,-12pt)},anchor=south west,rotate=270
      ,font=\kut@normalfont\vphantom{Xy}]
      {\@subject};
%    \end{macrocode}
% Institute in the lower right corner of the page.
%    \begin{macrocode}
    \path (current page.south east) node
      [shift={(-8pt,6pt)},anchor=north east,rotate=270
      ,rounded corners,fill=black,text=white,inner sep=8pt
      ,font=\kut@normalfont\vphantom{Xy}]
      {\@shortinstitute};
%    \end{macrocode}
% Title bar on vertically centered on the left side of the page.
%    \begin{macrocode}
    \path
      (current page.east) node[shift={(-10,0)}] (A) {}
      (current page.east) node[shift={(0,-2.5)}] (B) {}
      [shading=axis,left color=white,right color=black] (A) rectangle (B.south east)
    ;
%    \end{macrocode}
% Author below the title bar.
%    \begin{macrocode}
    \path (B) node
      [anchor=north east
      ,inner sep=8pt
      ,text=black
      ,font=\kut@normalfont\vphantom{Xy}
      ,text width=14cm,text ragged left]
      {\@author};
%    \end{macrocode}
% Shading over the entire page (slightly bigger than the clipping).
%    \begin{macrocode}
    \path[fill=white,opacity=0.90,transparency group] (NW) rectangle (SE);
%    \end{macrocode}
% Title text on the title bar. No hyphens are allowed in the title text.
%    \begin{macrocode}
    \path (B) node
      [anchor=south east,shift={(-3pt,0)}
      ,text=yellow!75!red
      ,font=\kut@titlefont\vphantom{Xy}
      ,text width=14cm,text ragged left]
      {\nohyphens{\@title}};
  \end{tikzpicture}%
  \clearpage%
}
%</title>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{kaumeta} package}
%
% No package options are parsed.
%    \begin{macrocode}
%<*meta>
\ProcessOptions\relax
%    \end{macrocode}
%
% \noindent Dependencies are loaded.
%    \begin{macrocode}
\usepackage{ifthen}
%    \end{macrocode}
%
% \begin{macro}{\subject}
% The macro |\subject|\marg{subject} can be used to set the subject of the document. The last \meta{subject} set will be available in the macro \DescribeMacro{\@subject}|\@subject|.
%    \begin{macrocode}
\newcommand*\@subject{}
\newcommand\subject[1]{\renewcommand*\@subject{#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\institute}
% The macro |\institute|\oarg{short}\marg{institute} can be used to set the institute of the document's author. It is possible to provide a shorter version of the institue in the optional argument \meta{short}. If not explicitly set, the short version is the same as the long version \meta{institute}. The values are available in the macros \DescribeMacro{\@institute}|\@institute| and \DescribeMacro{\@shortinstitute}|\@shortinstitute|.
%    \begin{macrocode}
\newcommand*\@institute{}
\newcommand*\@shortinstitute{}
\newcommand\institute[2][]{%
  \renewcommand*\@institute{#2}%
  \ifthenelse{\equal{#1}{}}{%
    \renewcommand*\@shortinstitute{#2}%
  }{%
    \renewcommand*\@shortinstitute{#1}%
  }%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\place}
% The macro |\place|\marg{place} can be used to set the place of the document's author. The last \meta{place} set will be available in the macro \DescribeMacro{\@place}|\@place|.
%    \begin{macrocode}
\newcommand*\@place{}
\newcommand\place[1]{\renewcommand*\@place{#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\supervisor}
% \marg{name} sets the supervisor's name for the \textsf{kaumasters} class.
%    \begin{macrocode}
\newcommand\@supervisor{}
\newcommand\supervisor[1]{\renewcommand*\@supervisor{#1}}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\examiner}
% \marg{name} sets the examiner's name for the \textsf{kaumasters} class.
%    \begin{macrocode}
\newcommand\@examiner{}
\newcommand\examiner[1]{\renewcommand*\@examiner{#1}}
%</meta>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{kaulist} package}
%
% No options are parsed.
%
%    \begin{macrocode}
%<*list>
\ProcessOptions\relax
\RequirePackage{kaulanguage}
%    \end{macrocode}
%
% For |\prettylist|, the packages \textsf{pgffor}, \textsf{pgfmath}, and \textsf{pgfkeys} are required.
%
%    \begin{macrocode}
\RequirePackage{tikz}
%    \end{macrocode}
%
% \begin{macro}{\commasign}
% defines a comma (``,''), if one is to be used before ``and''.
%    \begin{macrocode}
\AtBeginDocument{%
  \setname{commasign}{}{,}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\andname}
% definses the word used as ``and''.
%    \begin{macrocode}
\AtBeginDocument{%
  \setname{andname}{and}{och}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\prettylist}
% \marg{list} transformes a comma separated list into a list in natural language.
%    \begin{macrocode}
\newcommand\prettylist[1]{%
%    \end{macrocode}
% The number of list elements is stored in |\num|.
%    \begin{macrocode}
  \foreach[count=\xi] \x in #1 {}%
  \let\num=\xi%
%    \end{macrocode}
% The first element is just printed without any changes.
%    \begin{macrocode}
  \foreach[count=\xi] \x in #1 {%
    \ifnum \xi=1%
      \x%
%    \end{macrocode}
% The last element is printed with preceding |\commasign|, if there are more than two elements in the list, and otherwise without.
%    \begin{macrocode}
    \else%
      \ifnum \xi=\num%
        \ifnum \num=2%
          {} \andname{} \x%
        \else%
          \commasign{} \andname{} \x%
        \fi%
%    \end{macrocode}
% Elements in between the first and the last one are printed with a preceding hard-coded comma sign (not |\commasign|).
%    \begin{macrocode}
      \else%
        , \x%
      \fi%
    \fi%
  }%
}%
%</list>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{kauclear} package}
%
% No options are parsed.
%
%    \begin{macrocode}
%<*clear>
\ProcessOptions\relax
%    \end{macrocode}
%
% \begin{macro}{\kuc@currentpagestyle}
% The page style is stored in |\kuc@currentpagestyle|. Default: |plain|
%    \begin{macrocode}
\newcommand\kuc@currentpagestyle{plain}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\pagestyle}
% The macro |\pagestyle| is redefined to update the page style in |\kuc@currentpagestyle|.
%    \begin{macrocode}
\let\pagestyle@orig\pagestyle
\renewcommand\pagestyle[1]{%
  \renewcommand\kuc@currentpagestyle{#1}%
  \pagestyle@orig{#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\kuc@restorepagestyle}
% The page style can be restored with |\kuc@restorepagestyle| after (temporarily) switching to another one using |\pagestyle@orig|.
%    \begin{macrocode}
\newcommand\kuc@restorepagestyle{%
  \pagestyle@orig{\kuc@currentpagestyle}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cleardoubpage@orig}
% The previous definition of |\cleardoublepage| is preserved in |\cleardoublepage@orig|.
%    \begin{macrocode}
\let\cleardoubpage@orig\cleardoublepage
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cleardoublepage}
% The macro |\cleardoublepage| is redefined. The new definition switches temporarily to the page style |empty|.
%    \begin{macrocode}
\renewcommand\cleardoublepage{%
  \clearpage%
  \pagestyle@orig{empty}%
  \cleardoubpage@orig%
  \kuc@restorepagestyle%
}
%</clear>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{kauenvironments} package}
%
% No package options are parsed.
%
%    \begin{macrocode}
%<*environments>
\ProcessOptions\relax
%    \end{macrocode}
%
% \noindent Load dependencies.
%    \begin{macrocode}
\RequirePackage{xspace}
\RequirePackage{xcolor}
%    \end{macrocode}
%
% \begin{environment}{researchquestions}
% The environment is an enumeration derived from |enumerate|. The argument of |\item|\oarg{question} is used to formulate the research question.
%    \begin{macrocode}
\newenvironment{researchquestions}%
  {\begin{enumerate}%
    \let\kue@item\item%
    \renewcommand\item[1][Research question]%
      {\kue@item\emph{##1}\nopagebreak\par}%
  }%
  {\end{enumerate}}%
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{contributions}
% The environment is an enumeration derived from |enumerate|. The argument of |\item|\oarg{contrib} is used to formulate the contribution.
%    \begin{macrocode}
\newenvironment{contributions}%
  {\begin{enumerate}%
    \let\kue@item\item%
    \renewcommand\item[1][%
      \textcolor{red}{\emph{write your contribution between brackets 
        as optional argument of \texttt{\string\item}.}}]%
      {\kue@item\emph{##1}\xspace}%
  }%
  {\end{enumerate}}%
%</environments>
%    \end{macrocode}
% \end{environment}
%
% \subsection{The \textsf{kauparts} package}
%
%    \begin{macrocode}
%<*parts>
\RequirePackage{xkeyval}

\define@cmdkeys[kup]{pkg}[kup@]{vshift,vstep}
\setkeys[kup]{pkg}{vshift=2,vstep=0.8}

\ProcessOptionsX[kup]<pkg>

\RequirePackage{calc}
\RequirePackage{ifthen}
\RequirePackage{suffix}
\RequirePackage{tikz}
\usetikzlibrary{positioning}

\define@cmdkeys[kup]{part}[kup@]{body,label}
\define@boolkeys[kup]{part}[kup@]{tocentry}

\newcounter{kup@part}
\newcounter{kup@vertical}
\setcounter{kup@vertical}{0}

\newcommand*\@titlepagestyle{empty}
\newcommand\titlepagestyle[1]{\renewcommand*\@titlepagestyle{#1}}

\newcommand\kup@partheadline[2]{%
  \path[node distance=1ex] (current page.north east) +(3pt,\kup@vshift+\kup@vstep*\arabic{kup@vertical}) %
    node%
      [ anchor=north east%
      , minimum width=2cm+3pt%
      %, minimum height=7mm%
      %, draw=black%
      %, fill=black%
      , left color=white%
      , right color=black%
      , text=white%
      , font=\LARGE\bfseries%
      ] (bar) {#1}%
    node[base left=of bar,font=\LARGE\bfseries] {#2};%
}
%    \end{macrocode}
% \begin{macro}{\kaupart}
%    \begin{macrocode}
\newcommand\kaupart[2][]{%
  \cleardoublepage%
  \thispagestyle{\@titlepagestyle}%
  \stepcounter{kup@part}%
  \setkeys[kup]{part}{body=\relax,label=,tocentry=true,#1}%
  \providecommand\phantomsection{}%
  \phantomsection%
  \ifkup@tocentry%
    \addcontentsline{toc}{part}{\numberline{\Roman{kup@part}}\textsc{#2}}%
  \fi%
  \renewcommand\@currentlabel{\Roman{kup@part}}%
  \ifthenelse{\equal{\kup@label}{}}{}{\label{\kup@label}}%
  \begin{tikzpicture}[remember picture,overlay,yscale=-1]%
    \kup@partheadline{\Roman{kup@part}}{#2}%
  \end{tikzpicture}%
  \kup@body
  \stepcounter{kup@vertical}%
  \cleardoublepage%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\kaupart*}
%    \begin{macrocode}
\WithSuffix\newcommand\kaupart*[2][]{%
  \cleardoublepage%
  \thispagestyle{\@titlepagestyle}%
  \setkeys[kup]{part}{body=\relax,tocentry=true,#1}%
  \providecommand\phantomsection{}%
  \phantomsection%
  \ifkup@tocentry%
    \addcontentsline{toc}{part}{\textsc{#2}}%
  \fi%
  \begin{tikzpicture}[remember picture,overlay,yscale=-1]%
    \kup@partheadline{\vphantom{X}}{#2}%
  \end{tikzpicture}%
  \kup@body
  \stepcounter{kup@vertical}%
  \cleardoublepage%
}
%</parts>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{kaupaper} package}
%
%    \begin{macrocode}
%<*paper>
\RequirePackage{xkeyval}
\PassOptionsToPackage{hypertexnames=false}{hyperref}

\define@cmdkeys[kua]{paper}[kua@]{author,title,subtitle,refprefix,reference,refnote,email,refstr,vanity,subdelimiter,label,summary,limitations,participation}
\define@cmdkeys[kua]{paperlist}[kua@]{printparticipation,tocline}
\setkeys[kua]{paper}{refstr=\reprintedname}

\ProcessOptionsX[kua]<paper>

\RequirePackage{kauparts}
\RequirePackage{ifthen}
\RequirePackage{tikz}

\RequirePackage{ragged2e}
\RequirePackage{hyphenat}

\RequirePackage{kaulanguage}

\AtBeginDocument{%
  \setname{reprintedname}{Reprinted from}{Ursprungligen publicertad i}%
  \setname{appendedpapersname}{List of Appended Papers}{Bifogade publiceringar}%
  \setname{participationname}{Comments on my Participation}{Kommentarer p\aa{} min medverka}%
  \setname{papername}{Paper}{Publicering}%
}

\newcounter{kua@section}
\newcounter{kua@equation}
\newcounter{kua@figure}
\newcounter{kua@table}

\newcounter{kua@paper}

\providecommand\thepaper{}
\providecommand\thepapertitle{}

\newcommand\kua@warning\relax
\newcommand\kua@dowarn{\gdef\kua@warning{\@latex@warning@no@line{List data created from the kaupaper environment was outdated. Rerun to get the lists right}}}
\AtEndDocument{\kua@warning}

\newcommand\kua@listof[4]{%
  \IfFileExists{\jobname.#1}%
    {% check whether empty file
      \newread\reader%
      \openin\reader\jobname.#1%
      \read\reader to \readmacro%
      \ifeof\reader%
        \textcolor{red}{\ldots{} will be available after the next \LaTeX{} run.}%
        \@starttoc{#1}%
        %\AtEndDocument{\@latex@warning@no@line{Rerun to get the list of #2 right}}%
        \kua@dowarn%
      \else% file not empty -> create list
        #3\@starttoc{#1}#4%
      \fi%
    }%
    {% file does not exist
      \textcolor{red}{\ldots{} will be available after the next \LaTeX{} run.}%
      \@starttoc{#1}%
      %\AtEndDocument{\@latex@warning@no@line{Rerun to get the list of #2 right}}%
      \kua@dowarn%
    }%
}
%    \end{macrocode}
% \begin{macro}{\listofpapers}
%    \begin{macrocode}
\newcommand\listofpapers[1][]{%
  \setkeys[kua]{paperlist}{printparticipation=yes,tocline=yes,#1}
  \section*{\appendedpapersname}%
  \ifthenelse{\equal{\kua@tocline}{yes}}{%
    \addcontentsline{toc}{section}{\appendedpapersname}%
  }{}%
  \kua@listof{pap}{appended papers}{%
    \begin{enumerate}%
    \renewcommand\theenumi{\textbf{\Roman{enumi}}}%
  }{\end{enumerate}}%
  \ifthenelse{\equal{\kua@printparticipation}{yes}}{%
    \subsection*{\participationname}%
    \kua@listof{pcp}{participation in appended papers}{}{}%
  }{}%
}
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}

\newcommand\l@paper[2]{%
  \item #1%
}

\newcommand\listofsummaries{%
  \kua@listof{sum}{paper summaries}{}{}%
}

\newcommand\l@summary[2]{%
  #1%
}

\newcommand\l@limit[2]{%
  %\paragraph{Limitations.}
  #1%
}

\newcommand\l@participation[2]{%
  #1%
}

\newcommand\papercover{%
  \begin{tikzpicture}[remember picture,overlay]%
    \path (current page.center)%
      node%
        [ node distance=\baselineskip%
        , text width=\textwidth%
        , text centered%
        ] (refstr) {\kua@refstr};%
      \ifthenelse{\equal{\kua@subtitle}{}}{%
        \node%
          [ node distance=1.5\baselineskip%
          , above=of refstr%
          , text width=\textwidth%
          , text centered%
          , font=\LARGE\bfseries%
          ] {\nohyphens{\kua@title}};%
      }{%
        \node%
          [ node distance=1.5\baselineskip%
          , above=of refstr%
          , text width=\textwidth%
          , text centered%
          , font=\large\bfseries%
          ] (sub) {\nohyphens{\kua@subtitle}};%
        \node%
          [ node distance=1pt%
          , above=of sub%
          , text width=\textwidth%
          , text centered%
          , font=\LARGE\bfseries%
          ] {\nohyphens{\kua@title}};%
      }%
      \node%
        [ node distance=\baselineskip%
        , below=of refstr%
        , text width=\textwidth%
        , text centered%
        , font=\large%
        ] {\nohyphens{\kua@reference}\ifthenelse{\equal{\kua@refnote}{}}{}{\mbox{}\\\medskip\nohyphens{\kua@refnote}}}%
    ;%
  \end{tikzpicture}%
}

\newenvironment{kaupaper}[1][]{%
  \setcounter{kua@section}{\value{section}}%
  \setcounter{kua@equation}{\value{equation}}%
  \setcounter{kua@figure}{\value{figure}}%
  \setcounter{kua@table}{\value{table}}%
  \setcounter{section}{0}%
  \setcounter{equation}{0}%
  \setcounter{figure}{0}%
  \setcounter{table}{0}%
  \setkeys[kua]{paper}{subtitle=,vanity=,subdelimiter={---},refprefix=,refnote=,label=,summary={\string\textcolor{red}{Use the \string\texttt{summary} key in the \string\texttt{kaupaper} environment to add a summary.}},participation={\string\textcolor{red}{Use the \string\texttt{participation} key in the \string\texttt{kaupaper} environment to add a comment.}},limitations={},#1}%
  \setcounter{kua@paper}{\value{kup@part}}\stepcounter{kua@paper}%
  \kaupart[tocentry=false,label=\kua@label,body=\papercover\kua@vanity]{\papername}%
  \ifthenelse{\equal{\kua@subtitle}{}}%
    {\ifthenelse{\equal{\kua@refnote}{}}%
      {\addcontentsline{pap}{paper}{\kua@author. \kua@title. \kua@refprefix\kua@reference.}}%
      {\addcontentsline{pap}{paper}{\kua@author. \kua@title. \kua@refprefix\kua@reference.\smallskip\\\kua@refnote}}%
     \addcontentsline{toc}{part}{\textsc{\papername} \Roman{kua@paper}: \\\kua@title}}%
    {\ifthenelse{\equal{\kua@refnote}{}}%
      {\addcontentsline{pap}{paper}{\kua@author. \kua@title\kua@subdelimiter\kua@subtitle. \kua@refprefix\kua@reference.}}%
      {\addcontentsline{pap}{paper}{\kua@author. \kua@title\kua@subdelimiter\kua@subtitle. \kua@refprefix\kua@reference.\smallskip\\\kua@refnote}}%
     \addcontentsline{toc}{part}{\textsc{\papername} \Roman{kua@paper}: \\\kua@title\kua@subdelimiter\kua@subtitle}}%
  \addcontentsline{sum}{summary}{\string\subsubsection*{\papername~\Roman{kua@paper} -- \kua@title}\kua@summary}%
  \ifthenelse{\equal{\kua@limitations}{}}{}{%
    \addcontentsline{sum}{limit}{\kua@limitations}}%
  \addcontentsline{pcp}{participation}{\string\paragraph{\papername~\Roman{kua@paper}}\kua@participation}%
  \renewcommand*\thepaper{\papername~\Roman{kua@paper}}%
  \renewcommand*\thepapertitle{\kua@title}%
  \renewcommand\maketitle{%
    \thispagestyle{plain}%
    \centering%
      \Large\bfseries%
      \vspace*{0.15\textheight}%
      \kua@title\par%
      \addvspace{1ex}%
      \ifthenelse{\equal{\kua@subtitle}{}}{}{\large\kua@subtitle}%
      \par\addvspace{\baselineskip}%
      \normalsize%
      \kua@author\\[0.25\baselineskip]%
      \normalfont%
      \kua@email%
    \par\addvspace{2\baselineskip}\justifying%
  }%
}{%
  \setcounter{section}{\value{kua@section}}%
  \setcounter{equation}{\value{kua@equation}}%
  \setcounter{figure}{\value{kua@figure}}%
  \setcounter{table}{\value{kua@table}}%
  \clearpage%
}
%</paper>
%    \end{macrocode}
%
% \subsection{The \textsf{kaustudies} package}
%
%    \begin{macrocode}
%<*studies>
\RequirePackage{xkeyval}

\define@boolkey[kus]{pkg}[kus@]{tinyfontswitch}[true]{}

\presetkeys[kus]{pkg}{tinyfontswitch}{}
\ProcessOptionsX[kus]<pkg>

\RequirePackage[twoside]{geometry}
\RequirePackage{garamondx}
\RequirePackage[garamondx,cmbraces]{newtxmath}

\geometry
  { paperwidth=165mm
  , paperheight=242mm
  , inner=27mm
  , outer=27mm
  , top=24mm
  , bottom=22mm
  }

\ifkus@tinyfontswitch
  \let\kus@tiny@orig\tiny
  \renewcommand*\tiny{\sffamily\kus@tiny@orig}
\fi
%</studies>
%    \end{macrocode}
%
% \subsection{The \textsf{kaulanguage} package}
%
%    \begin{macrocode}
%<*language>
\ProcessOptions\relax
\newif\ifbabel
\babelfalse
\AtBeginDocument{%
  \@ifpackageloaded{babel}%
    {\babeltrue}%
    {\babelfalse}%
}
%    \end{macrocode}
% \begin{macro}{\setname}\marg{macro name}\marg{english}\marg{swedish}
%    \begin{macrocode}
\newcommand\setname[3]{%
  \IfFileExists{swedish.ldf}{%
    \ifbabel%
      \expandafter\edef\csname #1\endcsname{\noexpand\iflanguage{swedish}{#3}{#2}}%
    \else%
      \expandafter\edef\csname #1\endcsname{#2}%
    \fi%
  }%
  {\expandafter\edef\csname #1\endcsname{#2}}%
}
%</language>
%    \end{macrocode}
% \end{macro}
%
% \subsection{The \textsf{pgfcropmarks} package}
%
% No options are parsed.
%
%    \begin{macrocode}
%<*crop>
\ProcessOptions\relax
\RequirePackage{pgfpages}%
%    \end{macrocode}
%
% \noindent No files are created or updated.
%    \begin{macrocode}
\nofiles%
%    \end{macrocode}
%
% \noindent Boolean, true on odd pages.
%    \begin{macrocode}
\newif\if@kus@isodd%
\@kus@isoddtrue%
%    \end{macrocode}
%
% \begin{macro}{\cropmarklen}
% The length of the crop mark lines.
%    \begin{macrocode}
\newlength{\cropmarklen}\setlength{\cropmarklen}{40pt}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cropmarksep}
% The space between cropmark and paper edge.
%    \begin{macrocode}
\newlength{\cropmarksep}\setlength{\cropmarksep}{15pt}%
%    \end{macrocode}
% \end{macro}
%
% \noindent The cropmark page layout.
%    \begin{macrocode}
\pgfpagesdeclarelayout{cropmarks}%
{%
}%
{%
  \pgfpagesphysicalpageoptions%
  {%
    logical pages=1,%
    physical height=297mm,%
    physical width=210mm%
  }%
  \pgfpageslogicalpageoptions{1}%
  {%
    border code=%
      \if@kus@isodd
        \global\@kus@isoddfalse
        \pgfsetlinewidth{0.6pt}%
        \pgfusepath{discard}%
        \pgfmoveto{\pgfpoint{-\cropmarksep}{0pt}}%
          \pgflineto{\pgfpoint{-\cropmarklen}{0pt}}%
        \pgfmoveto{\pgfpoint{0pt}{-\cropmarksep}}%
          \pgflineto{\pgfpoint{0pt}{-\cropmarklen}}%
        \pgfmoveto{\pgfpoint{\paperwidth+\cropmarksep}{0pt}}%
          \pgflineto{\pgfpoint{\paperwidth+\cropmarklen}{0pt}}%
        \pgfmoveto{\pgfpoint{\paperwidth}{-\cropmarksep}}%
          \pgflineto{\pgfpoint{\paperwidth}{-\cropmarklen}}%
        \pgfmoveto{\pgfpoint{-\cropmarksep}{\paperheight}}%
          \pgflineto{\pgfpoint{-\cropmarklen}{\paperheight}}%
        \pgfmoveto{\pgfpoint{0pt}{\paperheight+\cropmarksep}}%
          \pgflineto{\pgfpoint{0pt}{\paperheight+\cropmarklen}}%
        \pgfmoveto{\pgfpoint{\paperwidth}{\paperheight+\cropmarksep}}%
          \pgflineto{\pgfpoint{\paperwidth}{\paperheight+\cropmarklen}}%
        \pgfmoveto{\pgfpoint{\paperwidth+\cropmarksep}{\paperheight}}%
          \pgflineto{\pgfpoint{\paperwidth+\cropmarklen}{\paperheight}}%
        \pgfusepath{stroke}%
      \else
        \global\@kus@isoddtrue
      \fi,%
    center=\pgfpoint{.5\pgfphysicalwidth}{.5\pgfphysicalheight}%
  }%
}%
%    \end{macrocode}
%
% \noindent Activate the cropmark page layout.
%    \begin{macrocode}
\pgfpagesuselayout{cropmarks}%
%</crop>
%    \end{macrocode}
%
% \Finale
\endinput
% vim: ft=tex:sw=2:sts=2:et:nu:ai
