[R] [Sweave] doesn't accept unicode?

Yihui Xie xie at yihui.name
Wed Oct 22 06:35:25 CEST 2014


That blog post is not entirely correct about UTF-8: if you use
pdflatex, you have to declare the font encoding
\usepackage[T1]{fontenc} when you save the document with UTF-8, e.g.
the following minimal example should work with pdflatex + knitr in
RStudio with UTF-8:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}
<<>>=
degree <- "°"
print(degree)
@
\end{document}

For XeTeX, you will have to use different packages, fontspec and
xunicode. Here is a minimal example for XeTeX:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xunicode}
\begin{document}
<<>>=
degree <- "°"
print(degree)
@
\end{document}

Similar things apply to ü. You need to specify the argument `encoding
= 'UTF-8'` when calling Sweave() or knitr::knit().

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Web: http://yihui.name


On Tue, Oct 21, 2014 at 10:58 PM, Marc Girondot <marc_grt at yahoo.fr> wrote:
> Look at here for an example using an encoding in knitr:
> http://max2.ese.u-psud.fr/epc/conservation/Girondot/Publications/Blog_r/Entrees/2014/9/4_symbol_in_knitr.html
>
> Sincerely
>
> Marc
>
> Le 22/10/2014 02:45, moonkid at posteo.org a écrit :
>
>> Of course I manage and write my tex-files in unicode (utf-8) (running
>> XeTeX). That is why my R-output need to be in unicode, too.
>>
>> But Sweave doesn't accept unicode files.
>>
>> [R]
>>>
>>> Sweave("analy.Snw")
>>
>> Fehler: ‘analy.Snw’ is not ASCII and does not declare an encoding
>> [/R]
>>
>> [analy.Snw]
>> <<>>=
>> x <- ü
>>
>> table(x)
>> @
>> [/analy.Snw]
>>
>> How should I "declare an encoding". I can not find an option for the <<>>.
>>
>> I don't have to declare any of my tex-files explicite because XeTeX use
>> the files like they come. It knows for itself the encoding.



More information about the R-help mailing list