[Tex/LaTex] Sweave outputs gibberish

rsweave

I have this .Rnw file:

\documentclass{article}
\begin{document}
<<>>=
data(cars)
fit <- lm(cars$speed~cars$dist)
summary(fit)
@
\end{document}

I am using RStudio to build the PDF file, and the summary outputs all kinds of special characters in the line specifying significance codes. (I don't have enough rep points to post pictures, but here is a screenshot:

enter image description here

In the generated .tex file the line reads as

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

These are probably the wrong type of quotes; I can fix the problem by manually typing ' instead of in the .tex file. I assume it's something to do with Sweave/pdflatex/latex not being able to cope with non-ASCII characters?

Is there anything I can set so that it displays correctly in the PDF file?

Best Answer

The likely cause of this problem is simply that your source file is not encoded in UTF-8 but R is producing UTF-8 output. The first thing you need to do is make sure that your source file is encoded using UTF-8.

Check the General Preferences in R-Studio to see what the default text encoding is. It's best if it's UTF-8, and I think that's the default for R-Studio. If it's not, you can change it to UTF-8.

If your original file was already UTF-8 encoded then you just need to fix the LaTeX source (as described below). If it was not, then you need to resave the file using Save with Encoding from the File menu.

Assuming your file is now encoded correctly, you need to change the source. There are two ways to do this: you can add the inputenc package to your source and use pdfLateX as your engine or you can use XeLaTeX as your engine. The engine choice can be set in the Sweave preferences of R-Studio.

Using pdflatex

Add the following to your preamble:

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

Using xelatex

Add the following to your preamble: (do not use inputenc).

\usepackage{fontspec}