命令中的参数总数

命令中的参数总数

我想编写一个带有两个参数的命令,分别接受文本和标记(实数值)。如何自动计算标记总和以供文档其他地方使用?

\newcounter{num}
\newcommand{\soal}[2]{
    \stepcounter{num} \thenum & {#1} & {#2} \\[4pt] \hline
}

答案1

更新答案:

该答案允许您从文档中的任何位置访问总数并添加实际值。

新输出

\documentclass{article}
\usepackage{calculator}
\usepackage{ifthen}
\newboolean{firstTime}                      % Declare a new variable of type boolean
\setboolean{firstTime}{true}                % Set it to true
\newcounter{num}

\newcommand{\soal}[2]{
    \ifthenelse{\boolean{firstTime}}{       % If first time, add 0 + (passed number)
        \ADD{0}{#2}{\totalMarks}            % Perform the actual calculation
        \setboolean{firstTime}{false}       % Set the boolean variable to false
    }{                                      % Else, a total exists, so add passed number
        \ADD{\totalMarks}{#2}{\totalMarks}  % Perform the actual calculation
    }
    \begin{tabular}{ccc}
        \stepcounter{num} \thenum & {#1} & \totalMarks \\[4pt] \hline
    \end{tabular}\par
}
\begin{document}
    \soal{Started at two and $\frac{1}{2}$}{2.50}
    \soal{Added three and $\frac{1}{4}$}{3.25}
    \soal{Added five and $\frac{3}{4}$}{5.75}\par~\\[2ex]
    Now, for the total marks: \totalMarks
\end{document}

原始答案:

输出

\documentclass{article}
\newcounter{num}
\newcounter{total}

\newcommand{\soal}[2]{
    \begin{tabular}{ccc}
        \stepcounter{num} \thenum & {#1} & \addtocounter{total}{#2} \thetotal \\[4pt] \hline
    \end{tabular}\par
}
\begin{document}
    \soal{Started at two}{2}
    \soal{Added three}{3}
    \soal{Added five}{5}
\end{document}

相关内容