6

I have simple RMarkdown document:

---
output:
  word_document: default
  html_document: default
---


```{r,engine='tikz', fig.ext = 'png'}
\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] {label} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {label} (y);
\end{tikzpicture}
```

It is example from 17.11 of pgfmanual.pdf.

HTML output is great if I change 'png' to 'svg':

TikZ SVG image in HTML - looks great

It produces circle and rectangle with tikz engine.
But the resulting image looks ugly and pixelated in DOCX even with 100% zoom:

TikZ image in docx - low resolution

I tried to change fig.width, dpi, out.width but did not get positive results.

The best result for me would me the following: get high resolution image with dimensions as specified in TikZ code.

Is it possible to increase resolution of image inserted to Word document from TikZ?

Update 1 : the proposed solution by @CL with set pandoc's dpi using pandoc_args does not work.

Update 2 : the proposed by @tarleb solution with {r,engine='tikz', engine.opts = list(convert.opts = '-density 800 -resize 800x800'), fig.ext = 'png'}:

---
output:
  word_document: default
  html_document: default
---

```{r,engine='tikz', engine.opts = list(convert.opts = '-density 800 -resize 800x800'), fig.ext = 'png'}
\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] {label} (y);
\draw[->,orange] (x) .. controls +(up:1cm) and +(left:1cm) .. node[above,sloped] {label} (y);
\end{tikzpicture}
```

ends in error:

sh: -density 800 -resize 800x800: command not found
Quitting from lines 8-18 (tikz-sizing.Rmd) 
Error in engine(options) : 
  Failed to compile tikz-sizing_files/figure-docx/unnamed-chunk-1-1.pdf to tikz-sizing_files/figure-docx/unnamed-chunk-1-1.png
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> engine
Execution halted
tarleb
  • 19,863
  • 4
  • 51
  • 80
N0rbert
  • 559
  • 5
  • 22
  • Don't have the time to write a full answer but you should be able to [set pandoc's `dpi`](https://superuser.com/a/1084323/491805) [using `pandoc_args`](https://stackoverflow.com/a/35274738/2706569). – CL. Aug 20 '18 at 13:47
  • 1
    @CL Thank you! But it does not work. Image is still pixelated even if I set `"--dpi", "300"`. – N0rbert Aug 20 '18 at 14:41
  • 1
    Does this work? `{r,engine='tikz', engine.opts = list( convert.opts = '-density 800 -resize 800x800' ), fig.ext = 'png'}`. The problem is that the Ti*k*Z PDF is small, and ImageMagick, which is doing the conversion to png, uses a low dpi count. The above should fix that. – tarleb Oct 02 '18 at 21:06

1 Answers1

5

The issue comes from the way TikZ images are created and converted. Knitr first compiles the code into a PDF file via pdflatex (or luatex/XeLaTeX). The resulting PDF is then converted to PNG via ImageMagick's convert. The PDF contains a vector graphic, while PNG is a pixel-oriented bitmap format; quality of the resulting PNG is limited only by the sampling rate used by the converter. The default used by ImageMagick is 72 dpi, cranking it up to a large value (like 300) will give better results for small images.

R Markdown allows to control the PDF → PNG conversion via the engine.opts setting:

{r, engine='tikz', engine.opts = list( convert = 'convert', convert.opts = '-density 300'), fig.ext = 'png'}

This tells ImageMagick to use a higher sampling rate / pixel density. A value like 300 should be enough, higher values will improve image quality at the cost of greater file sizes.

tarleb
  • 19,863
  • 4
  • 51
  • 80