how to include pdfpages in rmarkdown for beamer output

2.9k Views Asked by At

I am creating pdf slides using rmarkdown and beamer. When using latex instead of rmarkdown, I can include arbitrary pages from pdf files into my slides with

\usepackage{pdfpages}
...
\includepdf[pages={1-10}]{another_file.pdf}

Is it possible to achieve this with rmarkdown? Note that when just issuing the \includepdf command, intended to be located between slides, pandoc wraps it between \begin{frame} and \end{frame}.

3

There are 3 best solutions below

2
On

In rmarkdown you can use pdfpages like this:

---
output: 
  beamer_presentation:
    keep_tex: true
header-includes:
  - \usepackage{pdfpages}
  - \setbeamercolor{background canvas}{bg=}
  - \makeatletter\beamer@ignorenonframefalse\makeatother
---

test

``` {=latex}
\end{frame}
\includepdf[pages=1-10]{example-image-duck}
\begin{frame}
```

test

(the line \setbeamercolor{background canvas}{bg=} is necessary for beamer versions < 3.64, a patch has been added in 9e3bb9)

3
On

Not a perfect solution but it works (adapted from https://tex.stackexchange.com/questions/11458/error-when-inserting-a-pdf-page-into-a-beamer-presentation?newreg=10fd6a4a46c642118eb4ec905cf87303): use \includegraphics instead of includepdf. This works inside of frames but you have to manyally create a frame for each page you want to insert as it only allows to insert a single page at time.

---
title: "Foo"
output: 
  beamer_presentation:
---

# Stolen image

---

\includegraphics[page=1,width=\paperwidth]{lecture-1-terminal.pdf}

# My own work

## My slide 1

Great stuff!
2
On

As you mentioned, you can inlcude raw TeX in Pandoc Markdown. For me the following works:

$ pandoc -t beamer

foo

\includepdf[pages={1-10}]{another_file.pdf}

bar
^D

which results in:

\begin{frame}

foo

\includepdf[pages={1-10}]{another_file.pdf}

bar

\end{frame}

Maybe you need to update your pandoc version? (I'm currenlty using 2.0 from pandoc nightlies)