Use DTLforeach (datatool) within a tabularray table

datatooltabularray

After having populated a datatool database (EarlyCareerPhD) I would like to sort it and print its content within a tabularray-table.
I am aware of expanding macros within tabularray but expanding DTLforeach does not help here.

MWE

\documentclass{scrbook}

\usepackage{datatool,tabularray,xcolor}


\makeatletter
\define@key{EarlyCareerPhD}{project}{\DTLnewdbentry{\crcDB}{project}{#1}}
\define@key{EarlyCareerPhD}{periodStart}{\DTLnewdbentry{\crcDB}{periodStart}{#1}}
\define@key{EarlyCareerPhD}{periodEnd}{\DTLnewdbentry{\crcDB}{periodEnd}{#1}}
\define@key{EarlyCareerPhD}{name}{\DTLnewdbentry{\crcDB}{name}{#1}}
\define@key{EarlyCareerPhD}{nameFirst}{\DTLnewdbentry{\crcDB}{nameFirst}{#1}}
\define@key{EarlyCareerPhD}{fundingSource}{\DTLnewdbentry{\crcDB}{fundingSource}{#1}}
\define@key{EarlyCareerPhD}{topic}{\DTLnewdbentry{\crcDB}{topic}{#1}}
\makeatother




\NewDocumentCommand{\crcEarlyCareerPhD}{ m }{
  \def\crcDB{EarlyCareerPhD}
  \DTLifdbexists{\crcDB}{}{\DTLnewdb{\crcDB}}
  \DTLnewrow{\crcDB}
  \setkeys{EarlyCareerPhD}{#1}
}

\NewDocumentCommand{\crcEarlyCareerPhDtable}{ o }{
  \DTLsort{project,name}{EarlyCareerPhD}
\begin{tblr}[%
  expand=\DTLforeach,
  caption={Completed PhD Thesis}, %TODO
  long
  ]{
    colspec = {m{1cm}m{3cm}m{2.5cm}X[m]m{2.2cm}},
    rowhead = 1,
    column{1} = {font={\bfseries},
      fg = {blue}},
    column{4} = {font={\itshape}},
    row{1} = {font={\bfseries},fg={white}}
  }
  Project %
  & Surname, first name %
  & Type of funding %
  & Topic %
  & Duration\\
  {\DTLforeach{EarlyCareerPhD}{%
    \project=project,%
    \name=name,%
    \nameFirst=nameFirst,%
    \fundingSource=fundingSource,%
    \topic=topic,%
    \periodStart=periodStart,%
    \periodEnd=periodEnd%
  }{
    \project
    & \name, \nameFirst
    & \fundingSource
    & \fundingSource
    & \topic
    & \periodStart --\periodEnd}}\\
\end{tblr}
}
\begin{document}


\crcEarlyCareerPhD{
  project       = {A02},
  name          = {Surname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}

\crcEarlyCareerPhD{
  project       = {A02},
  name          = {Burname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}
\crcEarlyCareerPhD{
  project       = {A01},
  name          = {Aurname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}


\crcEarlyCareerPhDtable
\end{document}

enter image description here

Best Answer

You need to collect table body first (outside tblr environment):

\documentclass{scrbook}

\usepackage{datatool,tabularray,xcolor}

\makeatletter
\define@key{EarlyCareerPhD}{project}{\DTLnewdbentry{\crcDB}{project}{#1}}
\define@key{EarlyCareerPhD}{periodStart}{\DTLnewdbentry{\crcDB}{periodStart}{#1}}
\define@key{EarlyCareerPhD}{periodEnd}{\DTLnewdbentry{\crcDB}{periodEnd}{#1}}
\define@key{EarlyCareerPhD}{name}{\DTLnewdbentry{\crcDB}{name}{#1}}
\define@key{EarlyCareerPhD}{nameFirst}{\DTLnewdbentry{\crcDB}{nameFirst}{#1}}
\define@key{EarlyCareerPhD}{fundingSource}{\DTLnewdbentry{\crcDB}{fundingSource}{#1}}
\define@key{EarlyCareerPhD}{topic}{\DTLnewdbentry{\crcDB}{topic}{#1}}
\makeatother

\NewDocumentCommand{\crcEarlyCareerPhD}{ m }{
  \def\crcDB{EarlyCareerPhD}
  \DTLifdbexists{\crcDB}{}{\DTLnewdb{\crcDB}}
  \DTLnewrow{\crcDB}
  \setkeys{EarlyCareerPhD}{#1}
}

\NewDocumentCommand{\crcEarlyCareerPhDtable}{ o }{%
  \DTLsort{project,name}{EarlyCareerPhD}%
  \def\tblrbody{}%
  \DTLforeach{EarlyCareerPhD}{%
    \project=project,%
    \name=name,%
    \nameFirst=nameFirst,%
    \fundingSource=fundingSource,%
    \topic=topic,%
    \periodStart=periodStart,%
    \periodEnd=periodEnd%
  }{
    \edef\tblrbody{\tblrbody
    \project
    & \name, \nameFirst
    & \fundingSource
    & \fundingSource
    & \topic
    & \periodStart --\periodEnd\\
  }}%
  \begin{tblr}[%
    expand=\tblrbody,
    caption={Completed PhD Thesis}, %TODO
    long
  ]{
    colspec = {m{1cm}m{3cm}m{2.5cm}X[m]m{2.2cm}},
    rowhead = 1,
    column{1} = {font={\bfseries},
      fg = {blue}},
    column{4} = {font={\itshape}},
    row{1} = {font={\bfseries},fg={white}}
  }
  Project %
  & Surname, first name %
  & Type of funding %
  & Topic %
  & Duration\\
  \tblrbody
\end{tblr}
}

\begin{document}

\crcEarlyCareerPhD{
  project       = {A02},
  name          = {Surname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}
\crcEarlyCareerPhD{
  project       = {A02},
  name          = {Burname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}
\crcEarlyCareerPhD{
  project       = {A01},
  name          = {Aurname},
  nameFirst     = {Firstname},
  fundingSource = {DFG},
  topic         = {My fancy PhD thesis},
  periodStart   = {YYYY-MM},
  periodEnd     = {YYYY-MM}
}

\crcEarlyCareerPhDtable

\end{document}

enter image description here

Related Question