7

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.

N0rbert
  • 559
  • 5
  • 22

1 Answers1

5

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

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • Thank you very much! PDF output works as expected. But for HTML (`html_document`) and DOC (`word_document`) I get errors: *Quitting from lines 18-28 (tikz-cyrillic.Rmd) Error in engine(options) : Failed to compile tikz; check the template: tikz2pdf-cyr.tex Calls: ... process_group.block -> call_block -> block_exec -> in_dir -> engine Execution halted*. Could you please fix them? And then I mark problem as solved :) – N0rbert Aug 07 '18 at 11:37
  • Thank you again, @RalfStubner ! Now it is working as I expect. – N0rbert Aug 07 '18 at 12:12