如何在 tabularray 表中使用 DTLforeach(数据工具)?

如何在 tabularray 表中使用 DTLforeach(数据工具)?

填充 datatool 数据库 ( EarlyCareerPhD) 后,我想对其进行排序并在 tabularray 表中打印其内容。我知道在 tabularray 中扩展宏,但扩展DTLforeach在这里没有帮助。

平均能量损失

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

在此处输入图片描述

答案1

您需要先收集表体(外部tblr环境):

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

在此处输入图片描述

答案2

调整 LJR 的解决方案以使用 expl3。优点是时间复杂度是线性的,而不是行数的二次方。

(实际上有一个小的变化,即表“标题”本身也包含在要构建的标记列表中,因此expand=没有必要)

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

输出相同。

相关内容