5

I try compiling YiHui's BIG5 example (knitr with chinese content). I used Mac OSX snowleopard, latest RStudio, pdfLatex (same result with XeLatex). This is an excerpt of the raw .Rnw

\begin{document}
\title{knitr與繁體中文文檔}
\author{謝益輝}

測試,咳咳。以下代碼靠譜嗎?

<<test>>=
1+1 # 太簡單了
'引號能用嗎?'
rnorm(5)
(function(){
paste('這裡是字符串')
})()
@

Using the system default encoding "UTF-8" the resulting pdf is

1+1 #
## [1] 2
''
## [1] ""
rnorm(5)
## [1] -0.4290 1.8835 -0.7045 -0.9232 1.5433
(function(){ paste('') })()
## [1] ""

as you can see the chinese are just blank space. Then I saved the .Rnw with BIG5 encoding and compile again. The resulting pdf becomes:

AyyCHUNXaH
1+1 # F
## [1] 2
'H'
## [1] "H"
rnorm(5)
## [1] -0.27859 -0.79428 -0.76903 0.40313 -0.07851
(function(){ paste('oOr') })()
## [1] "oOr"

This time the chinese becomes some random letters.

What else can I do? Completely newbie w.r.t. encoding/locale.

Community
  • 1
  • 1
qoheleth
  • 2,219
  • 3
  • 18
  • 23

2 Answers2

3

You have to set up Chinese fonts for XeLaTeX, which I did not do in my example because I have no idea what fonts I should use for Traditional Chinese, and it is not easy to provide a portable solution across different operating systems because I do not know what fonts are available. You may take a look at the xeCJK package.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • How to set up fonts for xelatex in rmd? – xgdgsc Nov 06 '14 at 02:38
  • 1
    @xgdgsc You may try this package: https://github.com/rstudio/rticles https://github.com/rstudio/rticles/blob/master/inst/rmarkdown/templates/ctex/skeleton/skeleton.Rmd – Yihui Xie Nov 06 '14 at 05:35
0

Hi you may try these code below.

Sys.setlocale(category="LC_ALL",locale="en_US.UTF-8") 

However, it doesn't guarantee the random letter problem would be gone because I had the same problem with my R installation as well. My OS is Chinese. This problem is completely solved when I changed my OS to MAC. Hope it helps.

Julian
  • 33,915
  • 22
  • 119
  • 174