Knitr ignoring fig.pos?
The chunk option fig.pos
is only used when knitr thinks it has to write out a LaTeX figure
environment instead of pure Markdown ![]()
, and it writes LaTeX only when a figure caption (fig.cap
) is specified, and at least one of these options has been specified: fig.align
, out.width
, out.extra
. If you want to force knitr to write LaTeX code for figures and use fig.pos
, you may set the chunk option out.extra = ''
.
I know this was answered by Yihui Xie already but I have an alternative solution that avoids the need to include out.extra = ''
or any of the other option that were given while also not interfering with figures that are rendered without captions.
Simply add the latex package 'float'
and use the \floatplacement{figure}{H}
to ensure every figure with a caption is rendered in proper order within the text as you wanted. Alternatively it could be added to the .tex
file used when RMarkdown knits a pdf, but I am fairly new to this and haven't had time to look into that option myself.
I found this fix by looking at the .tex
file in the thesisdown package from Chestar Ismay
It is a fairly easy fix by just adding three lines into the YAML. I don't have enough reputation to post a screen shot of it working but you can just copy what I've done and try it yourself!
---title: "Untitled"author: "Author"date: "27 February 2017"header-includes: #allows you to add in your own Latex packages- \usepackage{float} #use the 'float' package- \floatplacement{figure}{H} #make every figure with caption = houtput: pdf_document: fig_cap: yes keep_tex: yes---```{r setup, include=FALSE}knitr::opts_chunk$set(echo = TRUE, fig.pos= "h")```## R MarkdownThis is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.\newpage## Including PlotsYou can also embed plots, for example:```{r pressure, echo=FALSE, fig.cap = "Hello"}plot(pressure)```Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.