How can I do some simple calculations in LaTeX?

Specifically, I want to divide \numpoints (part of the exam package) by 1.10. That is, I want to do something like this:

Grade: \underline{\hspace{2cm}} out of \numpoints<DIVIDED BY 1.1> (points available \numpoints).

  • 1
    Packages \calc, calculator and the pgfmath utilities might help. – user31729 Apr 18 '15 at 21:36
  • 1
    In addition, you should be more specific about the particular needs – user31729 Apr 18 '15 at 21:41

Using expl3 it's really simple:

enter image description here

  \cs_new_eq:NN \calc \fp_eval:n

  \titledquestion{First Question}[5]
  \titledquestion{Second Question}[5]
  \titledquestion{Third Question}[2]
  \titledquestion{Fourth Question}[2]




\calc performs any floating point calculation, which in the above case is rounded to one decimal. Since \numpoints outputs somewhat like a reference (?? if it doesn't exist and \exam@numpoints otherwise), I've made an alternative definition \calcnumpoints which defaults to 0 if \exam@numpoints is not yet defined. This way you can use it in calculations as expected.

| improve this answer | |
  • Is there a "round down", or "floor" function? I ended up with this statement: \calc{round(0.9*\numpoints,0)-1}). I'd prefer a floor statement, but cannot find much documentation on expl3 (see: ctan.org/pkg/expl3). I tried using floor. – Jeff Apr 20 '15 at 15:39
  • @Jeff: This is probably because \exam@numpoints was undefined. I've updated my answer to use a different \calcnumpoints that you can use in calculations. Now, if you want to see the floor, use floor(...,n). The argument n of floor implies a round down (towards negative infinity) to the nearest 10^n. – Werner Apr 20 '15 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.