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

梦想与她 提交于 2019-12-06 06:11:10

问题


Below is my experimental RMarkdown document (named 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}
```

It is based on example from 17.11 of pgfmanual.pdf.

Gummi using TeXLive with XeTeX with simple preamble

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

gives me the following output:

You can test it in OverLeaf.

But in RStudio I can't understand where should I enter preamble for TikZ device, so I have wrong output (HTML as example):

What should I change in RMarkdown document to get correct output in TikZ diagram?

I need the same image appearance for HTML, PDF and Word document (docx).

Note: I'm using Gummi and RStudio 1.1.456 on Ubuntu 16.04 LTS with TeXLive 2015 if it matters.


回答1:


Configuring the knitr engine is possible, see e.g. https://stackoverflow.com/a/51143900/8416610 for references. Your case is different, since you need both PDF and SVG output. Since SVG output uses DVI, we cannot use xelatex for processing the tikz graphic. Instead we have to setup standard latex to output Cyrillic:

---
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}
```

With 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}

Note that here different fonts are used for the image and the main text. At the moment I cannot upload any screen shots ...



来源:https://stackoverflow.com/questions/51689570/how-to-force-tikz-in-rmarkdown-document-to-show-cyrillic-text

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