% \iffalse meta-comment
% An Infrastructure for managing addresses and affiliations in LaTeX
% Copyright (c) 2011 Michael Kohlhase, all rights reserved
%               this file is released under the
%               LaTeX Project Public License (LPPL)
% The original of this file is in the public repository at 
% http://github.com/KWARC/sTeX/
% \fi
% 
% \iffalse
%<*package>
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{workaddress}[2019/03/20 v0.5 WorkAddress]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[utf8]{inputenc}
\usepackage{workaddress,sref,url,array,float}
\usepackage[show]{ed}
\usepackage[hyperref=auto,style=alphabetic]{biblatex}
\addbibresource{kwarcpubs.bib}
\addbibresource{extpubs.bib}
\addbibresource{kwarccrossrefs.bib}
\addbibresource{extcrossrefs.bib}
\usepackage{ctangit}
\usepackage{hyperref}
\usepackage{stex-logo}
\makeindex
\floatstyle{boxed}
\newfloat{exfig}{thp}{lop}
\floatname{exfig}{Example}
\WAperson[id=jdoe,affiliation=dfki,department=skss,
           url=http://dfki.de/jdoe]
          {John Doe}
\WAperson[id=miko,affiliation=jacu,department=case,
           url=http://kwarc.info/kohlhase]
          {Michael Kohlhase}
\WAinstitution[id=case,acronym=CASE,
                url=http://jacobs-university.de/ses/case,partof=jacu]
               {Center for Advanced Systems Engineering}
\WAinstitution[id=jacu,url=http://jacobs-university.de,shortname=Jacobs University,acronym=JU]
               {Jacobs University Bremen}
\WAinstitution[id=skss,url=http://dfki.de/sks,partof=dfki,acronym=SKS,]
               {Safe and Secure Cognitive Systems}
\WAinstitution[id=dfki,url=http://dfki.de,acronym=DFKI,shortname=DFKI]
               {German Research Center for Artificial Intelligence}
\def\githubissue#1{\cite{sTeX:github:on}, \hyperlink{https://github.com/KWARC/sTeX/issues/#1}{issue #1}}
\begin{document}
\RecordChanges
\DocInput{workaddress.dtx}
\end{document}
%</driver>
% \fi
%
%\CheckSum{334}
%
% \changes{v0.4}{2011/11/04}{Extracting from dcm.sty}
% \changes{v0.4}{2012/01/18}{new functionality for logos}
% \changes{v0.5}{2016/07/06}{giving \texttt{\textbackslash wp@ref} a first argument for errors}
% 
% \GetFileInfo{workaddress.sty}
% 
% \MakeShortVerb{\|}
% \def\scsys#1{{{\sc #1}}\index{#1@{\sc #1}}}
% \def\latexml{\scsys{LaTeXML}}
%
%   \title{{\texttt{workaddress.sty}}: An Infrastructure for managing Addresses and
%   Affiliations in {\LaTeX}\thanks{Version {\fileversion} (last revised {\filedate})}}
%   \author{\WAauthorblock[aff,url]{miko,jdoe}}
%   \maketitle
%   \begin{abstract}The |workaddress| package allows manage addresses and Affiliations in
%     a bib\TeX-like manner.\ednote{continue}
%   \end{abstract}
%
% \setcounter{tocdepth}{2}\tableofcontents\newpage
%
% \section{Introduction}\label{sec:intro}
%
% The |workaddress| package allows manage Addresses and affiliations of persons in a
% bib\TeX-like manner.\ednote{continue}
% 
% \section{The User Interface}\label{sec:user}
% 
% \subsection{Package Options}\label{sec:user.options}
% 
% The |workaddress| package takes a single option: \DescribeMacro{showmeta}|showmeta|. If
% this is set, then the metadata keys are shown (see~\cite{Kohlhase:metakeys:ctan} for
% details and customization options).
%
% \subsection{Database Entries for Persons}
% 
% The |workaddress| package recognizes that from a metadata perspective, persons are complex
% entities. In particular, specifying metadata is a tedious and repetitive task that leads
% to embarrassing errors. Therefore the |workaddress| package takes a hint from bibTeX and allows
% to specify personal metadata in a database and use it by a database key.
% \begin{exfig}[ht]
% \begin{verbatim}
% \WAperson[id=jdoe,affiliation=dfki,department=skss,
%            url=http://dfki.de/jdoe]
%           {John Doe}
% \WAperson[id=miko,affiliation=jacu,department=case,
%            url=http://kwarc.info/kohlhase]
%           {Michael Kohlhase}
% \end{verbatim}
% \caption{A small database of Persons}
% \label{fig:persons}
% \end{exfig}
% The \DescribeMacro{\WAperson}|\WAperson| macro allows to specify personal metadata\ednote{This
% should be synchronized with the FOAF specification~\cite{FOAF:spec}} with the following
% keys: 
% \begin{center}
% \begin{tabular}{|l|l|l|}\hline
% key & value & comment\\\hline\hline
% id & string & identifier of this person\\\hline
% birthdate & date & birthdate \\\hline
% email & & the primary e-mail address\\\hline
% url & URI & primary home page \\\hline
% affiliation & Inst. identifier & the primary professional affiliation\\\hline
% personaltitle & string & the personal title e.g. {\texttt{King}}\\\hline
% academictitle & string & the academic title e.g. {\texttt{Prof. Dr.}}\\\hline
% department & Inst. identifier & the department specified in the work address\\\hline
% workaddress & long string & the work address \\\hline
% privaddress & long string & the private address \\\hline
% worktel & string & work telephone number \\\hline
% privtel & string & private telephone number\\\hline
% workfax & string & work fax number \\\hline
% privfax & string & private fax number \\\hline
% worktelfax & string & if the phone and fax share a prefix, give this as well \\\hline
% privtelfax & string & dito\\\hline
% \end{tabular}
% \end{center}
% In Figure~\ref{fig:persons} we have specified (minimal) metadata for the authors of the
% |workaddress| package. The metadata can be accessed by specifying the identifiers (given by the
% |id| key) in the |workaddress| macros defined below, see for instance the |\WAcreators| macro
% in Figure~\ref{fig:workaddressblock}, which leads to the title block of this note.
% 
% Like in bibTeX~\cite{Patashnik:b88}, it is a good idea to collect the metadata in a separate
% file that is input in the document. In practice it may be possible to generate these
% files from conventional address databases.
% 
% \subsection{Institutions}
% 
% Institutions are treated analogously to persons.
% \begin{exfig}[ht]
% \begin{verbatim}
% \WAinstitution[id=case,partof=jacu,acronym=CASE,
%                 url=http://jacobs-university.de/ses/case]
%                {Center for Advanced Systems Engineering}
% \WAinstitution[id=jacu,url=http://jacobs-university.de]
%                {Jacobs University Bremen}
% \WAinstitution[id=skss,partof=dfki,url=http://dfki.de/sks,acronym=SKS]
%                {Safe and Secure Cognitive Systems}
% \WAinstitution[id=dfki,url=http://dfki.de,shortname=DFKI,acronym=DFKI]
%                {German Research Center for Artificial Intelligence}
% \end{verbatim}
% \caption{A small Database of Institutions and their Parts}
% \label{fig:institutions}
% \end{exfig}
% The \DescribeMacro{\WAinstitution}|\WAinstitution| macro allows to specify personal
% metadata\ednote{This should be synchronized with the FOAF
% specification~\cite{FOAF:spec}} with the following keys:
% \begin{center}
% \begin{tabular}{|l|l|l|}\hline
% key & value & comment\\\hline\hline
% id & string & identifier of this person\\\hline
% url & URI & primary home page \\\hline
% partof & Inst. identifier & parent institution\\\hline
% \end{tabular}
% \end{center}
% 
% \subsection{Applications}\label{sec:user.appl}
% 
% The data from the address database can be used in various ways. For instance, the
% \DescribeMacro{\WAauthorblock}|\WAauthorblock| macro creates a block of users and their
% affiliations. In the context of the database from Figures~\ref{fig:persons}
% and~\ref{fig:institutions}, |\WAauthorblock{miko,jdoe}| creates
% \begin{center}\WAauthorblock{miko,jdoe}\end{center}
%
% \DescribeMacro{\wa@institution@logo}|\wa@institution@logo| creates the logo of an
% institution from the database, and (if that is not there create a box and a message
% instead.)
% 
% \section{Limitations}\label{sec:limitations}
% 
% In this section we document known limitations. If you want to help alleviate them,
% please feel free to contact the package author. Some of them are currently discussed in
% the \sTeX GitHub repository~\cite{sTeX:github:on}. 
% \begin{enumerate}
% \item none reported yet
% \end{enumerate}
% 
% \StopEventually{\printbibliography}
% 
% \section{The Implementation}\label{sec:impl}
%
% \subsection{Package Options}\label{sec:impl.options}
% The first step is to declare (a few) package options that handle whether certain
% information is printed or not. They all come with their own conditionals that are set by
% the options.
%
%    \begin{macrocode}
%<*package>
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{sref}}
\ProcessOptions
%    \end{macrocode}
%
% The first measure is to ensure that the |KeyVal| package is loaded (in the right
% version). For {\latexml} we also initialize the package inclusions. 
%    \begin{macrocode}
\RequirePackage{sref}
%    \end{macrocode}
%
% \subsection{Persons}
% 
% To implement the |\WAperson| macro, we need to implement its keywords.
%
%    \begin{macrocode}
\addmetakey*{wa@person}{id}
\addmetakey*{wa@person}{birthdate}
\addmetakey*{wa@person}{email}
\addmetakey*{wa@person}{url}
\addmetakey*{wa@person}{affiliation}
\addmetakey*{wa@person}{personaltitle}
\addmetakey*{wa@person}{academictitle}
\addmetakey*{wa@person}{department}
\addmetakey*{wa@person}{workaddress}
\addmetakey*{wa@person}{privaddress}
\addmetakey*{wa@person}{worktel}
\addmetakey*{wa@person}{privtel}
\addmetakey*{wa@person}{workfax}
\addmetakey*{wa@person}{privfax}
\addmetakey*{wa@person}{worktelfax}
\addmetakey*{wa@person}{privtelfax}
%    \end{macrocode}
% 
% \begin{macro}{\wa@def}
%   The next macro is an auxiliary one that puts the value into an appropriate token
%   register. 
%    \begin{macrocode}
\def\wa@def#1#2#3#4{\expandafter\xdef\csname wa@#1@#2@#3\endcsname{#4}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\wa@ref}
%   This macro looks up the information specified in the three last arguments. The first
%   argument determines the behavior if that information is undefined: |\wa@ref0| fails
%   silently (no output, no message), |\waref1| raises a warning but outputs nothing, and
%   |\wa@ref2{|\meta{a}|}{|\meta{b}|}{|\meta{c}|}| raises a warning and outputs
%   |?|\meta{a}|?|\meta{b}|?|\meta{b}|?|, Finally |\wa@ref3| gives an error and aborts.
%    \begin{macrocode}
\newcommand\wa@ref[4]{%
  \@ifundefined{wa@#2@#3@#4}{%
    \ifcase#1 {}\or%
    \PackageWarning{workaddress}{reference to undefined #4 of #2 #3\MessageBreak%
      you must define a #2 with #3=#4\MessageBreak% 
      via the macro \protect\WA#2, before you can use it!}%
    \or
    \PackageWarning{workaddress}{reference to undefined #4 of #2 #3\MessageBreak%
      you must define a #2 with #3=#4\MessageBreak% 
      via the macro \protect\WA#2, before you can use it!}%
      ?#2?#3?#4?
    \or
    \PackageError{workaddress}{reference to undefined #4 of #2 #3}%
    {you must define a #2 with #3=#4\MessageBreak% 
     via the macro \protect\WA#2, before you can use it!}
    \else\PackageError{workaddress}{\protect\wa@ref#1 not defined}{}
   \fi
  }{\csname wa@#2@#3@#4\endcsname}}%
%    \end{macrocode}
% \end{macro}
% 
% With this we can define the |\WAperson| macro, it just clears the keys, sets them
% again, and stores them in token registers. If course only if a |id| attribute is given,
% else we raise an error.
% 
% \begin{macro}{WAperson}
%    \begin{macrocode}
\let\wa@persons=\relax
\newcommand\WAperson[2][]{%
  \metasetkeys{wa@person}{#1}%
  \ifx\wa@person@id\@empty%
    \PackageWarning{workaddress}{key 'id' undefined in WAperson}%
  \else%
    \wa@def{person}\wa@person@id{id}{\wa@person@id}% redundant, but useful for checking
    \wa@def{person}\wa@person@id{name}{#2}
    \wa@def{person}\wa@person@id{email}{\wa@person@email}
    \wa@def{person}\wa@person@id{birthdate}{\wa@person@birthdate}
    \wa@def{person}\wa@person@id{url}{\wa@person@url}
    \wa@def{person}\wa@person@id{affiliation}{\wa@person@affiliation}
    \wa@def{person}\wa@person@id{workaddress}{\wa@person@workaddress}
    \wa@def{person}\wa@person@id{privaddress}{\wa@person@privaddress}
    \wa@def{person}\wa@person@id{personaltitle}{\wa@person@personaltitle}
    \wa@def{person}\wa@person@id{academictitle}{\wa@person@academictitle}
    \wa@def{person}\wa@person@id{department}{\wa@person@department}
    \wa@def{person}\wa@person@id{workaddress}{\wa@person@workaddress}
    \wa@def{person}\wa@person@id{privaddress}{\wa@person@privaddress}
    \wa@def{person}\wa@person@id{worktel}{\wa@person@worktel}
    \wa@def{person}\wa@person@id{privtel}{\wa@person@privtel}
    \wa@def{person}\wa@person@id{workfax}{\wa@person@workfax}
    \wa@def{person}\wa@person@id{privfax}{\wa@person@privfax}
    \wa@def{person}\wa@person@id{worktelfax}{\wa@person@worktelfax}
    \wa@def{person}\wa@person@id{privtelfax}{\wa@person@privtelfax}
    \@ifundefined{wa@persons}{%
      \xdef\wa@persons{\wa@person@id}%
    }{%
      \xdef\wa@persons{\wa@persons,\wa@person@id}%
    }%
  \fi%
}%
\newcommand\DCMperson[2][]{%
  \WAperson[#1]{#2}%
  \PackageWarning{workaddress}{\protect\DCMperson\space is deprecated, use \protect\WAperson\space instead}
}%
%    \end{macrocode}
% \end{macro}
%
% \subsection{Institutions}
% 
% To implement the |\WAinstitution| macro, we need to implement its keywords first.
%
%    \begin{macrocode}
\addmetakey*{wa@institution}{id}
\addmetakey*{wa@institution}{shortname}
\addmetakey*{wa@institution}{acronym}
\addmetakey*{wa@institution}{url}
\addmetakey*{wa@institution}{partof}
\addmetakey*{wa@institution}{countryshort}
\addmetakey*{wa@institution}{logo}
\addmetakey*{wa@institution}{streetaddress}
\addmetakey*{wa@institution}{townzip}
\addmetakey*{wa@institution}{type}
\addmetakey*{wa@institution}{country}
%    \end{macrocode}
% and we proceed as for |\WAperson|, 
%    \begin{macrocode}
\let\wa@institutions=\relax
%    \end{macrocode}
%
% \begin{macro}{WAinstitution}
%    \begin{macrocode}
\newcommand\WAinstitution[2][]{%
  \metasetkeys{wa@institution}{#1}%
  \ifx\wa@institution@id\@empty%
    \PackageWarning{workaddress}{key 'id' undefined in WAinstitution}%
  \else%
    \wa@def{institution}\wa@institution@id{id}{\wa@institution@id}% redundant, but useful for checking
    \wa@def{institution}\wa@institution@id{name}{#2}
    \wa@def{institution}\wa@institution@id{shortname}{\wa@institution@shortname}
    \wa@def{institution}\wa@institution@id{acronym}{\wa@institution@acronym}
    \wa@def{institution}\wa@institution@id{url}{\wa@institution@url}
    \wa@def{institution}\wa@institution@id{partof}{\wa@institution@partof}
    \wa@def{institution}\wa@institution@id{countryshort}{\wa@institution@countryshort}
    \wa@def{institution}\wa@institution@id{logo}{\wa@institution@logo}
    \wa@def{institution}\wa@institution@id{townzip}{\wa@institution@townzip}
    \wa@def{institution}\wa@institution@id{streetaddress}{\wa@institution@streetaddress}
    \wa@def{institution}\wa@institution@id{country}{\wa@institution@country}
    \wa@def{institution}\wa@institution@id{type}{\wa@institution@type}
    \@ifundefined{wa@institutions}{%
      \xdef\wa@institutions{\wa@institution@id}%
    }{%
      \xdef\wa@institutions{\wa@institutions,\wa@institution@id}%
    }%
  \fi%
}%
\newcommand\DCMinstitution[2][]{%
  \WAinstitution[#1]{#2}%
  \PackageWarning{workaddress}{\protect\DCMinstitution\space is deprecated, use \protect\WAinstitution\space instead}%
}%
%    \end{macrocode}
% \end{macro}
% 
%\subsection{Applications}\label{sec:impl.appl}
%
% \begin{macro}{\WAauthorblock}
%   This internal macro builds an author block from a list of |\WAperson| labels in
%   |\wa@creators|.
%    \begin{macrocode}
\addmetakey[false]{WAauthorblock}{dept}[true]
\addmetakey[false]{WAauthorblock}{aff}[true]
\addmetakey[false]{WAauthorblock}{url}[true]
\def\@true{true}
\newcounter{authors}
\newcommand\WAauthorblock[2][]{%
  \metasetkeys{WAauthorblock}{#1}
  {\let\tabularnewline\relax
   \@for\@I:=#2\do{\stepcounter{authors}}
   \def\@authors{}%
   \def\@affs{}%
   \def\@depts{}%
   \def\@urls{}%
   \@for\@I:=#2\do{%
     \xdef\@authors{\@authors&\wa@ref2{person}\@I{name}}
     \xdef\@@dept{\wa@ref1{person}\@I{department}}
     \xdef\@shortname{\csname wa@institution@\@@dept @shortname\endcsname}
     \xdef\@dept{\ifx\@shortname\@empty\wa@ref0{institution}\@@dept{name}\else\@shortname\fi}
     \xdef\@depts{\@depts&\@dept}
     \xdef\@@aff{\wa@ref0{person}\@I{affiliation}}
     \xdef\@shortname{\csname wa@institution@\@@aff @shortname\endcsname}
     \xdef\@aff{\ifx\@shortname\@empty\wa@ref0{institution}\@@aff{name}\else\@shortname\fi}
     \xdef\@affs{\@affs&\@aff}
     \xdef\@urls{\@urls&\wa@ref0{person}\@I{url}}
   }%
   \message{\theauthors authors: \@authors}%
  }%
  \begin{tabular}[t]{l*{\theauthors}{c}}
    \@authors\\
    \ifx\WAauthorblock@dept\@true\@depts\\\fi
    \ifx\WAauthorblock@aff\@true\@affs\\\fi
    \ifx\WAauthorblock@url\@true\@urls\\\fi
  \end{tabular}
}%
%    \begin{macrocode}
% \end{macro}
%
% \begin{macro}{\wapname}
%    \begin{macrocode}
\newcommand\wapname[1]{\wa@ref3{person}{#1}{name}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\waptname}
%    \begin{macrocode}
\newcommand\waptname[1]{\wa@ref3{person}{#1}{personaltitle} \wa@ref3{person}{#1}{name}}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\wa@institution@logo}
%    \begin{macrocode}
\newcommand\wa@institution@logo[2][]{%
  \IfFileExists{\wa@ref2{institution}{#2}{logo}}{%
    \includegraphics{\wa@ref2{institution}{#2}{logo}}%
  }{%
    \fbox{#2 logo}\message{still need logo for #2}%
  }%
}%
%</package>
%    \end{macrocode}
% \end{macro}
%
% \Finale
\endinput
% \iffalse
%%% Local Variables: 
%%% mode: doctex
%%% TeX-master: t
%%% End: 
% \fi

% LocalWords:  RequirePackage birthdate personaltitle academictitle workaddress
% LocalWords:  privaddress worktel privtel workfax privfax worktelfax noDelim
% LocalWords:  privtelfax getKeyValue valuelist ToString getValue affill STDERR
% LocalWords:  ExportMetadata AssignValue WAperson DefConstructor afterDigest
% LocalWords:  getArg setValue FishOutMetadata keyvals foreach idlist tabline
% LocalWords:  LookupValue insertElement atabline bitabline shorttitle nc args
% LocalWords:  sharealike noderivativeworks DefMacro authorblock subsubsection
% LocalWords:  contribs OptionalKeyVals omgroup omdoc srcref xml RawTeX partof
% LocalWords:  openElement iffalse kohlhase Thu scsys sc sc latexml 
% LocalWords:  maketitle WAtitle texttt fileversion WAcreators miko jdoe impl
% LocalWords:  WAabstract setcounter tocdepth tableofcontents newpage dmt03
% LocalWords:  WAsection ednote WAsubsection exfig hline dcmblock Patashnik
% LocalWords:  DescribeEnv WAcontributors WAshorttitle WAshorttitle WAdate
% LocalWords:  WAsubject WAsubject WAdescription WAdescription WApublisher
% LocalWords:  WApublisher WAdate WAtype WAtype WAidentifier WAidentifier
% LocalWords:  WAsource WAsource WAlanguage WAlanguage WArelation ctancite
% LocalWords:  WArelation WArights WArights WAlicense WAlicense titlepage
% LocalWords:  WAlicensenotice WAlicensenotice WAcopyrightnotice titlepage
% LocalWords:  WAcopyrightnotice WAcclicense WAcclicense user.blockstyles
% LocalWords:  WAchapter WAchatper WAsubsubsection WAsubsubsection ltxml
% LocalWords:  user.conig makeatletter printbibliography expandafter showmeta
% LocalWords:  xdef csname endcsname newcommand ifx ifundefined affs showmeta
% LocalWords:  Semiverbatim whatsit newenvironment mtabline providecommand vfil
% LocalWords:  WAsubtitle cclicense defdcm impl.blockstyles newcounter vskip
% LocalWords:  stepcounter tabularnewline theauthors lineskip textbf noindent
% LocalWords:  omd omd srefaddidkey thechapter autoclose thesection ifnum ifnum
% LocalWords:  thesubsection thesubsubsection WAparagraph ISOtimestamp doctex
% LocalWords:  WAinstitution WAinstitution compactenum textsf langle textsf
% LocalWords:  langle metakeys addmetakey metasetkeys countryshort townzip aff
% LocalWords:  streetaddress depts
