1

I am struggling to produce an exam sheet in French using exams2nops. There are accents in the text provided in the intro and title argument of this function and also in the Rnw files containing the function. The formers are correctly displayed in the resulting PDF, but not the later, for example é from a Rnw file is displayed as <U+00E9>.

The call to exams2nops looks like this:

exams2nops(file=myexam, n = N.students, dir = '.',
           name = paste0('exam-', exam.date),
           title = "Examen écrit",
           course = course.id,
           institution = "",
           logo = paste(exams.dir, 'input/logo.jpg', sep='/'),
           date = exam.date,
           replacement = TRUE,
           intro = intro,
           blank=round(length(myexam)/4),
           duplex = TRUE, pages = NULL,
           usepackage = NULL,
           language = "fr",
           encoding = "UTF-8",
           startid = 1,
           points = c(1), showpoints = TRUE,
           samepage = TRUE,
           twocolumn = FALSE,
           reglength = 9,
           header=NULL)

Note that "Examen écrit" is correctly displayed in the final PDF, the problem is with the accent in the Rnw files. The function call yields no error.

The *.tex files by generated by exams2nops, already have the problem. For example, the sentense 'Quarante patients ont été inscrits' in the original Rnw file, becomes 'Quarante patients ont <U+00E9>t<U+00E9> inscrits' in the tex file.

I use exams_2.4-0 with R 4.2.2 with TeXShop 4.70 on OSX 11.6.

I checked that Rnw are utf-8 encoded, for example:

$ file -I question1.Rnw
question1.Rnw: text/x-tex; charset=utf-8

It seems they are utf-8-encoded, indeed. These files were translated with deepl or google translate, then edited in emacs.

I tried setting the encoding parameter of exams2nops to latin-1. It did not help.

Any Idea?

vdet
  • 21
  • 4
  • could you please accept either your own or my answer so that the questions is flagged as resolved here on StackOverflow. You can accept an answer by clicking the checkmark on the left of it. – Achim Zeileis Jan 29 '23 at 15:13

2 Answers2

1

The problem disapeared after setting R 'locales' properly. A recurrent problem with OSX R installs. The symptome is:

During startup - Warning messages:
1: Setting LC_CTYPE failed, using "C" 
2: Setting LC_COLLATE failed, using "C" 
3: Setting LC_TIME failed, using "C" 
4: Setting LC_MESSAGES failed, using "C" 
5: Setting LC_MONETARY failed, using "C" 

at start up. This thread explains how to fix it: Installing R on Mac - Warning messages: Setting LC_CTYPE failed, using "C".

vdet
  • 21
  • 4
0

I'm collecting a few further comments here in addition to the existing answer:

  • The only encoding (beyond ASCII) supported by R/exams, starting from version 2.4-0, is UTF-8. Support for other encodings like latin1 etc. has been discontinued.

  • As only UTF-8 is supported the encoding does not have to be specified in R/exams function calls anymore (as still might be advised in older tutorials).

  • To leverage this support of UTF-8, R has to be configured with a suitable locale. A "C" locate (see the answer by @vdet) is not sufficient.

  • When using R/LaTeX (Rnw) exercises all issues with encodings can also be avoided entirely by using LaTeX commands for special characters, e.g., {\'e}t{\'e} instead of été. The latter is of course more convenient but the former can be more robust, especially when working with teams of instructors living on different operating systems with different locale settings.

  • When using LaTeX commands instead of special characters in R strings (as opposed to the exercise files), then remember that the backslash has to be escaped. For example, the argument title = "Examen écrit" becomes title = "Examen {\\'e}crit".

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49