如何强制 RMarkdown 文档中的 Tikz 显示西里尔文字?

How to force Tikz in RMarkdown document to show cyrillic text?

下面是我的实验性RMarkdown文档(名为tikz-cyrillic.Rmd):

---
title: "TikZ cyrillic test"
output:
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
    dev: tikz
  html_document: default
  word_document: default
---

```{r,engine='tikz', fig.ext = if (knitr:::is_latex_output()) 'pdf' else 'svg'}
\begin{tikzpicture}
\path (0,0) node
(x) {Hello World!}
(3,1) node[circle,draw](y) {$\int_1^2 x \mathrm d x$};
\draw[->,blue]
(x) -- (y);
\draw[->,red]
(x) -| node[near start,below] {мир!} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {Привет} (y);
\end{tikzpicture}
```

基于示例from 17.11 of pgfmanual.pdf

Gummi 使用 TeXLive 和 XeTeX 以及简单的序言

\usepackage[main=russian,english]{babel}
\usepackage{fontspec}
\setmainfont[Ligatures={TeX,Historic}]{Times New Roman}

给我以下输出:

你可以测试一下in OverLeaf

但是在 RStudio 中我无法理解我应该在哪里输入 TikZ 设备的序言,所以我有错误的输出(HTML 例如):

我应该在 RMarkdown 文档中更改什么才能在 TikZ 图表中获得正确的输出?

我需要 HTML、PDF 和 Word 文档 (docx) 的图像外观相同。

注意:如果重要的话,我在 Ubuntu 16.04 LTS 和 TeXLive 2015 上使用 Gummi 和 RStudio 1.1.456。

可以配置 knitr 引擎,例如参见 供参考。你的情况不同,因为你需要 PDF 和 SVG 输出。由于 SVG 输出使用 DVI,我们不能使用 xelatex 来处理 tikz 图形。相反,我们必须设置标准 latex 来输出西里尔文:

---
title: "TikZ cyrillic test"
output:
  pdf_document:
    keep_tex: yes
    latex_engine: xelatex
  html_document: default
mainfont: Liberation Serif
monofont: Liberation Mono
---

```{r,engine='tikz', fig.ext = if (knitr:::is_latex_output()) 'pdf' else 'svg', engine.opts = list(template = "tikz2pdf-cyr.tex")}
\begin{tikzpicture}
\path (0,0) node
(x) {Hello World!}
(3,1) node[circle,draw](y) {$\int_1^2 x \mathrm d x$};
\draw[->,blue]
(x) -- (y);
\draw[->,red]
(x) -| node[near start,below] {мир!} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {Привет} (y);
\end{tikzpicture}
```

tikz2pdf-cyr.tex:

\documentclass{article}
\usepackage{libertine}
\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{preview}
%% TIKZ_CODE %%
\end{preview}
\end{document}

请注意,此处图片和正文使用了不同的字体。目前我无法上传任何屏幕截图 ...