Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 863
N0rbert Avatar asked Aug 07 '18 21:08

N0rbert


1 Answers

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.

like image 196
tarleb Avatar answered Sep 18 '22 12:09

tarleb