sweaver-exams

How do I change the decimal separator in r-exams question to comma?


I'm trying to display a comma as the decimal separator in the questions that I'm creating with the "r-exams/sweave". However, I can't load the siunitx package or do any other configuration that allows this change. I intend to export the question to moodle using the function exams2moodle.


Solution

  • Via LaTeX packages

    LaTeX packages like siunitx or icomma can only be used if the output is produced via LaTeX (e.g., exams2pdf etc.). However, when the LaTeX code is converted to HTML (e.g., as in exams2moodle) I'm not aware of a general solution of converting numbers with decimal point to decimal comma.

    Via options(OutDec = ",") in R

    However, what is relatively simple is to set options(OutDec = ",") within R. This works provided that:

    1. all relevant numbers are either hard-coded in the text with a comma or produced dynamically with r ... or \Sexpr{...}, respectively,
    2. it is assured that the exsolution in num exercises still uses a decimal point.

    As an example, consider the schoice exercise deriv2. It fulfills both items above: 1. all numbers are inserted dynamically from R, 2. it is not a num exercise.

    library("exams")
    options(OutDec = ",")
    exams2html("deriv2.Rmd")
    

    deriv2

    The same also works for the Rnw version of the exercise.

    If the exercise should always produce the numbers with a comma, you can also include the options(OutDec = ",") in the first code chunk at the beginning of the exercise file and revert to options(OutDec = ".") in a code chunk at the end.

    Using the num exercise deriv is also possible. But to assure item 2. you would either need to write fmt(res, decimal.mark = ".") instead of just fmt(res) in the exsolution or alternatively revert to options(OutDec = ".") before adding the meta-information.

    Via fmt(..., decimal.mark = "{,}") in R

    One small disadvantage of the approach above is that viewers with an attention to detail might notice that in LaTeX math mode $...$ a small space is inserted after the comma. See the screenshot above for an example.

    If you want to avoid this, then {,} needs to be used as the decimal separator. Unfortunately, options(OutDec) does not support this as it needs to be a string of length 1. Also, OutDec might not be enough because numbers in math mode need {,} while numbers in plain text need just ,.

    In this case the easiest solution is to leave options(OutDec) at the system default. Instead use fmt(..., decimal.mark = "{,}") for numbers within math mode and fmt(..., decimal.mark = ",") in plain text. To reduce typing you could also add two convenience functions, say:

    cfmt <- function(x, ...) fmt(x, ..., decimal.mark = ",")
    mfmt <- function(x, ...) fmt(x, ..., decimal.mark = "{,}")
    

    Instead of building on the exams::fmt() function you could also use the function base::format(..., decimal.mark = ...) in case that you want to handle any rounding yourself.

    Requirements

    Note that passing decimal.mark to fmt() requires at least version 2.4-0 of R/exams.