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

luciano picture luciano · May 18, 2013 · Viewed 59.3k times · Source

I'm trying to control the position of a plot when converting to PDF using knitr and pandoc. My .Rmd file looks this:

# My report

Some text some text some text some text some text some text some text some text some text


```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'}

library(ggplot2)

ggplot(mtcars, aes(mpg, drat)) + geom_point()

```

Some text some text some text some text some text some text some text some text some text

\usepackage{graphicx}
\begin{figure}[placeHere]
  \centering
    \includegraphics[width=0.5\textwidth]{placeHere}
\end{figure}

Some text some text some text some text some text some text some text some text some text

I'm converting to PDF using the functions provided here: http://quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html

How can I place the plot between the second and third blocks of text? The latex code is not working as it currently stands.

EDIT: This is what I'm trying now.

# My report

   ```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=FALSE)
library(ggplot2)
```

```{r, echo=FALSE, fig.height=3}

ggplot(mtcars, aes(disp, hp)) + geom_point()


```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


```{r, echo=FALSE, fig.height=3}



ggplot(mtcars, aes(vs, am)) + geom_point()


```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


```{r, echo=FALSE, fig.height=6}



ggplot(mtcars, aes(disp, cyl)) + geom_point()

```


```{r, echo=FALSE, fig.height=6}

ggplot(mtcars, aes(hp, qsec)) + geom_point()


```


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 




```{r, echo=FALSE, fig.height=3}

ggplot(mtcars, aes(hp, wt)) + geom_point()

```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 



```{r, echo=FALSE, fig.height=5}

ggplot(mtcars, aes(mpg, drat)) + geom_point()

```




Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

Answer

paleo13 picture paleo13 · Nov 19, 2015

I present an alternative solution. Instead of inserting [H] symbols into a latex document in a post-hoc manner, I suggest redefining the figure environment to ignore any position arguments and use [H].

To do this, create a .tex file in the same directory as the .Rmd file which redefines the figure environment, and update the YAML header in the .Rmd to include the file during compilation.

Here is an example of a .tex file:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

Here is example .Rmd which includes it (assuming you called the .tex file 'preamble-latex.tex'):

---
title: "example"
author: "you"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
  rmarkdown::pdf_document:
    fig_caption: yes        
    includes:  
      in_header: preamble-latex.tex
---

```{r, fig.cap='Markdownvellous!'}
plot(1:10, 1:10)
```