% \iffalse meta-comment
%
% The MIT License (MIT)
% 
% Copyright (c) 2013 Christian Dietrich
% 
% Permission is hereby granted, free of charge, to any person obtaining a copy of
% this software and associated documentation files (the "Software"), to deal in
% the Software without restriction, including without limitation the rights to
% use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
% the Software, and to permit persons to whom the Software is furnished to do so,
% subject to the following conditions:
% 
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
% 
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
% FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
% IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{dataref.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{dref}
%<package> [2013/12/06 v0.1 dref]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage[usagereport]{dataref}[2013/12/06]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\usepackage{listings}
\usepackage{pdfcomment}
\usepackage{filecontents}
\begin{filecontents}{datapoints.tex}
\drefset{/control group/mice race}{Black Six}
\drefset{/control group/mice count}{32}
\drefset{/control group/dead after 24h}{3}
\drefset{/control group/dead after 48h}{7}
\drefset{/control group/recovered}{6}

\drefset{/med A/mice race}{Black Six}
\drefset{/med A/mice count}{32}
\drefset{/med A/dead after 24h}{6}
\drefset{/med A/dead after 48h}{1}
\drefset{/med A/recovered}{9}

\drefsethelp{.*/mice race}{The mice race used for experiments heavily 
     influences the outcome of the results}

\drefsethelp{.*/(dead after|recovered)}{Of all infected mice, a
  certain number died within a specified period of time. A certain
  recovered from the infection. Each mouse is in exactly one category.}
\end{filecontents}
\input{datapoints}
% \OnlyDescription
\begin{document}
  \DocInput{dataref.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \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{v0.1}{2013/12/06}{Initial version}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \providecommand*{\url}{\texttt}
% \GetFileInfo{dataref.sty}
% \title{The \textsf{dataref} package}
% \author{Christian Dietrich 2013 \url{stettberger@dokucode.de}\\
%        \url{https://github.com/stettberger/dataref}}
% \date{\filedate~\fileversion}
%
% \maketitle
%
% \section{Introduction}
%
% Writing scientific texts is a craft. It is the craft of communicating your results to your colleagues and to the curious world public. Often your conclusions are based upon facts and numbers that you gathered during your research for the specific topic. You might have done many experiments and produced lot of data. The craft of writing is to guide your reader through a narrative that is based upon that data. But there may be many versions of that data. Perhaps you found a problem in your experiment, while already writing, that forces you back into the laboratory. After a while, the moon has done its circle many times, you return from that dark place and your methodology has improved as significantly as your data has. But now you have to rewrite that parts of the data that reference the old data points.
%
% The \textsf{dataref} is here to help you with managing your data points. It provides you with macro style keys that represent symbolic names for your data points. You can reference those symbolic names with \cmd{\dref}, use them in calculations to have always up-to-date percentage values, define projections between sets of data points and document them. \textsf{dataref} also introduces the notion of assertions (\cmd{\drefassert}) for your results to ensure that your prosa text references fit the underlying data.
%
% \section{Usage}
%
% The \textsf{dataref} package heavily uses \textsf{pgfkeys} and \textsf{pgfmath} to perform storage and operations upon data points. See \texttt{texdoc pgfmanual} for further informations about those topics.
%
% \DescribeMacro{\drefset\{\meta{name}\}\{\meta{value}\}}
%
% The \cmd{\drefset} command is used to define the symbolic data points. The first argument is the symbolic name, the second argument is the value. The value can be a number, but it can also be arbitrary text. The key may contain virtually all characters, including spaces and slashes. It is good practice to use a hierarchy to structure you data point names.
%
% \lstinputlisting[language=tex, firstline=5, lastline=15,basicstyle=\footnotesize,frame=single]{datapoints.tex}
%
% The code snippet, which is best stored in an external file, and which might be auto-generated, is best read with \cmd{\input}. It defines 10 symbolic names, that are partitioned into two "directories" (\texttt{control group} and \texttt{medicament A}).
%
% \DescribeMacro{\dref\{\meta{name}\}}
%
% This macro is used to reference a single symbolic data point. The value stored in that datapoint is inserted into the text. \cmd{\dref} additonally marks the data point as used. It will then appear in the dref usage report. For undefined keys the default behaviour is to abort the compilation. But the package option \texttt{ignoremissing} just outputs a warning. All referenced/missing/found datapoints are noted in the aux file.
%
% \vspace{1em}\noindent\begin{tabular}{ll}\toprule
% Macro & Expansion \\
% \midrule
% \cmd{\dref\{/control group/mice race\}} & \dref{/control group/mice race} \\
% \cmd{\dref\{/control group/mice count\}} & \dref{/control group/mice count} \\
% \cmd{\dref[sci,precision=2,zerofill=true]\{/med A/recovered\}} & \dref[sci,precision=2,zerofill=true]{/med A/recovered} \\ \bottomrule
% \end{tabular}\vspace{1em}
%
% \cmd{\dref} additionally takes an optional argument. This argument is interpreted as \texttt{/pgf/number format/} argument. See the pgf/tikz manual for more information. Only if the optional argument is present the value is printed as a number, so \verb|[]| is a useful option, since it enforces printing it as a number with the proper number format applied. Be aware that \cmd{\dref} is \textbf{not} expandable.
%
% \DescribeMacro{\drefvalueof\{\meta{name}\}}
%
% Since \cmd{\dref} is not expandable, this macro can be used to get the bare value of a symbolic data point. But use it with caution, since it bypasses all internal book keeping.
% \begin{quote}
%  \verb|\drefvalueof{/med A/mice race}|
% \end{quote}
%
% \DescribeMacro{\drefref\{\meta{name}\}}
%
% This is complement of \cmd{\drefvalueof}, it does \emph{only} the book keeping for a key (marking it as referenced etc.) So it might be used to compensate the usage of its bad sibling.
% \begin{quote}
%  \verb|\drefref{/med A/mice race}|
% \end{quote}
%
% \DescribeMacro{[ignoremissing]}
% \DescribeMacro{[defaultvalue=1.0]}
% These two package options influence the behaviour regarding unknown
% keys. With \verb|ignoremissing| each missing symbolic datapoint is replaced by the defaultvalue. This behaviour might be useful when you use the .aux file, where the unknown keys are noted to extract data points from a third source (e.g. database, wikidata, etc). In the future a secondary tool will be provided to resolve those references.
%
% \DescribeMacro{\drefsethelp\{\meta{pattern}\}\{\meta{text}\}}
% \DescribeMacro{\drefhelp\{\meta{name}\}}
%
% \textsf{dataref} comes with a simple method for defining documentation for data points. This help can for example be used to communicate what is the concrete semantics of the data point. This is of special interest when writter and data gatherer are not the same person. \cmd{\drefsethelp} takes two arguments: first a regular expression that matches the symbolic data point, second the help text.
%
% \lstinputlisting[language=tex, firstline=17, lastline=18,basicstyle=\footnotesize,frame=single]{datapoints.tex}
%
% The helptext for a key is obtained by using the \cmd{\drefhelp} macro. It checks all defined helps (in linear order, first defined, first matched), and prints the first matching help text.
%
% \begin{quote}
%  \verb|\drefhelp{/med A/mice race}|
% \end{quote}
%
% \DescribeMacro{\drefcalc[\meta{lets}]\{\meta{expr}\}[\meta{format}]}
% 
% The \cmd{\drefcalc} command is the core function of calculating with
% data points. It is based on the pgfmath engine. It uses the required
% argument as a mathematical expression, but has additional features,
% that can be used.
% 
% \begin{quote}
%  \verb|\drefcalc{(4+7)/12 * 100}| $\Rightarrow$ \drefcalc{(4+7)/12 * 100}
% \end{quote}
% 
% \noindent It adds support for the \texttt{data} function within pgfmath, which
% references symbolic data points. The keyname has to be in double
% quotes to indicate a string, but you can easily define an appropriate
% macro that abstracts from \verb|data("")|.
% 
% 
% \begin{quote}
%  \begin{verbatim}\drefcalc{data("/control group/recovered") /
%      data("/control group/mice count")}\end{verbatim} 
%    $\Rightarrow$ \drefcalc{data("/control group/recovered") / data("/control group/mice count")}\\
%  \verb|\newcommand{\cg}[1]{data("/control group/#1")}|\newcommand{\cg}[1]{data("/control group/#1")}\\
%  \verb|\drefcalc{\cg{recovered}/\cg{mice count}}| $\Rightarrow$ \drefcalc{\cg{recovered}/\cg{mice count}}
% \end{quote}
% 
% \noindent The first optional argument lets you define constants within pgfmath
% (zero arity functions, that can be called without parenthesis). Those
% bindings are only valid for the current \cmd{\datarefcalc} call.
% 
% \begin{quote}
%  \verb|\drefcalc[A=\cg{recovered},B=12*20]{B/A}| $\Rightarrow$ \drefcalc[A=data("/control group/recovered"),B=12*20]{B/A}\\
%  \verb|\drefcalc[X=100]{30/X}| $\Rightarrow$ \drefcalc[X=100]{30/X}
% \end{quote}
% 
% \noindent The second optional argument, that appears after the required argument defines the pgfmath's \texttt{number format}.
% \begin{quote}
%  \verb|\drefcalc{1/3}[precision=5,fixed]| $\Rightarrow$ \drefcalc{1/3}[fixed,precision=5]
% \end{quote}
% 
% \DescribeMacro{\drefcalc*}
% \DescribeMacro{\drefresult}
% \DescribeMacro{\drefformat\{\meta{number}\}}
% 
% \noindent When you get confused about the which optional argument does
% what, just think of a pipe. First you define bindings, then you
% calculate, then you emit stuff. When drefcalc is called with an star
% argument, it does not print the result, but only sets
% \cmd{\drefresult}. \cmd{\drefformat} is used to format a number.
% 
% \begin{quote}
%  \verb|\drefcalc*{1/3} ABC: \drefresult| $\Rightarrow$ \drefcalc*{1/3} ABC: \drefresult\\
%  \verb|\drefformat[fixed,precision=1]{\drefresult}|$\Rightarrow$ \drefformat[fixed,precision=1]{\drefresult}\\
%  \verb|\drefformat[sci]{100000}| $\Rightarrow$ \drefformat[sci]{100000}
% \end{quote}
% 
% \DescribeMacro{\dreflet\{\meta{lets}\}}
% 
% The bindings for \cmd{\drefcalc} are only local to that macro
% call. Defining a binding for the current group can be done with
% \cmd{\dreflet}.
% 
% \begin{quote}
%   \verb|\newcommand{\cg}[1]{data("/control group/#1")}|\newcommand{\cg}[1]{data("/control group/#1")}\\
%   \verb|\dreflet{percent=data("/med A/mice count")/100}|\dreflet{percent=data("/med A/mice count")/100}\\
% 
%   The result clearly shows that a lorem ipsum kills\\
%   \verb|\drefcalc{\cg{dead after 24h}/percent}| percent within 24
%   and \verb|\drefcalc{\cg{dead after 28h}/percent}| percent within 48 hours.
% 
%   The result cleary shows that a lorem ipsum kills
%   \drefcalc{\cg{dead after 24h}/percent} percent within 24
%   and \drefcalc{\cg{dead after 48h}/percent} percent within 48 hours.
% \end{quote}
% 
% \DescribeMacro{\drefprojection\{\meta{from}\}\{\meta{to}\}\{\meta{projection}\}}
% 
% Sometimes one or multiple sets of data have to be projected/mixed into a
% new set of data that is fully dependent on those values. This is
% achieved with \cmd{\drefprojection}. It projects one data set (subdirectoy) into
% another one. Tithin the projection three different operations are
% possible: \cmd{\id}, \cmd{\rename} and \cmd{\calc}.
% 
% \drefprojection{/control group}{/projection}{
%       \id{mice race} % identity function
%       \rename{mice count}{count} % renaming of points
%       \calc{data("/dead after 24h")+data("/dead after 48h")}{died}
% }
% 
% \begin{quote}
%   \begin{verbatim}\drefprojection{/control group}{/projection}{
%       \id{mice race} % identity function
%       \rename{mice count}{count} % renaming of points
%       \calc{data("/dead after 24h")+data("/dead after 48h")}{died}
%     }\end{verbatim}
% 
%   \verb|\dref{/projection/died}| $\Rightarrow$ \dref{/projection/died}\\
%   \verb|\dref{/projection/mice race}| $\Rightarrow$ \dref{/projection/mice race}\\
%   \verb|\dref{/projection/count}| $\Rightarrow$ \dref{/projection/count}
% \end{quote}
% 
% \DescribeMacro{\drefrow\{\meta{list}\}\{\meta{macro}\}}
% \DescribeMacro{\drefrow*}
% 
% 
% Often different columns in a table have to be obtained from your data
% points. Often those rows and columns are similar. Generating parts of
% tables within \LaTeX is very tricky, so \textsf{dataref} provides you
% with \cmd{\drefrow}. This macro iterates over a comma-separated list
% of values and fills out a macro which is interpreted as a symbolic
% data point. The entries are seperated with \& and printed. In the
% starred variant the resulting text is not interpreted as symbolic
% name, but as a macro.
% 
% The second argument is the macro, and can have two macro
% replacements. The first replacement \verb|#1| is the value of the list
% item, the second \verb|#2| is the index in the list.
% 
% \begin{verbatim}\begin{tabular}{lccc}
%   Group & $<$ 24h & $<$48h & recovered\\ \hline
%   Control Group & \drefrow{dead after 24h,dead after 48h,recovered}%
%                           {/control group/#1}\\
%   Medicament A & \drefrow{dead after 24h,dead after 48h,recovered}%
%                           {/med A/#1}\\
%   Starred Variant & \drefrow*{B,C,D}{\#1=#1,\#2=#2}\\
% \end{tabular}
% \end{verbatim}
% 
% 
% \vspace{1em}\begin{tabular}{lccc}
%   Group & $<$ 24h & $<$48h & recovered\\ \hline
%   Control Group & \drefrow{dead after 24h,dead after 48h,recovered}%
%                           {/control group/#1}\\
%   Medicament A & \drefrow{dead after 24h,dead after 48h,recovered}%
%                           {/med A/#1}\\\hline
%   Starred Variant & \drefrow*{B,C,D}{\#1=#1,\#2=#2}\\
% \end{tabular}\vspace{1em}
% 
% \DescribeMacro{\drefassert\{\meta{expr}\}}
% \DescribeMacro{[noassert]}
% 
% Sometimes the underlying data changes while you are writing. But what
% if your prose text relies on certain characteristics of the data. \cmd{\drefassert} uses a pgfmath
% expression that evaluates to \verb|true| or \verb|false|. When the
% assertion holds (\verb|true|) nothing happens, only a terminal message
% is printed. When it does not hold (\verb|false|) the compilation is aborted.
% 
% \begin{verbatim}\drefassert{data("/control group/mice count") > 30} 
% Of the more than thirty infected mice...
% \end{verbatim}
% 
% The \textbf{noassert} package options disables the latex abortion. In
% that case only a warning message is printed on the terminal.
% 
% \DescribeMacro{[annotate=none]}
% \DescribeMacro{[annotate=footnote]}
% \DescribeMacro{[annotate=pdfcomment]}
% \DescribeMacro{\drefannotate\{\meta{style}\}}
% 
% 
% While writing a document it is desirable to know, what key is used,
% while writing the text and generating the document. Therefore
% \textsf{dataref} provides the possibility to annotate values. The
% default package option \textbf{none} disables this kind of
% annotation. The \textbf{pdfcomment} option uses pdf
% annotations. Be aware that those annotations work properlyy only on a few
% selected PDF readers\footnote{In doubt use Acrobat}. \cmd{\drefannotate} sets the annoation
% style for the current group.
% 
% \begin{quote}
%   \verb|\drefannotate{none}|\drefannotate{none}\\
%   \dref{/control group/mice race}, \dref{/control group/mice count}, \drefcalc{100/3}
% \end{quote}
% 
% \begin{quote}
%   \verb|\drefannotate{footnote}|\drefannotate{footnote}\\
%   \dref{/control group/mice race}, \dref{/control group/mice count}, \drefcalc{100/3}
% \end{quote}
% 
% \begin{quote}
%   \verb|\drefannotate{pdfcomment}|\drefannotate{pdfcomment}\\
%   \dref{/control group/mice race}, \dref{/control group/mice count}, \drefcalc{100/3}
% \end{quote}
% 
% \DescribeMacro{\drefusagereport}
% \DescribeMacro{[usagereport]}
% 
% With the \textbf{usagereport} package option enabled,
% \cmd{\drefusagereport} generates a usagereport of all referenced
% keys. The usage report groups the keys by the help texts.
% 
% \section*{Datagraphy}
%   \drefusagereport
% \pagebreak
% \StopEventually{}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% Guard against reading twice
%    \begin{macrocode}
\ifx\drefloaded\undefined
  \let\drefloaded=\relax
\else
  \expandafter\endinput
\fi
\ifx\PackageError\undefined
  \def\dref@error#1{\immediate\write-1{Package dref: Error! #1.}}%
\else
  \def\dref@error#1{\PackageError{dref}{#1}{}}%
\fi
% \end{macrocode}
% 
% \begin{macrocode}
\RequirePackage{pgf}
\RequirePackage{kvoptions}
\RequirePackage{xparse}
\RequirePackage{etoolbox}
\RequirePackage{etextools}
%    \end{macrocode}
%    \begin{macrocode}
\SetupKeyvalOptions{
  family=dref,
  prefix=dref@
}
\DeclareStringOption[/data]{datapath}
\DeclareStringOption[1]{defaultvalue}
\DeclareStringOption[none]{annotate}
\DeclareBoolOption{usagereport}
\DeclareBoolOption{ignoremissing}
\DeclareBoolOption{noassert}
\ProcessKeyvalOptions*
%    \end{macrocode}
%
% \begin{macro}{\dref@set}
%    \begin{macrocode}
\newcommand{\dref@set}[2]{%
    \pgfkeys@temptoks{#2}%
    \expandafter\xdef\csname pgfk@\dref@datapath#1\endcsname{\the\pgfkeys@temptoks}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefset}
%    \begin{macrocode}
\def\drefset#1#2{\dref@set{#1}{#2}}
%    \end{macrocode}
% \end{macro}

%
% \begin{macro}{\dref@expandable}
%    \begin{macrocode}
\long\def\dref@expandable#1{%
  \pgfkeysifdefined{\dref@datapath#1}{%
    \pgfkeysvalueof{\dref@datapath#1}%
  }{%
    \dref@defaultvalue%
  }%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dref@unexpandable}
%    \begin{macrocode}
\long\def\dref@unexpandable#1{%
  \def\drefcurrentkey{\dref@datapath#1}%
  \pgfkeysifdefined{\drefcurrentkey}{%
    \immediate\write\@auxout{\noexpand\dref@found{\drefcurrentkey}{\thepage}}%
  }{%
    \immediate\write\@auxout{\noexpand\dref@notfound{\drefcurrentkey}{\thepage}}%
    \ifdref@ignoremissing%
       \typeout{Dref warning: undefined key `\drefcurrentkey'}%
       \dref@mkannotate{UNDEFINED: \drefcurrentkey}%
    \else%
       \dref@error{Dref error: undefined key `\drefcurrentkey'}%
    \fi%
  }%
  \immediate\write\@auxout{\noexpand\dref@referenced{\drefcurrentkey}{\thepage}}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\dref}
%    \begin{macrocode}
\DeclareDocumentCommand{\dref}{o m}{%
  \dref@unexpandable{#2}%
  \IfNoValueTF {#1}{%
    \gdef\dref@dref@output{\dref@expandable{#2}}%
  }{%
    \gdef\dref@dref@output{%
      \pgfmathparse{\dref@expandable{#2}}%
      \dref@format[#1]{\pgfmathresult}%
    }%
  }%
  \dref@dref@output%
  \dref@mkannotate{#2}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefvalueof}
%    \begin{macrocode}
\def\drefvalueof#1{%
  \dref@expandable{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefref}
%    \begin{macrocode}
\def\drefref#1{%
  \dref@unexpandable{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\dref@help@match}
%    \begin{macrocode}
\newcommand{\dref@help@match}[2]{%
  \ifstrmatch{#1}{#2}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dref@help}
%    \begin{macrocode}
\newcommand{\dref@help}[2][]{%
  \pgfkeysifdefined{#2/help}{%
    \pgfkeysvalueof{#2/help}%
  }{#1}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\drefsethelp}
%    \begin{macrocode}
\csdef{dref@helps}{}
\newcommand{\drefsethelp}[2]{
  \csdef{dref@help@#1}{#2}%
  \listcsadd{dref@helps}{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefhelp}
%    \begin{macrocode}
\newcommand{\drefhelp}[1]{
  \renewcommand{\do}[1]{%
    \dref@help@match{##1}{#1}{%
      \csuse{dref@help@##1}%
    \listbreak}{}%
  }%
  \ifcsvoid{dref@helps}{}{%
    \dolistcsloop{dref@helps}%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dref@referenced}
%    \begin{macrocode}
\long\def\dref@notfound#1#2{
  \ifdref@usagereport%
    \dref@usagereport@notfound{#1}{#2}%
  \else\relax\fi%
}
\long\def\dref@found#1#2{
  \ifdref@usagereport%
    \dref@usagereport@found{#1}{#2}%
  \else\relax\fi%
}
\long\def\dref@referenced#1#2{
  \ifdref@usagereport%
    \dref@usagereport@referenced{#1}{#2}%
  \else\relax\fi%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dref@let}
%    \begin{macrocode}
\def\dref@let#1{%
  \def\@tmp##1=##2;{\pgfmathdeclarefunction*{##1}{0}{\pgfmathparse{##2}}}%
  \renewcommand*{\do}[1]{\@tmp##1;}%
  \docsvlist{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dreflet}
%    \begin{macrocode}
\def\dreflet#1{%
  \dref@let{#1}%
}
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\drefcalc}
%    \begin{macrocode}
\DeclareDocumentCommand{\dref@calc}{o m}{%
  \IfNoValueTF {#1}{}{%
    \dref@let{#1}%
  }%
  \pgfmathparse{#2}%
}
\def\drefresult{0.0}
\DeclareDocumentCommand{\drefcalc}{s O{} m O{}}{%
  \begingroup%
  \dref@calc[#2]{#3}%
  \xdef\drefresult{\pgfmathresult}%
  \IfBooleanTF {#1} {}% Wit star do not print anything
               {%
                 \dref@format[#4]{\pgfmathresult}%
                 \dref@mkannotate{#3}%
               }%
  \endgroup%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\drefformat}
%    \begin{macrocode}
\newcommand{\dref@format}[2][]{%
  \pgfmathprintnumber[#1]{#2}%
}
\DeclareDocumentCommand{\drefformat}{O{} m}{%
  \dref@format[#1]{#2}%
}
%    \end{macrocode}
% \end{macro}

%
% \begin{macro}{data()}
%    \begin{macrocode}
\gdef\dref@data@math@prefix{}
\pgfmathdeclarefunction{data}{1}{%
        \begingroup%
                \dref@unexpandable{\dref@data@math@prefix#1}%
                \pgfmathparse{\dref@expandable{\dref@data@math@prefix#1}}%
                \pgfmath@smuggleone\pgfmathresult%
        \endgroup%
}
\DeclareDocumentCommand{\drefprojection}{m m m}{%
  \begingroup%
     \def\dref@data@math@prefix{#1}%
     \def\rename##1##2{\dref@unexpandable{#1/##1}\drefset{#2/##2}{\dref@expandable{#1/##1}}}%
     \def\id##1{\rename{##1}{##1}}%
     \def\calc##1##2{%
       \begingroup%
          \dref@calc{##1}%
          \xdef\dref@project@result{\pgfmathresult}
       \endgroup%
       \drefset{#2/##2}{\dref@project@result}%
      }%
     #3%
     \endgroup%
}
%    \end{macrocode}
% \end{macro}
%

% \begin{macro}{\dref@makerow}
%    \begin{macrocode}

\newtoks\dref@toks

\newcommand{\dref@makerow}[2]{%
  {\global\dref@toks={}%
    \@tempcnta=\z@%
    \def\inner##1##2{#2}%
    \renewcommand*{\do}[1]{%
      \advance\@tempcnta\@ne%
      \csdef{@cell\number\@tempcnta}{\inner{##1}{\number\@tempcntb}}%
    }%
    \expandafter\def\expandafter\arglist\expandafter{#1}%
    \expandafter\docsvlist\expandafter{\arglist}%
    \@tempcntb=\z@
        {\loop\ifnum\@tempcntb<\@tempcnta
          \advance\@tempcntb\@ne
          \edef\next{%
            \ifnum\@tempcntb=\@ne\else&\fi
            \csuse{@cell\number\@tempcntb}}%
          \global\dref@toks=\expandafter{\the\expandafter\dref@toks\next}%
          \repeat}%
  }%
  \the\dref@toks}
\DeclareDocumentCommand{\drefrow}{s m m}{%
  \IfBooleanTF {#1} {%
    \dref@makerow{#2}{#3}%
  }{% Wit star do not print anything
    \dref@makerow{#2}{\dref[]{#3}}%
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dref@mkannotate}
%    \begin{macrocode}

\expandafter\ifstrequal\expandafter{\dref@annotate}{pdfcomment}{
  \RequirePackage{pdfcomment}
}

\newcommand{\dref@mkannotate}[1]{%
  \expandafter\ifstrequal\expandafter{\dref@annotate}{none}%
    {\relax}%
    {\expandafter\ifstrequal\expandafter{\dref@annotate}{footnote}%
      {\footnote{#1}}%
      {\expandafter\ifstrequal\expandafter{\dref@annotate}{pdfcomment}%
        {\pdfcomment[opacity=0.4,voffset=2ex]{#1}}%
        {\dref@error{Value for annotate not supported: '\dref@annotate'}%
          }}}}%

\newcommand{\drefannotate}[1]{%
  \renewcommand{\dref@annotate}{#1}%
}
%    \end{macrocode}
% \end{macro}
%

%% Usagereport
%    \begin{macrocode}
\ifdref@usagereport
  \RequirePackage{longtable}
  \RequirePackage{booktabs}
\fi
%    \end{macrocode}
%
% \begin{macro}{\dref@usagereport@referenced}
%    \begin{macrocode}
\newcommand{\dref@usagereport@notfound}[2]{}
\newcommand{\dref@usagereport@found}[2]{}

\csdef{pgfdat@usagereport@keys}{}
\csdef{pgfdat@usagereport@matchedkeys}{}

\newcommand{\dref@usagereport@referenced}[2]{
  \ifinlistcs{#2}{dref@usagereport@referenced@#1}{}{
    \listcsgadd{dref@usagereport@referenced@#1}{#2}
  }
  \ifinlistcs{#1}{dref@usagereport@keys}{}{
    \listcsgadd{dref@usagereport@keys}{#1}
  }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\dref@usagereport@strippath}
%    \begin{macrocode}
\expandafter\def\expandafter\dref@usagereport@strippath@\dref@datapath#1\blanktest{#1}

\newcommand{\dref@usagereport@strippath}[1]{%
  \expandafter\ifstrmatch\expandafter{\expandafter^\dref@datapath.*$}{#1}%
    {\dref@usagereport@strippath@#1\blanktest}%
    {#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dref@usagereport@formatreferencelist}
%    \begin{macrocode}
\newcommand{\dref@usagereport@formatreferencelist}[1]{%
  \begingroup%
  \def\sep{}%
  \renewcommand{\do}[1]{\sep\ifdef{\hyperlink}{\hyperlink{page.##1}{##1}}{##1}\def\sep{, }}%
  \dolistcsloop{dref@usagereport@referenced@#1}%
  \endgroup%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dref@usagereport@keyheader}
%    \begin{macrocode}
\newcommand{\dref@usagereport@keyheader}[1]{%
  \textbf{\ifdef{\hypertarget}%
    {\hypertarget{#1}{\dref@usagereport@strippath{#1}}}%
    {\dref@usagereport@strippath{#1}}}%
  & \dref@usagereport@formatreferencelist{#1}%
  & \pgfkeysifdefined{#1}{\pgfkeysvalueof{#1}}{\textbf{\red{undefined}}} \\%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dref@usagereport@forhelp}
%    \begin{macrocode}
\newcommand{\dref@usagereport@forhelp}[1]{%
  \begingroup%
  \noindent\csuse{dref@help@#1}
  \renewcommand{\do}[1]{%
    \dref@help@match{#1}{##1}{%
      \dref@usagereport@keyheader{##1}%
      \ifinlistcs{##1}{dref@usagereport@matchedkeys}{}{%
        \listcsgadd{dref@usagereport@matchedkeys}{##1}%
      }%
    }{}%
  }%
  \begin{longtable}{lll}\toprule%
    & Page(s) & Value \\ \midrule
  \dolistcsloop{dref@usagereport@keys}%
  \end{longtable}%
  \endgroup%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\dref@usagereport@withouthelp}
%    \begin{macrocode}
\newcommand{\dref@usagereport@withouthelp}{%
  \renewcommand{\do}[1]{%
    \ifinlistcs{##1}{dref@usagereport@matchedkeys}{}{%
      \dref@usagereport@keyheader{##1}%
    }%
  }%
  \begin{longtable}{lll}\toprule%
    Keys without Help  & Page(s) & Value \\\midrule
    \endhead
    \dolistcsloop{dref@usagereport@keys}%
  \end{longtable}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefusagereport}
%    \begin{macrocode}
\newcommand{\drefusagereport}{%
  \ifdref@usagereport%
  \ifcsvoid{dref@usagereport@keys}{\typeout{EMPTY}}{%
  \begingroup%
  \setlength{\LTleft}{2em}%
  \setlength{\LTright}{0pt}%
  \renewcommand{\do}[1]{%
    \ifinlistcs{##1}{dref@usagereport@matchedkeys}{}{%
      \dref@usagereport@forhelp{##1}%
    }%
  }%
  \dolistcsloop{dref@helps} % For all help text
  \setlength{\LTleft}{0em}%
  \dref@usagereport@withouthelp\relax
  \endgroup%
  }% csempty @keys
  \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\drefassert}
%    \begin{macrocode}
\newcommand{\drefassert}[1]{%
  \begingroup%
    \pgfmathsetmacro{\result}{(#1) ? 1 : 0}
    \expandafter\ifstrequal\expandafter{\result}{1.0}{%
      \typeout{Assertion holds: #1}%
    }{%
      \ifdref@noassert%
        \typeout{Assertion failed: #1}%
      \else%
        \dref@error{Assertion failed: #1}%
      \fi%
   }%
  \endgroup%
}
%    \end{macrocode}
% \end{macro}
%
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput
