4

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

lukascbossert
  • 3,015
  • 1
  • 16
  • 37

2 Answers2

4

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

L.J.R.
  • 10,932
3

Adapting L.J.R.'s solution to use expl3. An advantage is that the time complexity is linearithmic instead of quadratic in the number of rows.

(actually there's a small change namely that the table "header" itself is included in the token list to be built as well so expand= is not necessary)

\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} }

\ExplSyntaxOn \NewDocumentCommand{\crcEarlyCareerPhDtable}{ o }{% \DTLsort{project,name}{EarlyCareerPhD} \tl_build_begin:N \l__lukas_tblrbody_tl \tl_build_put_right:Nn \l__lukas_tblrbody_tl { \begin{tblr}[caption={Completed~PhD~Thesis}, long]{ % slight annoyance in \ExplSyntaxOn environment, need to specify space by ~ 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 }{ \tl_build_put_right:Nx\l__lukas_tblrbody_tl{ \project & \name, \nameFirst & \fundingSource & \fundingSource & \topic & \periodStart --\periodEnd\ }} \tl_build_put_right:Nn\l__lukas_tblrbody_tl{\end{tblr}} \tl_build_end:N\l__lukas_tblrbody_tl \l__lukas_tblrbody_tl } \ExplSyntaxOff

\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}

The output is identical.

user202729
  • 7,143
  • Could you please add in an explanation of the expl3 code? When I look through the expl3 pdf documentation, I don't find any explanations for these functions -- not that I understand expl3, anyway. – Canyon Dec 05 '23 at 00:51
  • @Canyon Documentation of the functions are in texdoc interface3. But overall, I think there's no good expl3 tutorial at the moment. – user202729 Dec 05 '23 at 00:54