1

In this question, there are three answers to build a tabular from a key value command.

How to build a spreadtab with the same key value in order to be able to sum up columnwise (with a tag to reuse the values)?

\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}
2
  • That's a hard one. You'd have to build the table body before spreadtab scans it, which isn't too difficult. But for some reason, when building the table body programmatically, spreadtab goes in an infinite loop for me, while the same body built manually works. – Skillmon Mar 29 at 11:47
  • That's why I wasn't able to solve this by myself! I'm going to try not using spreadtab and compute the sums within \l__nbur_evaluation_row_prop – NBur Mar 29 at 11:59
1

The following works by first building the entire table inside a token list, then applying \tl_rescan:nn to the contents of that token list.

\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}
2
  • I still have lot of to learn! Just to clarify: why did do change the \space to ~ ? – NBur Mar 29 at 13:33
  • 1
    @NBur Because inside of \ExplSyntaxOn a ~ is the same as a space. – Skillmon Mar 29 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.