%%
%% This is file `keyvaltable.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% keyvaltable.dtx  (with options: `package')
%% 
%% Copyright (C) 2016 by Richard Gay <gay@mais.informatik.tu-darmstadt.de>
%% 
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.2 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.2 or later is part of all distributions of LaTeX version
%% 1999/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{keyvaltable}
    [2016/05/22 v0.2 Package for filling tables using key-value lists]
\RequirePackage{etoolbox}
\RequirePackage{xkeyval}
\RequirePackage[table]{xcolor}
\RequirePackage{booktabs}
\RequirePackage{longtable,tabu}
\newcommand\kvtSet[1]{\bgroup
  \def\kvt@@presetqueue{\egroup}
  \setkeys[kvt]{defaults}{#1}{}%
  \kvt@@presetqueue}
\newcommand\kvt@lazypreset[2]{%
  \appto\kvt@@presetqueue{\presetkeys[kvt]{#1}{#2}{}}}
\DeclareListParser{\mkv@dossvlist}{;}
\newcommand\kvt@addtableprop[2]{%
  \define@key[kvt]{defaults}{#1}{%
    \kvt@lazypreset{Table}{#1=##1}}%
  \presetkeys[kvt]{defaults}{#1=#2}{}%
  \define@cmdkey[kvt]{Table}{#1}{}%
  \presetkeys[kvt]{Table}{#1=#2}{}}
\newcommand\kvt@addchoicetableprop[3]{%
  \define@choicekey[kvt]{defaults}{#1}{#3}{%
    \kvt@lazypreset{Table}{#1=##1}}%
  \presetkeys[kvt]{defaults}{#1=#2}{}%
  \define@choicekey[kvt]{Table}{#1}{#3}%
    {\csdef{cmdkvt@Table@#1}{##1}}%
  \presetkeys[kvt]{Table}{#1=#2}{}}
\newcommand\kvt@addcolumnprop[2]{%
  \define@key[kvt]{defaults}{#1}{%
    \kvt@lazypreset{Column}{#1=##1}}%
  \presetkeys[kvt]{defaults}{#1=#2}{}%
  \define@key[kvt]{Column}{#1}{%
    \csdef{kvt@col@#1@\kvt@@column}{##1}}%
  \presetkeys[kvt]{Column}{#1=#2}{}%
}
\newcommand\kvt@addchoicecolumnprop[3]{%
  \define@choicekey[kvt]{defaults}{#1}{#3}{%
    \kvt@lazypreset{Column}{#1=##1}}%
  \presetkeys[kvt]{defaults}{#1=#2}{}%
  \define@choicekey[kvt]{Column}{#1}{#3}%
    {\csdef{kvt@col@#1@\kvt@@column}{##1}}%
  \presetkeys[kvt]{Column}{#1=#2}{}%
}
\kvt@addtableprop{rowbg}{white..black!10}
\kvt@addtableprop{headbg}{black!14}
\kvt@addchoicetableprop{shape}{multipage}{multipage,onepage}
\kvt@addcolumnprop{default}{}
\kvt@addcolumnprop{format}{\kvt@struttedcell}
\kvt@addcolumnprop{align}{l}
\kvt@addcolumnprop{head}{}
\kvt@addchoicecolumnprop{hidden}{false}{false,true}
\kvtSet{}
\newcommand\kvt@struttedcell[1]{\strut #1\strut}
\newcommand\NewKeyValTable[3][]{%
  \csdef{kvt@options@#2}{#1}%
  \csdef{kvt@headings@#2}{}%
  \csdef{kvt@alignments@#2}{}%
  \csdef{kvt@colkeys@#2}{}%
  \csdef{kvt@rowcount@#2}{1}%
  \csdef{kvt@rows@#2}{}%
  \listadd\kvt@alltables{#2}%
  \def\do##1{%
    \kvt@parsecolspec{#2}##1::\@undefined}%
  \mkv@dossvlist{#3}%
}
\def\kvt@parsecolspec#1#2:#3:#4\@undefined{%
  \def\kvt@@column{#1@#2}%
  \setkeys[kvt]{Column}{#3}%
  \ifcsstring{kvt@col@hidden@#1@#2}{true}{}{%
    \cseappto{kvt@alignments@#1}{\csexpandonce{kvt@col@align@#1@#2}}%
    \ifcsvoid{kvt@headings@#1}{}{\csappto{kvt@headings@#1}{&}}%
    \ifcsstring{kvt@col@head@#1@#2}{}%
      {\cseappto{kvt@headings@#1}{#2}}%
      {\cseappto{kvt@headings@#1}{\csexpandonce{kvt@col@head@#1@#2}}}%
    \listcsadd{kvt@colkeys@#1}{#2}%
  }%
  \define@cmdkey[KeyValTable]{#1}{#2}[]{}%
  \presetkeys[KeyValTable]{#1}{#2}{}%
}
\newcommand\kvt@alltables{}
\newcounter{kvtRow}
\newcounter{kvtTypeRow}
\newcounter{kvtTotalRow}
\setcounter{kvtTotalRow}{1}
\newcommand\kvtLabel[2]{%
  \protected@edef\@currentlabel
    {\csname p@#1\endcsname\csname the#1\endcsname}%
  \ifstrempty{#2}{}{\label{#2}}%
  \csuse{the#1}}
\newenvironment{KeyValTable}[2][]{%
  \bgroup%
  \def\Row##1{\kvt@AddKeyValRow{#2}{##1}\kvt@@row\\}%
  \kvt@SetOptions{#2}{#1}%
  \csuse{kvt@StartTable@\cmdkvt@Table@shape}{#2}%
}{%
  \csuse{kvt@EndTable@\cmdkvt@Table@shape}%
  \egroup}
\AfterEndEnvironment{KeyValTable}{%
  \csdef{kvt@rowcount@\kvt@@recenttable}{\thekvtTypeRow}}
\newcommand\kvt@SetOptions[2]{%
  \bgroup\edef\kvt@@do{\egroup\noexpand%
    \setkeys[kvt]{Table}%
      {\csexpandonce{kvt@options@#1},\unexpanded{#2}}%
  }\kvt@@do}
\newcommand\kvt@StartTable@onepage[1]{%
  \kvt@StartTabu{tabu}{#1}}
\newcommand\kvt@StartTable@multipage[1]{%
  \kvt@StartTabu{longtabu}{#1}}
\newcommand\kvt@StartTabu[2]{%
  \gdef\kvt@@recenttable{#2}%
  \bgroup\edef\kvt@@do{\egroup
    \noexpand\taburowcolors[2] 2{\cmdkvt@Table@rowbg}%
    \noexpand\begin{#1}{\csuse{kvt@alignments@#2}}%
    \noexpand\toprule
    \noexpand\rowcolor{\cmdkvt@Table@headbg}%
  }\kvt@@do%
  \csuse{kvt@headings@#2}\\\midrule
  \iftabu@long\expandafter\endhead\fi
  \setcounter{kvtRow}{1}%
  \setcounter{kvtTypeRow}{\csuse{kvt@rowcount@#2}}%
  \everyrow{%
    \addtocounter{kvtRow}{1}%
    \addtocounter{kvtTypeRow}{1}%
    \addtocounter{kvtTotalRow}{1}%
  }%
}
\newcommand\kvt@EndTable@onepage{%
  \bottomrule%
  \end{tabu}}
\newcommand\kvt@EndTable@multipage{%
  \bottomrule%
  \end{longtabu}}
\newcommand\kvt@AddKeyValRow[2]{%
  \setkeys[KeyValTable]{#1}{#2}%
  \def\do##1{%
    \ifdefvoid\kvt@@row{}{\appto\kvt@@row{&}}%
    \eappto\kvt@@row{%
      \csexpandonce{kvt@col@format@#1@##1}{%
      \ifcsvoid{cmdKeyValTable@#1@##1}%
        {\csexpandonce{kvt@col@default@#1@##1}}%
        {\csexpandonce{cmdKeyValTable@#1@##1}}}%
    }%
  }\dolistcsloop{kvt@colkeys@#1}%
}
\newcommand\ShowKeyValTable[2][]{%
  \begin{KeyValTable}[#1]{#2}%
    \csuse{kvt@rows@#2}%
  \end{KeyValTable}%
  \csdef{kvt@rows@#2}{}}
\newcommand\AddKeyValRow[2]{%
  \bgroup%
  \kvt@AddKeyValRow{#1}{#2}%
  \csxappto{kvt@rows@#1}{\expandonce{\kvt@@row}\noexpand\\}%
  \egroup}
\newenvironment{KeyValTableContent}[1]{%
  \def\Row{\AddKeyValRow{#1}}}{}%
\endinput
%%
%% End of file `keyvaltable.sty'.
