Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

Gummi 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):

RStudio HTML-output

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.

like image 526
N0rbert Avatar asked Aug 04 '18 21:08

N0rbert


People also ask

What is knitr in R Markdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.

How do I change a document in R Markdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I add text to R Markdown?

Rmd file, you may now add text to your document. You may start with the most basic word processing—simply typing your text below the YAML header section. If you want to start a new paragraph, you can simply hit Enter twice on your key- board, and begin writing again.


1 Answers

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 ...

like image 116
Ralf Stubner Avatar answered Oct 17 '22 06:10

Ralf Stubner