0

I'm working on R Project trying to knit a PDF in Rmarkdown that includes a svg file.

First I tried:

![svg_figure](folder/file.svg)

And I got this:

! LaTeX Error: Unknown graphics extension: .svg

Then I tried:

```{r figSvg,eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE,fig.height=10}
library(cowplot)
svg_figure<-cowplot::ggdraw()+cowplot::draw_image(folder/file.svg)
plot(svg_figure)
```

But I got a blank space in my pdf, and I had to install the 'magick' library for the image to be drawn.

And once installed, I get this error:

Error in methods::is(image,"magick-image"): object 'folder' not found

The folder does exist, and when I try adding the png file of the same visual scheme, I have no errors.

Does anyone know what might be happening or about a different method to include a svg file into a pdf in Rmarkdown?

Thanks.

cata
  • 23
  • 3
  • The error "object 'folder' not found" is probobly because the parameter should be a string, so `...draw_image("folder/file.svg")`. – chrwahl May 30 '22 at 12:51
  • Maybe this can help you: https://stackoverflow.com/a/46065070/322084 – chrwahl May 30 '22 at 12:53
  • Cool, that was exactly the problem @chrwahl. Thanks – cata May 30 '22 at 14:31
  • Does this answer your question? [Is it possible to include svg image in pdf document rendered by rmarkdown?](https://stackoverflow.com/questions/34064292/is-it-possible-to-include-svg-image-in-pdf-document-rendered-by-rmarkdown) – chrwahl May 30 '22 at 15:18

1 Answers1

0

Pandoc (the converter used by R Markdown) will try to convert the SVG using the rsvg-convert executable. Try to install the respective package on your system to use the simple Markdown syntax.


A more manual approach would be to convert the SVG to TikZ, a TeX package that works a lot like SVG. There are converters, e.g. svg2tikz. You'd include the generated code like this:

```{=tex}
% TikZ code goes here
```

But that will probably require a lot more work, as it bypasses R Markdown and will need manual adjustments. I would only use it if you want to make sure that the image remains a vector graphic.

tarleb
  • 19,863
  • 4
  • 51
  • 80