如何在 DTLforeach* 中为键应用动态名称?

如何在 DTLforeach* 中为键应用动态名称?

我想创建一个新命令来快速获取过滤列的值数量。

我的 MWE 是:

\documentclass{scrbook}
\usepackage{datatool,xstring,siunitx}

\begin{filecontents*}[overwrite]{PI.csv}
period|gender
1|female
2|male
1,2|female
2|female
\end{filecontents*}

\DTLsetseparator{|}
\DTLloaddb{PI}{PI.csv}

\NewDocumentCommand{\crcCountPIs}{mmm}{%
  % #1: funding period of interest
  % #2: column of database
  % #3: value to compare against
  \DTLforeach*[%start of conditions
  \DTLisinlist{#1}{\period}% 1st condition
  \and%
  \DTLiseq{\csname #2\endcsname}{#3}% 2nd condition
  ]%end of conditions
  {PI} % database
  {\period=period,%
    \expandafter\csname #2\expandafter\endcsname=#2% %fails, but is more flexible
    %\gender=#2% works, but is not flexible
  }
  {}
  \DTLsavelastrowcount{\n}\num{\n}%
}
\begin{document}
female (period 1): \crcCountPIs{1}{gender}{female}\\
male (period 1): \crcCountPIs{1}{gender}{male}\\
female (period 2): \crcCountPIs{2}{gender}{female}\\
male (period 2): \crcCountPIs{2}{gender}{male}
\end{document}

问题在于\expandafter\csname #2\expandafter\endcsname=#2想要使用动态名称作为比较中使用的键的行。

使用静态\gender=#2可以工作并给出这个(期望的)结果:

在此处输入图片描述

相关内容