Image cropping/trimming in RMarkdown

Viewed 252

I am very new to RMarkdown. Mainly using it to prepare PDF (Beamer) presentations. I'd like to know whether it is possible to crop the JPG/PNG image using very basic RMarkdown command:

![]()

I know that it is possible to adjust the height by :

![](images/f0_1.jpg){height=250} or to use LaTeX \includegraphics commands to adjust/trim the image (eg.: \includegraphics[trim={5.7cm 0.5cm 6.3cm 0cm},clip, scale=0.26]{f0_1}).

However, I'd love to use ![]() if it is possible. Mainly because it will generate the snippet of the image before I even compile the document.

Also, I'd like to know whether it is possible to insert cropped version the specific page of the PDF document using: ![](). I am able to do it using LaTex command: \includegraphics[page=16, trim={4.3cm 2.5cm 2.4cm 11.4cm},clip, scale=0.65]{oli.pdf.

Thank you very much!

1 Answers

Solution 1.

As far as I know, ![]() hasn't required features.

But if you want to avoid the \includegraphics from LaTeX, you can try next solution from library knitr:

```{r out.extra = c("page = N"), out.width = "85%"}
knitr::include_graphics("your_file.pdf")
```

For the several pages (even from separate files) you can use:

```{r out.extra = c("page = A", "page = B"), out.width = "85%"}
knitr::include_graphics(c("your_file1.pdf", "your_file2.pdf"))
```

Solution 2.

But if you really want to use ![]() - I can advise to you this solution with an intermediate pdf file.

You need to install pdftk if haven't.

\newpage

```{r message=FALSE, echo = FALSE, warning=FALSE, include=FALSE}
system("pdftk input_file.pdf cat 1 output output_file.pdf")
#"cat 1" - you can choose one page, or several pages via "cat 100-200"
```

![](output_file.pdf){height=250} 

+ about cropping look there

Related