使用命令计算总和?

使用命令计算总和?

我必须承认这听起来是一个简单的问题。

我正在为家庭作业创建评分模型,并为每个问题添加最高分。有没有办法让 LaTeX 自动打印出这些(整数)分数的总和?

\points{3} Solve equation [...]
\points{5} Give proof of [...]

Max points: \totalpoints{}

然后会打印出“最高分:8”在文档的末尾。

答案1

使用普通的 LaTeX 计数器实际上非常简单:

\documentclass{article}

\newcounter{points}
\newcommand*{\points}[1]{\addtocounter{points}{#1}}
\newcommand*{\totalpoints}{\thepoints}% Or simply use `\thepoints` directly.

\begin{document}

\points{3} Solve equation [...]
\points{5} Give proof of [...]

Max points: \totalpoints{}

\end{document}

答案2

我不确定这是否有趣,而且它完全过度杀伤。这只是一个在 LaTeX 文档中使用 Lua 代码的非常简单的示例。

用 编译lualatex

\documentclass{article}
\usepackage{luacode}
\newcommand\points[1]{%
\luaexec{
  if a == nil then
    a = #1
  else
    a = a + #1
  end
}}
\newcommand\totalpoints{\luaexec{tex.sprint(a)}}

\begin{document}
\points{3} Solve equation [...]
\points{5} Give proof of [...]

Max points: \totalpoints{}
\end{document}

答案3

该课程提供了为您计算总分的exam命令。\numpoints

请参阅文档以了解完整详细信息 - MWE 如下

\documentclass[addpoints]{exam}

\begin{document}

%\pointsdroppedatright          % try uncommenting this line
%\pointpoints{point}{points}

\cfoot{\footnotesize Page \thepage {} of \numpages} 
\lfoot{\footnotesize TOTAL POINTS : \numpoints\ }

This exam has \numquestions\ questions, for a total of \numpoints\ points. The number of points for each  part is given in brackets.

\begin{questions}
%+++++++++++++++++++++++++
%   QUESTION
%+++++++++++++++++++++++++
\question[10] What is your favourite fruit?

\vspace{\fill}

%+++++++++++++++++++++++++
%   QUESTION
%+++++++++++++++++++++++++
\question[23] What is the answer?

\vspace{\fill}
\droppoints

\end{questions}
\end{document}

相关内容