
如何在 LaTeX 中進行一些簡單的計算?
具體來說,我想將\numpoints
(包包的一部分exam
)除以 1.10。也就是說,我想做這樣的事情:
Grade: \underline{\hspace{2cm}} out of \numpoints<DIVIDED BY 1.1> (points available \numpoints).
答案1
使用expl3
這真的很簡單:
\documentclass[addpoints]{exam}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
\makeatletter
\newcommand{\calcnumpoints}{\@ifundefined{exam@numpoints}{0}{\exam@numpoints}}
\makeatother
\begin{document}
\begin{questions}
\titledquestion{First Question}[5]
\titledquestion{Second Question}[5]
\titledquestion{Third Question}[2]
\titledquestion{Fourth Question}[2]
\end{questions}
\numpoints
\calc{round(\calcnumpoints/1.1,1)}
\end{document}
\calc
執行任何浮點計算,在上述情況下四捨五入到一位小數。由於\numpoints
輸出有點像參考(??如果它不存在,\exam@numpoints
否則),我做了一個替代定義,如果尚未定義,則\calcnumpoints
預設為 0 。\exam@numpoints
這樣您就可以按預期在計算中使用它。