How to use Portuguese accent R-Pack exams?

When I use the exams package to create questions, I can do it perfectly in English even with special characters. For example, an Rnw question that I can compile with the exam package:

<<echo=FALSE, results=hide>>= ## DATA GENERATION P <- round(runif(n = 1, min = 1000, max = 2000), digits = 2) S <- round(runif(n = 1, min = P + 500, max = 3000), digits = 2) ## QUESTION/ANSWER GENERATION i <- round((S - P)/P, digits = 2)*100 @ \begin{question} Qual \'ea taxa de juros simples obtida por uma aplica\c{c}\~ao de \textdollar $\Sexpr{P}$ que, ap\'os um ano, produz um montante de \textdollar$\Sexpr{S}$? \end{question} \begin{solution} Os juros s\~ao calculados por: \begin{equation} S = P(1+i \times n) \Rightarrow S = P + Pin \Rightarrow \end{equation} \begin{equation} Pin = S - P \Rightarrow i = \frac{SP}{Pn} \Rightarrow i = \frac{SP}{P} \end{equation} O valor absotulo dos juros \'e $\Sexpr{i}$\%. \end{solution} %% META-INFORMATION %% \extype{num} %% \exsolution{\Sexpr{fmt(abs(tstat), 3)}} %% \exname{t statistic} %% \extol{0.01} 

For example, when I need it, I just use \ c {c} and so on. BUT, I tried to use Rmarkdown instead of Rnw files. And the same example in Rmd:

 --- output: pdf_document --- ```{r data generation, echo = FALSE, results = "hide"} P <- round(runif(n = 1, min = 1000, max = 2000), digits = 2) S <- round(runif(n = 1, min = P + 500, max = 3000), digits = 2) i <- round((S - P)/P, digits = 2)*100 ``` Question ======== Qual é a taxa de juros simples obtida por uma aplicação de $`r P` Solution ======== Os juros são calculados por: \begin{equation} S = P(1+i \times n) \Rightarrow S = P + Pin \Rightarrow \end{equation} \begin{equation} Pin = S - P \Rightarrow i = \frac{SP}{Pn} \Rightarrow i = \frac{SP}{P} \end{equation} O valor absotulo dos juros é `ri`%. Meta-information ================ extype: num exsolution: `r round(i, digits = 3)` exname: Euclidean distance extol: 0.01 

I can compile with RStudio correctly showing accents:

compilation with RStudio

but when trying:

 exams2pdf('file.Rmd', encoding = 'utf8') 

he does not work.

compilation using exams () functions

Can someone help me with this problem?

+5
source share
2 answers

Thanks to @MartinDabbelJuSmelter for his answer, which essentially captures what happens. However, there are simpler solutions for this. The easiest way:

 exams2pdf("file.Rmd", encoding = "UTF-8", template = "plain8") 

which leads to the desired result. Why is it so complicated and why is it so poorly documented?

This is difficult due to the modular structure of the exams package. There are so many different ways to combine different building blocks, something simple, since it takes two arguments. First, you must declare that file.Rmd is in encoding UTF-8. Secondly, you should use a template that supports UTF-8. The exams package comes with a simple plain template with UTF-8 enabled.

In different encodings, there are no more LaTeX templates, etc., because we expected that almost all users would want to use their own template in any case. For example, to display the name of your university or its course, or use a specific style / font, etc.

If you have only questions with one choice ( schoice ) or multiple choice ( mchoice ), then there is also exams2nops , which offers a standardized format in which it is much easier to use (but also with less settings). For an impression see:

 exams2nops(c("anova.Rmd", "tstat2.Rmd"), language = "pt", encoding = "UTF-8") 

So you cannot use your num file.Rmd question for this. But the big advantage if you include it in the schoice question is that you get an automatic scan and rating for exams2nops .

So now for the Achilles heel: poor documentation. In my defense, I have to say that in section 2.3 (Creating the first exam) vignette("exams2", package = "exams") the encoding problem is briefly discussed, and we recommend that you look at the result of exams_skeleton() to see how it works. So in your case

 exams_skeleton(markup = "markdown", encoding = "UTF-8") 

should have provided some useful examples.

But perhaps it should be easier to find. The best documentation page for exams is high on my wish list - especially for beginners to guide - but this summer I didn't find the time I needed. Hopefully next year ...

PS: The following lines at the beginning of the Rmd file are completely unnecessary and are ignored by exams :

 --- output: pdf_document --- 

The result in which each exercise is exams2 is determined by the exams2 xyz functions, and not by the exercise files themselves. For instance:

 exams2html("foo.Rmd", encoding = "UTF-8", template = "plain8") 

The reason the function (and not the Rmd file) decided about this is simple: users usually want to create a large pool of questions, and then they may want to use the same question in a PDF file (for a written exam) or for Moodle or for direct voting through ARSnova etc. If you had to modify the question pool for this each time (or keep copies), it would be rather cumbersome.

+2
source

I tried different approaches with the arguments header and inputs of exams2pdf() , but none of them gave me the correct result. Also, adding LaTeX commands through the YAML header-includes ( header-includes ) does not work. So I made it the "hard way":


Go to your R library and find the place where the package exams are stored. Inside, go to the tex folder. There you will find various templates used by exams. Make a copy of the default plain.tex template and call this duplicate plain_pt.tex. Inside this file you add

 \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage[portuguese]{babel} 

in the preamble. Save the file.

Full path on my OSX machine:

  /Library/Frameworks/R.framework/Versions/3.3/Resources/library/exams/tex 

On Linux, it could be:

 ~/R/x86_64-pc-linux-gnu-library/3.3/Resources/library/exams/tex 

Now, when calling exams2pdf you can select a new template using template = 'plain_pt' .

This should also work for other languages ​​by adding the appropriate LaTeX commands.

+5
source

All Articles