0

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}.

Ott Toomet
  • 1,894
  • 15
  • 25

3 Answers3

1

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!
Ott Toomet
  • 1,894
  • 15
  • 25
1

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)

  • This worked, though the slide content is still squished. To get the entire pdf as full-page slides I used: ```{=latex} \end{frame} \includepdf[pages=-,fitpaper=true]{example-image-duck} \begin{frame} ``` – Seb Dec 07 '22 at 04:45
  • @Seb If your included slides have a different aspect ratio, you could use this ratio as class option to avoid squished or scaled pages. – samcarter_is_at_topanswers.xyz Dec 07 '22 at 08:56
0

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)

mb21
  • 34,845
  • 8
  • 116
  • 142
  • Thank you mb21 for the reply. Unfortunately `\includepdf` only works outside of frames. I would like to avoid getting it wrapped in the `frame` environment. – Ott Toomet Sep 22 '17 at 17:32
  • This won't work for two reasons: if `\includepdf` is used inside a frame a) it will be behind the background canvas, which is non-transparent by default, thus hiding the pdf and b) the ten pages you include will superimpose each and only the last one would be visible if the background canvas would not obscure it. – samcarter_is_at_topanswers.xyz Oct 31 '19 at 14:24