Producing pdf with knitr and Rmarkdown: accents in text show up but not in figures.

做~自己de王妃 提交于 2019-12-11 07:17:06

问题


Accents in paragraphs work perfectly. Text in figures is correct in the plot viewer, but once I compile the pdf the accents disappear from the figures.

Here's an example that reproduces the issue.

---
title: 'Some title'
author: 'This be me'
date: '`r format(Sys.Date(), "%B %Y")`'
lang: es
header-includes: 
  - \usepackage{tikz}
output: 
  pdf_document:
    fig_caption: yes
---


```{r global options, echo = F, message=F}
library(knitr)
opts_chunk$set(fig.width=6, fig.height=3.5, dev = 'tikz')
```

I have some paragraphs that include cool accents like áspid. 

If I run the next chunk in the R console I see the accent on the figure it generated. But the accent is missing in the pdf. 


```{r}
plot(pressure, main= "áspid")
```

回答1:


This looks like a bug in the R tikzDevice package. It generates a .tikz file using the UTF-8 encoding, but doesn't include something like

\usepackage[utf8]{inputenc}

in it to declare the encoding. There are a number of references in its documentation to the "Unicode" section of the ?tikzDevice help topic, but there is no such section. You might want to report an issue on the Github page for the package, https://github.com/yihui/tikzDevice.

Edited to add:

After some experimentation, I can't see a way to add that line to the .tikz file, but requesting xelatex instead of the default LaTeX engine does seem to work. You can do that by adding the R code

options(tikzDefaultEngine = "xetex")

into your early code chunk. That will use xelatex for the figures, pdflatex for the rest. ("xetex" is not a typo; that's how you request xelatex.)

If you want, you can switch to xelatex for the rest using latex_engine: xelatex in the YAML header, but the figures will still use pdflatex unless you also add the option setting.



来源:https://stackoverflow.com/questions/46201466/producing-pdf-with-knitr-and-rmarkdown-accents-in-text-show-up-but-not-in-figur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!