这是关于以下内容的后续问题:如何在 tabularray 表中使用 DTLforeach(数据工具)?。
考虑一下这个MWE:
\documentclass{scrbook}
\usepackage{datatool,tabularray,xcolor}
\NewDocumentCommand{\crcEntry}{ o m m }{%
\def\crcDB{#2}%
\DTLifdbexists{\crcDB}{}{\DTLnewdb{\crcDB}}%
\DTLnewrow{\crcDB}%
\IfNoValueTF{#1}{\setkeys{#2}{#3}}{\setkeys{#1}{#3}}%
}
\makeatletter
\define@key{OtherFundingSource}{nameID}{%
\DTLnewdbentry{\crcDB}{nameID}{#1}
}
\define@key{OtherFundingSource}{periodStart}{\DTLnewdbentry{\crcDB}{periodStart}{#1}}
\define@key{OtherFundingSource}{periodEnd}{\DTLnewdbentry{\crcDB}{periodEnd}{#1}}
\define@key{OtherFundingSource}{projectTitle}{\DTLnewdbentry{\crcDB}{projectTitle}{#1}}
\define@key{OtherFundingSource}{fundingAgency}{\DTLnewdbentry{\crcDB}{fundingAgency}{#1}}
\makeatother
\NewDocumentCommand{\crcOtherFundingSourceTable}{ }{%
\def\tblrbody{}%
\DTLforeach*{OtherFundingSource}{%
\nameID=nameID,%
\fundingAgency=fundingAgency,%
\projectTitle=projectTitle,%
\periodStart=periodStart,%
\periodEnd=periodEnd%
}{%
\edef\tblrbody{\tblrbody
{\nameID}
& {x}
& \projectTitle
& \periodStart --\periodEnd
& \fundingAgency\\
}}%
\begin{tblr}[%
expand=\tblrbody,
label={none},
entry={none},
long
]{
colspec = {m{2.6cm}m{.8cm}X[m]m{1.3cm}m{2cm}},
rowhead = 1,
row{1} = {
font={\bfseries},
halign={c}
},
}
PI %
& Project %
& Project title %
& Funding period %
& Funding agency\\
\tblrbody
\end{tblr}}
\begin{document}
\crcEntry{OtherFundingSource}{
nameID = {NAME},
projectTitle = {This is my \emph{title}.},
periodStart = {2020},
periodEnd = {2023},
fundingAgency = {LaTeX-funding}
}
\crcOtherFundingSourceTable
\end{document}
它会给我错误:
! \@sDTLforeach 的使用与其定义不匹配。\text@command #1->\edef \reserved@a { \unexpanded {#1}}\ifx \reserved@a @emp...
l.77 \crcOtherFundingSourceTable
\emph
但是当我从内容中删除时,我可以编译。
答案1
切勿\edef
在任意文本内容上使用。
使用\protected@edef\tblrbody
。