Display Chinese/Japanese/Korean in PDF from Rmd in RStudio

New RStudio shows many useful features to make dynamic documents. We could write Rmd and output word document and PDF. However, when I try to write Chinese in Rmd and convert it into PDF, the Chinese characters is missing. @Yihui added a new feature fig.showtext which allow us to show Chinese in the plot. Still, the Chinese words in the content are missing. I refer to a lot of posts and find the only way might be using the Rnw to write plain tex document. But I just want to use Rmd!

Then I review the PDF generation process and find the key is the md to tex. Rmarkdown use pandoc as the converter and pandoc just use some template. If we want to show Chinese, we need to hack the template and add the support of Chinese. Just go the “R/i686-pc-linux-gnu-library/3.1/rmarkdown/rmd/latex” where is the template located and add \usepackage{xeCJK} and \setCJKmainfont{youfont} before \begin{document}. Youfont stand for the character font installed in your computer which is used to show in the PDF. Remember to use xetex to process the tex or you may try CJK solution. OK, save the hack and now we could use Chinese both in the main text and the plot. More information could be found here.

The same way could be applied for Japanese and Korean. Just add the package support for your language.

update 20140722

@Yixuan write a post on the usage of showtext package in knitr. Also @Yihui showed adding the packages in the header.tex and modified the yaml of the Rmd as the following:

---
output:
  pdf_document:
    includes:
      in_header: header.tex
---

will also solve the font problems. You may also use the tex code in the Rmd and pandoc will complie the code like in the Rnw. Thanks a lot, @Yihui and @Yixuan!

update 20150119

Chinese issue has been solved by the ctex templete in rticles package(on CRAN now). You might just use the following code to install the package.

# install pandoc first
install.packages(c('rmarkdown','rticles'))
rmarkdown::draft("MyCtexArticle.Rmd", template = "ctex", package = "rticles")

updata 20160919

Aftet the update of ctex, use yaml as shown in this post would directly show the Chinese/Japanese/Korean:

---
title: "中文/にほんご/韓國語"
author: "Yufree"
date: "2016年9月19日"
header-includes:
  - \usepackage{ctex}
output: 
  pdf_document:
    latex_engine: xelatex
---

if you preferred xeCJK package to handle this issue, use CJKmainfont: [fontname] in yaml at the top level to set your font.

You might find Rmd templates for Chinese/Janpanese/Korean here.

Miao Yu - vistors walk around since
Published under (CC) BY-NC-SA
comments powered by Disqus