3

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?

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

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.