0

This is a follow up question on two other questions I am struggeling with:

A couple of things I could figure out, but I still struggle with the final delimiter and displaying more than one name.

Update

With changing to datatool completely I was able to get it almost done. But still after the second item I get ,\space which is not wanted. It works smoothly for one entry:

enter image description here

\NewDocumentCommand{\PIsList}{ m }{%
  \DTLforeach*{PI}{\id=ID}{%
    \expandafter\DTLifinlist\expandafter{\id}{#1}%
    {\DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id}\thePIname\DTLiflastrow{}{,\space}}
    {}%
}}

complete MWE:

\documentclass{scrbook}

\usepackage{datatool,xspace}
% ===== people database setup =======
\makeatletter
\define@key{person}{name}{\DTLnewdbentry{\crcDB}{name}{#1}}
\define@key{person}{ID}{\DTLnewdbentry{\crcDB}{ID}{#1}}
\makeatother

% ===== project database setup
\makeatletter
\define@key{project}{ID}{\DTLnewdbentry{\crcDB}{ID}{#1}}
\define@key{project}{pi}{\DTLnewdbentry{\crcDB}{pi}{#1}}
\makeatother

% Load entries into databases
\NewDocumentCommand{\crcEntry}{ o m m }{%
  \def\crcDB{#2}%
  \DTLifdbexists{\crcDB}{}{\DTLnewdb{\crcDB}}%
  \DTLnewrow{\crcDB}%
  \IfNoValueTF{#1}{\setkeys{#2}{#3}}{\setkeys{#1}{#3}}%
}


\NewDocumentCommand{\PIsList}{ m }{%
  \DTLforeach*{PI}{\id=ID}{%
    \expandafter\DTLifinlist\expandafter{\id}{#1}%
    {\DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id}\thePIname\DTLiflastrow{}{,\space}}
    {}%
}}


\NewDocumentCommand{\crcProPI}{ m }{%
   \DTLgetvalueforkey{\projectPIsList}{pi}{project}{ID}{#1}%
    \textbf{%
     #1\space%
      (\PIsList{\projectPIsList})}\xspace%
}
% in case someone misspelled the command
% this is just a fallback
\let\crcpropi\crcProPI



\begin{document}

\crcEntry[project]%<-- keyset
{project}%<-- databse
{%
  ID={A01},
  pi={person1,person2}
}
\crcEntry[project]%<-- keyset
{project}%<-- databse
{%
  ID={A02},
  pi={person3}
}

\crcEntry[person]{PI}{%
  ID = {person1},
  name={Perssonos},
}

\crcEntry[person]{PI}{%
  ID = {person2},
  name={Holgersson},
}

\crcEntry[person]{PI}{%
  ID = {person3},
  name={Ickstein},
}



\crcpropi{A01} % --> A01 (Perssonos, Holgersson)

\crcpropi{A02} % -> A02 (Ickstein)

\end{document}

1 Answer 1

1

Looking at https://tex.stackexchange.com/a/108731/98739 I was able to adapt:

\newcommand*{\uniquePI}{}

\NewDocumentCommand{\PIsList}{ m }{%
  \DTLforeach*{PI}{\id=ID}{%
    \expandafter\DTLifinlist\expandafter{\id}{#1}%
    {\DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id}%
    \ifdefempty{\uniquePI}%
    {\let\uniquePI\thePIname}% first element of list
    {% append to list
      \eappto\uniquePI{,\space\thePIname}%
    }}
    {}%
}\uniquePI}

enter image description here

Same result can be achieved using expl3-Syntax (credits go to gusbrs (see comments):

\ExplSyntaxOn
\NewDocumentCommand{\PIsList}{ m }
{
  \seq_clear:N \l_tmpa_seq
  \DTLforeach*{PI}{\id=ID}
  {
    \exp_args:Nx \DTLifinlist {\id}{#1}
    {
      \DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id}
      \seq_put_left:NV \l_tmpa_seq \thePIname
    }
    {}
  }
  \seq_use:Nn \l_tmpa_seq { ,~ }
}
\ExplSyntaxOff
7
  • 1
    Ah, you figured out the datatool part, which I don't know well. Now that you did, you can try \NewDocumentCommand{\PIsList}{ m } {\seq_clear:N \l_tmpa_seq \DTLforeach*{PI}{\id=ID} {\exp_args:Nx \DTLifinlist {\id}{#1} {\DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id} \seq_put_left:NV \l_tmpa_seq \thePIname} {}} \seq_use:Nn \l_tmpa_seq { ,~ }}
    – gusbrs
    Apr 6, 2022 at 12:37
  • Wow, your solution is about 1 sec quicker compiling the document. you should post it as an answer as well. Apr 6, 2022 at 12:44
  • 1
    Please feel free to include it in yours, you did the hard part. ;-)
    – gusbrs
    Apr 6, 2022 at 12:46
  • The only issue I have now is the sorting of the names, they are according to their appearence in the database (row) and not in the field pi. But that is something due to datatool... Apr 6, 2022 at 13:08
  • You can sort that sequence. See \seq_sort:Nn in texdoc interface3. (Though probably not trivial if non-ascii characters are involved, which I guess might be the case...).
    – gusbrs
    Apr 6, 2022 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .