tikzDevice does not output code with Umlauts under Windows in UTF-8
I write a report with RMarkdown and use tikzDevice for plotting. When I use German Umlauts (äöüÖÄÜ), RStudio throws the following error:
pandoc.exe: Cannot decode byte '\xd6': Data.Text.Internal.Encoding.streamDecodeUtf8With: Invalid UTF-8 stream
Here is a minimal example:
---
title: "test"
author: "test"
date: "Today"
output:
pdf_document:
keep_tex: true
header-includes:
- \usepackage{tikz}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(tikzDevice)
options(tikzDefaultEngine = "xetex")
```
```{r plot, dev="tikz", external=FALSE}
x <- rnorm(50)
y <- rnorm(50)
plot(x, y, xlab = "ÖÄÜ", ylab = "öäü")
```
With this code, tikzDevice writes the TeX file (plot) with an 1252 encoding, which does not work when included into the main LaTeX document. Therefore Pandoc throws an error. I tried it under Ubuntu and the code works. I suspect, that the Windows encoding is the reason for this problem, but I cannot figure out a solution.
The source file (Rmd) is in the UTF-8 encoding. The generated TeX file (by tikzDevice) is NOT in the UTF-8 encoding.
SessionInfo (Windows):
version R version 3.6.1 (2019-07-05)
os Windows 10 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate German_Germany.1252
ctype German_Germany.1252
tz Europe/Berlin
date 2019-09-04
SessionInfo (Ubuntu):
version R version 3.4.4 (2018-03-15)
os Ubuntu 18.04.3 LTS
system x86_64, linux-gnu
ui X11
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz Europe/Berlin
date 2019-09-04
I can reproduce the behavior. Please open as issue at https://github.com/daqana/tikzDevice/issues. As a workaround you can use