这是我正在努力解决的另外两个问题的后续问题:
有几件事我可以弄清楚,但我仍然在努力确定最后的分隔符并显示多个名称。
更新
通过彻底改变,datatool
我能够得到它几乎完成。但在第二项之后,我还是得到了,\space
不想要的东西。它对于一个条目运行顺利:
\NewDocumentCommand{\PIsList}{ m }{%
\DTLforeach*{PI}{\id=ID}{%
\expandafter\DTLifinlist\expandafter{\id}{#1}%
{\DTLgetvalueforkey{\thePIname}{name}{PI}{ID}{\id}\thePIname\DTLiflastrow{}{,\space}}
{}%
}}
完整的 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
看着https://tex.stackexchange.com/a/108731/98739我能够适应:
\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}
使用-Syntax 可以获得相同的结果expl3
(归功于gusbrs
(参见评论):
\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