strange symbols generated from rmarkdown when I use Chinese characters in ggplot2

Viewed 146

Recently, I encounter a problem in Rmarkdown. When I add Chinese characters into xlab() and ylab() in ggplot2. The pdf document generated by rmd will display strange ...... symbol. However, I try to search useful something and the result told me I should add dev: cairo_pdf. Ok, after I add the code to YAML, the new error strange symbols occur again. It displays another strange symbols of □□□.

enter image description here

enter image description here I want to ask for help from someone who may pay attention to my quesiton. Could you help me? Thank you very much. And here my test code:

---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: no
    dev: cairo_pdf
  word_document: default
keywords:
header-includes: \usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow}
  \usepackage{wrapfig} \usepackage{float} \floatplacement{figure}{H}
---

library(ggplot2)
ggplot(cars,aes(speed,dist)) + 
  geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")
1 Answers

Sorry, I cannot replicate your images. I just altered the header-includes and included the figure in a code chunk. Did you install xelatex and TeX Live?

Here is the code that worked fine for me:

---
title: "test.Rmd"
author: "author"
documentclass: ctexart
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: no
    dev: cairo_pdf
  word_document: default
header-includes: 
  - \usepackage{booktabs} 
  - \usepackage{longtable} 
  - \usepackage{array} 
  - \usepackage{multirow}
  - \usepackage{wrapfig} 
  - \usepackage{float} 
  - \floatplacement{figure}{H}  
---

```{r}
library(ggplot2)
ggplot(cars,aes(speed,dist)) + 
  geom_line()+xlab("速度\nSpeed") + ylab("距离\nDist")
```

-output

enter image description here

Related