Flavio Barros - 6 months ago 59
LaTeX Question

How to use R package exams with portuguese accents?

When I'm using the package exams to generate questions, I can do it perfectly in english even with special characters. For example, a Rnw question that I can compile with exams 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)

i <- round((S - P)/P, digits = 2)*100
@

\begin{question}
Qual \'e a 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}

S = P(1+i \times n) \Rightarrow S = P + Pin \Rightarrow

Pin = S - P \Rightarrow
i = \frac{S-P}{Pn} \Rightarrow i = \frac{S-P}{P}

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 ç I just use \c{c} and so on. BUT, I have been trying 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
========

S = P(1+i \times n) \Rightarrow S = P + Pin \Rightarrow

Pin = S - P \Rightarrow
i = \frac{S-P}{Pn} \Rightarrow i = \frac{S-P}{P}

O valor absotulo dos juros é r i%.

Meta-information
================
extype: num
exsolution: r round(i, digits = 3)
exname: Euclidean distance
extol: 0.01


I can compile with RStudio showing the accents correctly:

but when I try:

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


it doen't work.

Could someone help me with this issue?

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

Go to your R library and find the location where the package exams is saved. Inside navigate to the tex folder. There you will find the different templates used by exams. Create a copy of the default template plain.tex and call this duplicate plain_pt.tex. Inside that file you add

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


to the preamble. Save the file.

(The full path on my OSX machine: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/exams/tex)

Now, when calling exams2pdf you can choose your new template with template = plain_pt.

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