完整代码

完整代码

我想澄清的是,我已尽最大努力制作最小的 MWE,但仍然能够准确地捕捉到我所面临的问题。

总而言之,我得到了以下帮助另一个答案定义命令时

\addtoDel{1}{1a}{1}

此命令将值插入到由以下定义的表中:

\TypesetTable

生成的表格看起来像

在此处输入图片描述

我已经item在我的文档中重新定义了命令,以自动生成三个标签tittelprobalfa

我想将其挂接到\addtoDel命令中。我尝试按如下方式操作

\newcommand{\points}[1]{%
    \addtoDel{\arabic{tittel}}{%
        \expandafter\arabic{prob}\alph{alfa}\expandafter
        }{#1}
}

但是,此命令扩展计数器太晚了。例如,以下代码

.
.
.
\TypesetTable
.
.
.
\Oppgave[4] % Oppgave 1

\begin{oppgaver}
    \points{1} \item Prob 1
    % \addtoDel{1}{1a}{1} \item Prob 1

    \points{1} \item Prob 2
    % \addtoDel{1}{1b}{1} \item Prob 2

    \points{2} \item Prob 3
    % \addtoDel{1}{1c}{2} \item Prob 3
\end{oppgaver}
.
.
.

生产

在此处输入图片描述

这是错误的。这些点是正确的,但它只扩展了最后一个标签。切换到手动插入标签

.
.
.
\TypesetTable
.
.
.
\Oppgave[4] % Oppgave 1

\begin{oppgaver}
    % \points{1} \item Prob 1
    \addtoDel{1}{1a}{1} \item Prob 1

    % \points{1} \item Prob 2
    \addtoDel{1}{1b}{1} \item Prob 2

    % \points{2} \item Prob 3
    \addtoDel{1}{1c}{2} \item Prob 3
\end{oppgaver}
.
.
.

给出正确的输出

在此处输入图片描述

我怎样才能改变\points以产生上述结果?

完整代码

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{xparse,booktabs}

\usepackage{etoolbox} %Neccecary for ifthenelse
\usepackage{xparse}

\usepackage{enumitem}


\newcounter{tittel}
\setcounter{tittel}{0}

\newcounter{prob}[tittel]
\setcounter{prob}{0}

\newcounter{prob1}
\setcounter{prob1}{0}

\newcounter{prob2}[tittel]
\setcounter{prob2}{0}

\newcounter{alternative}[prob]
\setcounter{alternative}{0}

\newcounter{alfa}[prob]

\ExplSyntaxOn

\NewDocumentCommand{\TypesetTable} { O{10} }
 {
  \seq_if_empty:NT \g__nebu_deli_seq
   {
    \seq_gput_right:Nn \g__nebu_deli_seq { X }
    \seq_gput_right:Nn \g__nebu_pdeli_seq { 0 }
    \seq_gput_right:Nn \g__nebu_delii_seq { X }
    \seq_gput_right:Nn \g__nebu_pdelii_seq { 0 }
   }
  \nebu_typeset_table:n { #1 }
 }

\NewDocumentCommand{\addtoDel}{mmm}
 {
  \clist_gput_right:cn { g__nebu_del\int_to_roman:n { #1 }_clist } { #2 }
  \clist_gput_right:cn { g__nebu_pdel\int_to_roman:n { #1 }_clist } { #3 }
 }

\AtEndDocument{
  \iow_shipout:cx { @auxout }
   { \exp_not:N \LoadDel { i } { \g__nebu_deli_clist } }
  \iow_shipout:cx { @auxout }
   { \exp_not:N \LoadPDel { i } { \g__nebu_pdeli_clist } }
  \iow_shipout:cx { @auxout }
   { \exp_not:N \LoadDel { ii } { \g__nebu_delii_clist } }
  \iow_shipout:cx { @auxout }
   { \exp_not:N \LoadPDel { ii } { \g__nebu_pdelii_clist } }
}
\cs_generate_variant:Nn \iow_shipout:Nx { c }
\NewDocumentCommand{\LoadDel}{mm}
 {
  \seq_gset_split:cnn { g__nebu_del#1_seq } { , } { #2 }
 }
\NewDocumentCommand{\LoadPDel}{mm}
 {
  \seq_gset_split:cnn { g__nebu_pdel#1_seq } { , } { #2 }
 }
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }

\int_new:N \l__nebu_columns_int
\int_new:N \l__nebu_pdeli_total_int
\int_new:N \l__nebu_pdelii_total_int
\int_new:N \l_nebu_countrows_int
\seq_new:N \g__nebu_deli_seq
\seq_new:N \g__nebu_pdeli_seq
\seq_new:N \g__nebu_delii_seq
\seq_new:N \g__nebu_pdelii_seq
\clist_new:N \g__nebu_deli_clist
\clist_new:N \g__nebu_pdeli_clist
\clist_new:N \g__nebu_delii_clist
\clist_new:N \g__nebu_pdelii_clist
\tl_new:N \l__nebu_preamble_tl
\tl_new:N \l__nebu_table_body_tl

\cs_new_protected:Npn \nebu_typeset_table:n #1
 {
  % Compute the number of inner columns
  \int_set:Nn \l__nebu_columns_int
   {
    \int_min:nn { #1 }
     {
      \int_max:nn { \seq_count:N \g__nebu_deli_seq }
                  { \seq_count:N \g__nebu_delii_seq }
     }
   }
  % Define the table preamble
  \tl_set:Nx \l__nebu_preamble_tl
   { l c *{ \int_to_arabic:n { \l__nebu_columns_int } } { c } c }
  % Compute the total for Del1
  \int_zero:N \l__nebu_pdeli_total_int
  \seq_map_inline:Nn \g__nebu_pdeli_seq
   { \int_set:Nn \l__nebu_pdeli_total_int { \l__nebu_pdeli_total_int + ##1 } }
  % Compute the total for Del2
  \int_zero:N \l__nebu_pdelii_total_int
  \seq_map_inline:Nn \g__nebu_pdelii_seq
   { \int_set:Nn \l__nebu_pdelii_total_int { \l__nebu_pdelii_total_int + ##1 } }
  % Start building the table contents
  \tl_clear:N \l__nebu_table_body_tl
  \tl_put_right:Nx \l__nebu_table_body_tl
   { \exp_not:N \multicolumn { \int_to_arabic:n { \l__nebu_columns_int + 2 } } { c } { } }
  \tl_put_right:Nn \l__nebu_table_body_tl { & Sum \\ \midrule }

  % Insert Del1
  \__nebu_insert_table_part:nn { deli } { Del~1 }
  \tl_put_right:Nn \l__nebu_table_body_tl { \midrule }
  % Insert Del2
  \__nebu_insert_table_part:nn { delii } { Del~2 }

  % Add total
  \tl_put_right:Nx \l__nebu_table_body_tl
   {
    \exp_not:N \midrule
    \exp_not:N \multicolumn
                { \int_to_arabic:n { 2 + \l__nebu_columns_int } }
                { r }
                { Total~antall~poeng }
    &
    \int_to_arabic:n { \l__nebu_pdeli_total_int + \l__nebu_pdelii_total_int }
    \exp_not:N \\
   }

  % Print the table
  \use:x{ \exp_not:N \begin{tabular}{\l__nebu_preamble_tl} }
  \toprule
  \l__nebu_table_body_tl
  \bottomrule
  \end{tabular}
}

\cs_new_protected:Npn \__nebu_insert_table_part:nn #1 #2
 {
  \int_set:Nn \l_nebu_countrows_int
   {
    \seq_count:c { g__nebu_#1_seq } / \l__nebu_columns_int
   }
  \int_step_inline:nnnn { 1 } { 1 } { \l_nebu_countrows_int }
   {
    % First line
    \int_compare:nTF { ##1 = 1 }
     {
      \tl_put_right:Nn \l__nebu_table_body_tl { #2 & Oppgave & }
     }
     {
      \tl_put_right:Nx \l__nebu_table_body_tl
       { \exp_not:N \cmidrule(lr){3-\int_to_arabic:n { \l__nebu_columns_int + 2 }} }
      \int_compare:nT { ##1 = 2 }
       {\tl_put_right:Nx \l__nebu_table_body_tl
         { \exp_not:N \cmidrule(l{3em}r)
            {\int_to_arabic:n { \l__nebu_columns_int + 2 }-
             \int_to_arabic:n { \l__nebu_columns_int + 3 }}
         }
       }
      \tl_put_right:Nn \l__nebu_table_body_tl { & & }
     }
    \int_step_inline:nnnn 
     { (##1 - 1)*\l__nebu_columns_int + 1 } 
     { 1 }
     { ##1 * \l__nebu_columns_int }
     {
      \tl_put_right:Nx \l__nebu_table_body_tl 
       { \seq_item:cn { g__nebu_#1_seq } { ####1 } & }
     }
     \tl_put_right:Nn \l__nebu_table_body_tl { \\ }
     % Second line
    \int_compare:nTF { ##1 = 1 }
     { \tl_put_right:Nn \l__nebu_table_body_tl { & Poeng & } }
     { \tl_put_right:Nn \l__nebu_table_body_tl { & & } }
     \int_step_inline:nnnn 
      { (##1 - 1)*\l__nebu_columns_int + 1 }
      { 1 }
      { ##1 * \l__nebu_columns_int }
      {
       \tl_put_right:Nx \l__nebu_table_body_tl
        { \seq_item:cn { g__nebu_p#1_seq } { ####1 } & }
      }
      \tl_put_right:Nx \l__nebu_table_body_tl
       {
        \int_compare:nT { ##1 = 1 }
         { \int_to_arabic:n { \use:c { l__nebu_p#1_total_int } } }
        \exp_not:N \\
       }
   }
 }

 \newif\iffirstalt

 \NewDocumentCommand{\Oppgave}{s o}{   %
  \IfBooleanTF{#1}%
   {\refstepcounter{alternative}\iffirstalt\else\refstepcounter{prob}\firstalttrue\fi}%
   {\setcounter{alternative}{0}\refstepcounter{prob}\firstaltfalse}%
  \section*{Oppgave~\arabic{prob}~% 
  \stepcounter{prob2}
  \label{\arabic{tittel}.\arabic{prob2}}
    {\normalfont\IfBooleanTF{#1}{Alternativ \Roman{alternative}~}{}}%
                \IfNoValueTF{#2}{}{\normalfont\normalsize (#2~poeng)}}%
   \addcontentsline{toc}{section}{Oppgave~\arabic{prob}} \refstepcounter{alfa} 
}

\ExplSyntaxOff

\makeatletter

\let\org@item\@item
\def\myspecial@item[#1]{%
  \org@item[#1]%
  \ifdef\@listctr
  {%
    \expandafter\ifdefequal\@listctr\@enumctr
    {\addcontentsline{toc}{subsection}{\hspace{0.8em}\textbf{\alph{alfa})}}
    \label{\arabic{tittel}.\arabic{prob2}.\alph{alfa}}
    \refstepcounter{alfa}}%
    {}%
  }{}%
}

\newenvironment{oppgaver}{%
  \begin{enumerate}[ref={\alph{alfa}},topsep =1.2em,label=\textbf{\alph{alfa})},labelsep=1.5em,itemsep=0.7cm]
  \let\@item\myspecial@item
}{%
  \end{enumerate}
}

\makeatother

\newcommand{\points}[1]{%
    \addtoDel{\arabic{tittel}}{%
        \expandafter\arabic{prob}\alph{alfa}\expandafter
        }{#1}
}

\begin{document}

\TypesetTable

\addtoDel{2}{a1}{2}
\addtoDel{2}{a2}{1}
\addtoDel{2}{a3}{1}
\addtoDel{2}{b}{2}

\stepcounter{tittel}

\Oppgave[4] % Oppgave 1

\begin{oppgaver}
    % \points{1} \item Prob 1
     \addtoDel{1}{1a}{1} \item Prob 1

    % \points{1} \item Prob 2
     \addtoDel{1}{1b1}{1} \item Prob 2

    % \points{2} \item Prob 3
     \addtoDel{1}{1c1}{2} \item Prob 3
\end{oppgaver}

\end{document}

答案1

您是否想定义\points如下?

\newcommand{\points}[1]{%
  \begingroup\edef\x{\endgroup
    \noexpand\addtoDel{\arabic{tittel}}{\arabic{prob}\alph{alfa}}{#1}%
  }\x
}

随着身体

\begin{document}

\TypesetTable

\addtoDel{2}{a1}{2}
\addtoDel{2}{a2}{1}
\addtoDel{2}{a3}{1}
\addtoDel{2}{b}{2}

\stepcounter{tittel}

\Oppgave[4] % Oppgave 1

\begin{oppgaver}
     \points{1} \item Prob 1

     \points{1} \item Prob 2

     \points{2} \item Prob 3
\end{oppgaver}

\end{document}

我明白了

在此处输入图片描述

相关内容