How to keep the position of the figure with the picture in the pdf output of the knife?

I use knitr (1.9.5 and 1.9.17) and rmarkdown (0.5.3.1) and would like to keep the position of the figure in the pdf output. The generated PDF file works fine when using the chunk fig.pos="H" option.

However, the position of the figure is not held when fig_caption: yes is set to yaml.

How do I fix this problem? Thanks for any suggestions.

EDIT:

After exploring the floating point environment in latex. I am adding a float package to the header.

 \usepackage{float} 

But the generated tex file always uses htbp in the figure environment for any fig.pos options. After manually changing htbp to H positions of all the figures will be held.

This is my rmd file example:

 --- 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 
+25
r knitr r-markdown
source share
8 answers

As Yihui said in his answer ( Position of a figure in markdowns when converting to PDF with knitr and pandoc ), we cannot expect too much about formatting from mardown. To work around this problem, just write some R scripts to replace htbp with H

Compared to knit from the knit package, I found that render from rmarkdown is better to export a tex file. Just remember to add keep_tex: yes to the yaml header of your rmarkdown file.

 library(rmarkdown) render('filepath.Rmd') x <- readLines('filepath.tex') pos <- grep('begin\\{figure\\}\\[htbp\\]', x) x[pos] <- gsub('htbp', 'H', x[pos]) writeLines(x, 'filepath.tex') tools::texi2pdf('filepath.tex', clean = TRUE) # gives foo.pdf file.remove('filepath.tex') 
+4
source share

As Andrew noted, this fig.pos does not work in blocks, but it works if it is included in the global parameters:

 '''{r global_options, include=FALSE} knitr::opts_chunk$set(fig.pos = 'H') ''' 

EDIT: the above seems to be used for work and needs \usepackage{float} in the preamble:

 header-includes: \usepackage{float} 

See also here .

+16
source share

Refresh look at this best solution here . (a summary of the problem below is still good, but follow the link for the best solution).

To summarize some tests in RStudio

The argument chitr chrin fig.pos = "H" works until fig_caption: yes is in the yaml header.

Each shape in the generated .tex looks like this:

 \subsection{my_section}\label{my_section} \includegraphics{path_to_fig.pdf} 

But if fig_caption: yes is in the yaml header, then .tex looks like this:

 \subsection{my_section}\label{my_section} \begin{figure}[htbp] \centering \includegraphics{path_to_fig.pdf} \caption{} \end{figure} 

fig.pos = "H" not used, instead is "htbp" .

Workaround for this with RStudio:

put

 fig_caption: yes keep_tex: yes 

in the barley as well

 header-includes: \usepackage{float} 

then search and replace [htbp] with [H] in the generated .tex file

then open the .tex file in RStudio and use the "Compile PDF" button.


Example .Rmd

 --- title: "Testing fig placement with captions" author: "Andrew Dolman" date: "1 September 2015" output: pdf_document: fig_caption: yes keep_tex: yes header-includes: \usepackage{float} --- This 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>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: ```{r} summary(cars) ``` You can also embed plots, for example: ```{r, echo=FALSE, fig.pos="H"} plot(cars) ``` Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot. ```{r, echo=FALSE, fig.pos="H"} plot(cars) ``` 
+13
source share

For me, adding a float package and then \floatplacement{figure}{H} in YAML solved a problem 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} --- 
+10
source share

Although the answer provided by @Bangyou does work, it complicates knitting.

You can also set the default global parameter for placing figures in latex, including in the YAML header:

 \makeatletter\renewcommand*{\fps@figure}{H}\makeatother 

As described here (and here for the \makeat... ).

That way you can just use the knit button in RStudio or rmarkdown :: render and do with it.

The problem is that all the numbers are forcibly filled with H, and you cannot configure it to accommodate.

+6
source share

The option that worked for me:

At the beginning of .tex at the beginning: \usepackage{float} .

At the beginning of Rmd: knitr::opts_chunk$set(fig.pos = 'H') . H is uppercase).

And in each fragment with the image: fig.cap="lorem blabla" and out.extra='' (parameter value = empty line).

No, you need to define: fig_caption: yes and keep_tex: yes in yaml.

These parameters force the image to maintain its position either for include_graphics , or for graphs created with R code.

I used them in a bookdown environment, creating pdf and html as expected :)

+4
source share

In case someone else came across this thread, I had to use a small register 'h' and it worked in version R 3.5.3 and latex 2018.

 '''{r, echo=FALSE, fig.pos="h"} plot(cars) ''' 
0
source share

The code in the markdown figure when converting to PDF with knitr and pandoc helps me, helps someone else find this useful.

 --- title: "Example" author: "Martin" output: pdf_document --- '''{r} knitr::knit_hooks$set(plot = function(x, options) { knitr::hook_plot_tex(x, options) }) ''' '''{r myplot, echo=FALSE, results='hide', fig.cap='Test', fig.pos='h'} library(ggplot2) ggplot(mtcars, aes(mpg, drat)) + geom_point() ''' 
0
source share

All Articles