使用键值命令填充电子表格

使用键值命令填充电子表格

在此问题,有三个答案可以通过键值命令构建表格。

如何构建具有相同键值的电子表格以便能够按列求和(使用标签来重用这些值)?

\documentclass{memoir}

\usepackage{xparse}
\usepackage{spreadtab}

\ExplSyntaxOn
\keys_define:nn { evaluation }
{
    cc .tl_set:N = \l__nbur_evaluation_cc_tl,
    cc .initial:n = \q_no_value,
    ds .tl_set:N = \l__nbur_evaluation_ds_tl,
    ds .initial:n = \q_no_value,
    dm .tl_set:N = \l__nbur_evaluation_dm_tl,
    dm .initial:n = \q_no_value,
    projet .tl_set:N = \l__nbur_evaluation_projet_tl,
    projet .initial:n = \q_no_value,
    tp .tl_set:N = \l__nbur_evaluation_tp_tl,
    tp .initial:n = \q_no_value,
}
\prop_new:N \l__nbur_evaluation_row_prop
\cs_new_protected:Npn \__nbur_evaluation_parse_row:nn #1 #2
{
    \quark_if_no_value:nF {#2}
    {
        \prop_set_from_keyval:Nn \l__nbur_evaluation_row_prop {#2}
        \use:x
        {
            \exp_not:n {#1}
            & \prop_item:Nn \l__nbur_evaluation_row_prop { n }
            & \prop_item:Nn \l__nbur_evaluation_row_prop { e }
            & \prop_item:Nn \l__nbur_evaluation_row_prop { p }
        }
        \\
    }
}
\cs_generate_variant:Nn \__nbur_evaluation_parse_row:nn { nV }

\NewDocumentCommand{\Evaluation}{m}
{
    \group_begin:
    \keys_set_known:nn {evaluation}{#1}
    \begin{spreadtab}{{tabular}{@{}*{4}{c}@{}}}
        @Type & @Number & @Exam & @home\space work\\
        \__nbur_evaluation_parse_row:nV {@{contrôle\space continu}} \l__nbur_evaluation_cc_tl
        \__nbur_evaluation_parse_row:nV {@{devoir\space surveillé}} \l__nbur_evaluation_ds_tl
        \__nbur_evaluation_parse_row:nV {@{devoir\space maison}} \l__nbur_evaluation_dm_tl
        \__nbur_evaluation_parse_row:nV {@{Projet}} \l__nbur_evaluation_projet_tl
        \__nbur_evaluation_parse_row:nV {@{travaux\space pratiques}} \l__nbur_evaluation_tp_tl
%       \midrule % doesn't work
%       @\emph{Sums} & & sum(c1:[0,-1])tag(exam) & sum(d1:[0,-1])tag(perso)\\ % doesn't work
    \end{spreadtab}
    \group_end:
}
\ExplSyntaxOff

\begin{document}
    \Evaluation{cc={n=1,e=2,p=3}, projet={p=8}, dm={n=1,p=4}}
\end{document}

答案1

以下工作原理是首先在标记列表中构建整个表,然后应用于\tl_rescan:nn该标记列表的内容。

\documentclass{memoir}

\usepackage{xparse}
\usepackage{spreadtab}

\ExplSyntaxOn
\keys_define:nn { evaluation }
{
    cc .tl_set:N = \l__nbur_evaluation_cc_tl,
    cc .initial:n = \q_no_value,
    ds .tl_set:N = \l__nbur_evaluation_ds_tl,
    ds .initial:n = \q_no_value,
    dm .tl_set:N = \l__nbur_evaluation_dm_tl,
    dm .initial:n = \q_no_value,
    projet .tl_set:N = \l__nbur_evaluation_projet_tl,
    projet .initial:n = \q_no_value,
    tp .tl_set:N = \l__nbur_evaluation_tp_tl,
    tp .initial:n = \q_no_value,
}
\prop_new:N \l__nbur_evaluation_row_prop
\tl_new:N \l__nbur_evaluation_body_tl
\cs_new_protected:Npn \__nbur_evaluation_parse_row:nn #1 #2
  {
    \quark_if_no_value:nF {#2}
      {
        \prop_set_from_keyval:Nn \l__nbur_evaluation_row_prop {#2}
        \tl_put_right:Nx \l__nbur_evaluation_body_tl
          {
            \exp_not:n {#1}
            & \__nbur_evaluation_get_from_prop:n { n }
            & \__nbur_evaluation_get_from_prop:n { e }
            & \__nbur_evaluation_get_from_prop:n { p }
            \exp_not:n { \\ }
          }
      }
  }
\cs_generate_variant:Nn \__nbur_evaluation_parse_row:nn { nV }
\cs_new:Npn \__nbur_evaluation_get_from_prop:n #1
  { \prop_item:Nn \l__nbur_evaluation_row_prop {#1} }
\iow_new:N \l_nbur_iow
\NewDocumentCommand{\Evaluation}{m}
  {
    \group_begin:
      \keys_set_known:nn {evaluation}{#1}
      \tl_set:Nn \l__nbur_evaluation_body_tl
        {
          \begin{spreadtab}{{tabular}{@{}*{4}{c}@{}}}
          @Type & @Number & @Exam & @home~ work\\
        }
      \__nbur_evaluation_parse_row:nV {@{contrôle~ continu}}  \l__nbur_evaluation_cc_tl
      \__nbur_evaluation_parse_row:nV {@{devoir~ surveillé}}  \l__nbur_evaluation_ds_tl
      \__nbur_evaluation_parse_row:nV {@{devoir~ maison}}     \l__nbur_evaluation_dm_tl
      \__nbur_evaluation_parse_row:nV {@{Projet}}             \l__nbur_evaluation_projet_tl
      \__nbur_evaluation_parse_row:nV {@{travaux~ pratiques}} \l__nbur_evaluation_tp_tl
      \tl_put_right:Nn \l__nbur_evaluation_body_tl
        {
          \midrule
          @\emph{Sums} & & sum(c1:[0,-1])tag(exam) & sum(d1:[0,-1])tag(perso)\\
          \end{spreadtab}
        }% doesn't work
      \exp_args:Nno \tl_rescan:nn {} \l__nbur_evaluation_body_tl
    \group_end:
  }
\ExplSyntaxOff

\begin{document}
\Evaluation{cc={n=1,e=2,p=3}, projet={p=8}, dm={n=1,p=4}}
\end{document}

相关内容