Plot Size and Resolution with R Markdown, Knitr, Pandoc, Beamer

Plot size and resolution with R markdown, knitr, pandoc, beamer

I think that is a frequently asked question about the behavior of figures in beamer slides produced from Pandoc and markdown. The real problem is, R Markdown produces PNG images by default (from knitr), and it is hard to get the size of PNG images correct in LaTeX by default (I do not know why). It is fairly easy, however, to get the size of PDF images correct. One solution is to reset the default graphical device to PDF in your first chunk:

```{r setup, include=FALSE}
knitr::opts_chunk$set(dev = 'pdf')
```

Then all the images will be written as PDF files, and LaTeX will be happy.

Your second problem is you are mixing up the HTML units with LaTeX units in out.width / out.height. LaTeX and HTML are very different technologies. You should not expect \maxwidth to work in HTML, or 200px in LaTeX. Especially when you want to convert Markdown to LaTeX, you'd better not set out.width / out.height (use fig.width / fig.height and let LaTeX use the original size).

Change R Markdown plot width

See this SO answer for how to specify these details in chunks.

To set global chunk options, use opts_chunk$set(out.width='750px', dpi=200) inside your first chunk. See this page on chunk options for more.

How to set size for local image using knitr for markdown?

You can also read the image using png package for example and plot it like a regular plot using grid.raster from the grid package.

```{r fig.width=1, fig.height=10,echo=FALSE}
library(png)
library(grid)
img <- readPNG("path/to/your/image")
grid.raster(img)
```

With this method you have full control of the size of you image.

Figure position in markdown when converting to PDF with knitr and pandoc

I'm not aware of such an option for pandoc to set the floating option of figures when converting a Markdown document to LaTeX. If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like pandoc. Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.

Two ways to go:

  1. use the Rnw syntax (R + LaTeX) instead of Rmd (R Markdown) (examples); then you will be able to use the chunk option fig.pos='H' after you \usepackage{float} in the preamble; in this case, you have full power of LaTeX, and pandoc will not be involved

  2. hack at the LaTeX document generated by pandoc, e.g. something like

    library(knitr)
    knit('foo.Rmd') # gives foo.md
    pandoc('foo.md', format='latex') # gives foo.tex
    x = readLines('foo.tex')
    # insert the float package
    x = sub('(\\\\begin\\{document\\})', '\\\\usepackage{float}\n\\1', x)
    # add the H option for all figures
    x = gsub('(\\\\begin\\{figure\\})', '\\1[H]', x)
    # write the processed tex file back
    writeLines(x, 'foo.tex')
    # compile to pdf
    tools::texi2pdf('foo.tex') # gives foo.pdf

If you do not like these solutions, consider requesting a new feature to pandoc on Github, then sit back and wait.

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

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.



Related Topics



Leave a reply



Submit