tabularray 和 \gappto

tabularray 和 \gappto

我用这个新命令\problem为我的数学学生排版测试。有时我还会包含你在某个问题上能获得多少学分。如果我使用这个credits键,一切都会正常。但我还有一个\task[<credits>]命令,我可以用它来打印小问题及其学分(见下面的 MWE)。我目前面临的问题是,用这个\task命令给出的学分将被多次输入,而不是只输入一次。你有什么建议吗?谢谢!


\documentclass[12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xifthen}    
\usepackage{tikz}
\usepackage{tabularray}
  \UseTblrLibrary{booktabs, counter}

\makeatletter
\def\credit{}
\newcounter{WS@credits}
\setcounter{WS@credits}{0}
\newcounter{subtask}
\setcounter{subtask}{1}
\renewcommand{\thesubtask}{\alph{subtask})}
\newcommand{\task}[1][]{%
    \ifthenelse{\isempty{#1}}{%
        \thesubtask%
        \stepcounter{subtask}%
    }{%
        \thesubtask%
        \addtocounter{WS@credits}{#1}%
        \ifnum\arabic{subtask}=1%
            \gappto\credit{(#1~p)}%
        \else%
            \gappto\credit{\\(#1~p)}%
        \fi%
        \stepcounter{subtask}%
    }%
}

\newcommand{\problem@credits}{}
\define@key{problem}{credits}[\problem@credits]{\renewcommand{\problem@credits}{%
    \addtocounter{WS@credits}{#1}%
    (#1~p)%
    }%
}

\newcounter{probctr}
\setcounter{probctr}{1}

\newcommand{\problem}[3][]{
    \setkeys{problem}{#1}%
    \begin{tblr}{colspec={>{\bfseries}lX}, column{1}={leftsep=0ex, rightsep=1.5ex}, column{2}={leftsep=0ex, rightsep=0ex}}
        \tikz[remember picture, baseline=(probctrbox.base)]{%
            \node[anchor=north west] (probctrbox) at (0,0) {A\arabic{probctr}};
        } &
        {#2}%
    \end{tblr}%
    \tikz[remember picture, overlay]{%
        \node at (probctrbox.south) [below, font=\scriptsize\normalfont]{\parbox{7ex}{\centering\ifdefempty{\credit}{\problem@credits}{\credit}}};
    }
    \stepcounter{probctr}
    \setcounter{subtask}{1}
    \renewcommand{\credit}{}
    \renewcommand{\problem@credits}{}
}
\makeatother

\begin{document}
\problem[credits=3]{
Write as power expressions.

\begin{tblr}{colspec={*{2}{>{\bfseries}lX}}, column{odd}={rightsep=0ex}}
    \task & $2\cdot 2\cdot 2\cdot 2$ &
        \task & $3\cdot 3\cdot 3\cdot 3\cdot 3\cdot 3$
\end{tblr}
}{
\begin{tblr}{colspec={*{2}{>{\bfseries}lX}}, column{odd}={rightsep=0ex}}
    \task & $=2^4$ &
        \task & $=3^6$
\end{tblr}
}

\problem{
Write as power expressions.

\begin{tblr}{colspec={*{2}{>{\bfseries}lX}}, column{odd}={rightsep=0ex}}
    \task[1] & $2\cdot 2\cdot 2\cdot 2$ &
        \task[3] & $3\cdot 3\cdot 3\cdot 3\cdot 3\cdot 3$
\end{tblr}
}{
\begin{tblr}{colspec={*{2}{>{\bfseries}lX}}, column{odd}={rightsep=0ex}}
    \task & $=2^4$ &
        \task & $=3^6$
\end{tblr}
}
\end{document}

相关内容