Plots in beamer slides converted from .md by pandoc are extremely large

knitr, pandoc, pdf, r-markdown

Solution

The development version of pandoc includes some code in the beamer template that should scale images to the width of the slide. That may help in your case.

You don't need to install development pandoc to use this, since the change is just to a template. Just generate a copy of the default beamer template using `pandoc -D beamer > my.beamer`. Insert the following lines into `my.beamer` after the line `\usepackage{graphicx}`:

\makeatletter
\def\ScaleIfNeeded{%
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother
\setkeys{Gin}{width=\ScaleIfNeeded}

Then use pandoc with the option `--template=my.beamer`.

Problem

Normal plots generated by R `chunks` in R markdown files are exactly there when converted to html slides or pdf. However, when they are converted to beamer slides by `pandoc -t beamer ex.md -V theme:Warsaw -o beamer.pdf `, the plots become extremely large, especially for those generated by `par(mfrow=c(n,m))`, in which case only a little part of the plot is displayed. I tried to fix by setting the chunk option `dev='pdf'`, but it doesn't work out. The plot in html is The plot in beamer is

Original source