4

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.

Marcos Vinicius
  • 151
  • 1
  • 10

1 Answers1

3

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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • Thank you. The only problem is that fmt(res, decimal.mark = ".") doesn't work specifically in exsolution. – Marcos Vinicius Sep 26 '20 at 02:10
  • 1
    Apologies, you need to use the current development version of the package (soon to be released to CRAN). I forgot that I added this feature after the last release. Simply run: `install.packages("exams", repos="http://R-Forge.R-project.org")` – Achim Zeileis Sep 26 '20 at 07:00
  • Thanks again for the support @achim-zeileis. I did that and nothing has changed. I don't want to abuse of your goodwill but there is an additional problem with using the comma as a decimal separator. It adds extra space in the mathematical environment and this in moodle can only be solved by turning, for example, 3.14 into 3{,}14. In a latex documents this is easily resolved using the icomma package. – Marcos Vinicius Sep 27 '20 at 00:55
  • 1
    Re: nothing changed. Did you properly re-load the package after re-installing the package? I would recommend trying in a new session and make sure that `packageVersion("exams")` is 2.4-0. Re: comma. It is not possible in Moodle to load extra LaTeX packages, see https://tex.stackexchange.com/questions/545491/french-decimal-separator-comma-without-any-package. And in R it is unfortunately not possible to set `OutDec = "{,}"`. So as an alternative, leave `OutDec` unchanged (i.e., `"."`) and instead use `fmt(..., decimal.mark = "{,}")`. – Achim Zeileis Sep 27 '20 at 01:47
  • 1
    As a personal comment: I appreciate the attention to detail and these details are the reason why I personally like to use LaTeX - but I doubt that many of your students care about it in an exam. That's why I don't bother with such fine-tuning. Although in German you would also use the decimal comma, I use a decimal point in all of my books, slides, exercises, etc. As long as students both comma and point are correctly accepted in the solutions that students enter (which is the case in Moodle) I've never had any problem with this. _(Of course, it's ok if your priorities are different, though!)_ – Achim Zeileis Sep 27 '20 at 01:54
  • 1
    I've added the `decimal.mark = "{,}"` solution with some further explanations to the answer. I hope this covers all relevant aspects now. – Achim Zeileis Sep 28 '20 at 07:49
  • Trying to summarize the story: my moodle is configured in portuguese and accepts the correct answer with a comma as a decimal separator. I tried to present to students in the question statement that we would only use decimal separator as dot (.) in the calculations and only in the answer given to the system should be presented with comma (,). The result was not encouraging, for a simple question 25% of students dit it wrong. – Marcos Vinicius Sep 28 '20 at 22:20
  • Present the random values with \Sexpr{} is simple, with the Sexpr and fmt is duable, but I hope that in future we can do the same thing in a more "transparent" way without the dependence of the decimal separator string. – Marcos Vinicius Sep 28 '20 at 22:20
  • Don't get me wrong, okay? Your library is fantastic. I only raised this issue to make it even better. – Marcos Vinicius Sep 28 '20 at 22:30
  • 1
    I've edited my answer to help making the modifications easier. Re: making the package do this automatically. I don't see a sufficiently simple and robust way to do it. Replacing all "." with either "," or "{,}" is difficult because math mode has to be detected and, more importantly, "." might be used as a thousands delimiter which is even harder to auto-detect. Hence, exercise authors really have to take care of it. My answer should provide enough detail that they can make an informed choice what works best for them. If you feel that this still isn't "acceptable" that is, of course, up to you. – Achim Zeileis Sep 30 '20 at 02:02