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
To center an image using the knitr::include_graphics() function, include it within an R code chunk that has the fig. align='center' option (and perhaps other options to control width, etc.).
To add an image in markdown you must stop text editing, and you do this with the command [Alt text] precedeed by a ! Then you have to add the path to the image in brackets. The path to the image is the path from your directory to the image.
knitr is an engine for dynamic report generation with R. It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of literate programming.
For me adding the float
package and then \floatplacement{figure}{H}
in YAML solved the issue like :
--- title: "test" date: "`r Sys.Date()`" output: pdf_document : keep_tex: true number_sections: true header-includes: \usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow} \usepackage[table]{xcolor} \usepackage{wrapfig} \usepackage{float} \floatplacement{figure}{H} ---
As Andrew pointed out, this fig.pos
doesn't work in chunks, but it does work if it is put in global options:
```{r global_options, include=FALSE} knitr::opts_chunk$set(fig.pos = 'H') ```
EDIT: the above apparently used to work and needs \usepackage{float}
in the preamble:
header-includes: \usepackage{float}
See also here and the Cookbook for some other ways.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With