我定义了命令:
\newcommand{\shcpoint}[1]{%
\ifnum #1<2
(\emph{#1 point})\quad\phantom{s}%
\else
(\emph{#1 points})\quad%
\fi
}
我使用的形式如下:
\documentclass{article}
\usepackage{amsmath}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}
\item \shcpoint{2} Some random question
\item \shcpoint{3} Other question
\end{enumerate}
\totalshcpoints
\end{document}
问题是,我如何将点存储在某个地方,也许是在文件中.aux
,然后将其添加到文档或使用它的部分(全局和本地)中以查找总数。我知道这是类exam
和其他包使用的功能。
答案1
您totcount
可以注册一个计数器并在任何地方使用总数(需要两次 LaTeX 运行才能稳定)。
\documentclass{article}
\usepackage{totcount}
\newcounter{totalpoints}
\regtotcounter{totalpoints}
\newcommand{\shcpoint}[1]{%
\addtocounter{totalpoints}{#1}%
\ifnum#1<2
(\emph{#1 point})\hphantom{\em s}%
\else
(\emph{#1 points})%
\fi\quad
}
\begin{document}
This questionary has a total of \total{totalpoints}~points.
\begin{enumerate}
\item \shcpoint{1} Easy question
\item \shcpoint{2} Some random question
\item \shcpoint{3} Other question
\end{enumerate}
\end{document}
答案2
latex 计数器是全局的,因此你可以这样做
\documentclass{article}
\newcounter{pts}
\newcommand{\shcpoint}[1]{%
\addtocounter{pts}{#1}%
\ifnum #1<2
(\emph{#1 point})\quad\phantom{s}%
\else
(\emph{#1 points})\quad%
\fi
}
\newcommand\totalshcpoints{\thepts}
\usepackage{amsmath}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}
\item \shcpoint{2} Some random question
\item \shcpoint{3} Other question
\end{enumerate}
\totalshcpoints
\end{document}