# Sweave outputs gibberish

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:

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?

• Try saving your file as UTF-8 and using \usepackage[utf8]{inputenc}. Jun 19, 2013 at 11:04
• I've added this as an answer with some more details. Jun 19, 2013 at 14:52
• You should try knitr instead of Sweave. This is one of the many surprising Sweave problems that knitr has solved. Jun 20, 2013 at 0:14

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

\usepackage[utf8]{inputenc}

Add the following to your preamble: (do not use inputenc).
\usepackage{fontspec}