I am using knitr (1.9.5 and 1.9.17) and rmarkdown (0.5.3.1), and would like to hold figure position in the pdf output. The generated pdf file is working fine when chunk option fig.pos="H"
is used.
However, the figure position is not hold when fig_caption: yes
is set in the
yaml header.
How should I fix this problem? Thanks for any suggestions.
EDIT:
After learning the float environment of Latex. I add float
package into header.
\usepackage{float}
But the generated tex file always use htbp
in the figure
environment regard to any fig.pos
options are used. After manually changing htbp
to H
, positions of all figures are hold.
This is my example of rmd file:
---
title: "Untitled"
output:
pdf_document:
fig_caption: yes
includes:
in_header: mystyles.sty
---
# Section 1
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r fig1, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
# Section 2
More test
```{r fig2, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
# Section 3
```{r fig3, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```
More test
As Andrew pointed out, this
fig.pos
doesn't work in chunks, but it does work if it is put in global options:For me adding the
float
package and then\floatplacement{figure}{H}
in YAML solved the issue like :Update look at this better solution here. (the summary of the problem below is still good, but follow the link to a better solution).
To summarise some testing in RStudio
The knitr chunk argument fig.pos = "H" works as long as
fig_caption: yes
is not in the yaml header.Each figure in the generated .tex looks like this
But if
fig_caption: yes
is in the yaml header then the .tex looks like thisfig.pos = "H"
has not been used,"htbp"
is there instead.A workaround for this using RStudio:
put
in the yaml as well as
then search and replace
[htbp]
with[H]
in the generated .tex filethen open the .tex file in RStudio and use the "Compile PDF" button.
Example .Rmd
Although the answer provided by @Bangyou works, it complicates knitting.
You can also set a global default option for figure placement in latex, including this in your YAML header includes:
As explained here (and here for the
\makeat...
part).This way you can just use the knit button in RStudio or rmarkdown::render and be done with it.
Issue is, all figures fill be forced with H and you won't be able to set one to float around.
As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace
htbp
toH
.Compared with
knit
from knitr package, I foundrender
from rmarkdown is better to export atex
file. Just remember to addkeep_tex: yes
in the yaml header of your rmarkdown file.The option that worked for me:
In the .tex put at the beginning:
\usepackage{float}
.At the beginning of the Rmd:
knitr::opts_chunk$set(fig.pos = 'H')
. TheH
in upper case).And in every chunk with an image:
fig.cap="lorem blabla"
andout.extra=''
(parameter value=empty string).No need to define:
fig_caption: yes
andkeep_tex: yes
in the yaml.These options make the image to keep their position, either for
include_graphics
and the plots generated by R code.I used them in the
bookdown
enviroment, generating the pdf and the html as expected :)