Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knitr ignoring fig.pos?

I am trying to insert a figure in a RMarkdown document but am having trouble getting it to appear in the right place. The figure below shows the problem: when using a figure caption, the figure appears at the top of the page rather than below the relevant paragraph in the document.

enter image description here

Here is the code for this minimum working example:

---
title: "Untitled"
author: "Author"
date: "27 February 2017"
output: 
  pdf_document:
    fig_cap: yes
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos= "h")
```

## R Markdown

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>.

\newpage

## Including Plots

You 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.

And here is the relevant part of the LaTeX output; note that the fig.pos option is ignored:

You can also embed plots, for example:

\begin{figure}
\centering
\includegraphics{test_files/figure-latex/pressure-1.pdf}
\caption{Hello}
\end{figure}

Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.

My set-up is described below. I'm pretty sure this worked in previous version of knitr, but I don't have a note of which version that might have been.

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] backports_1.0.5 magrittr_1.5    rprojroot_1.2   htmltools_0.3.5 tools_3.3.2    
 [6] yaml_2.1.14     Rcpp_0.12.9     stringi_1.1.2   rmarkdown_1.3   knitr_1.15.1   
[11] stringr_1.2.0   digest_0.6.12   evaluate_0.10  
like image 202
jkeirstead Avatar asked Feb 27 '17 13:02

jkeirstead


People also ask

What is the purpose of knitr?

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.

What does knitr :: Opts_chunk set echo true mean?

The first code chunk: ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` is used to specify any global settings to be applied to the R Markdown script. The example sets all code chunks as “echo=TRUE”, meaning they will be included in the final rendered version.

Does R Markdown use Pandoc?

A recent version of Pandoc (>= 1.12. 3) is required to use the rmarkdown package. RStudio also automatically includes this so you do not need to download Pandoc if you plan to use rmarkdown from the RStudio IDE. If not using the RStudio IDE, you'll need to install Pandoc for your platform.

What is knitr in R Markdown?

Creating documents with R Markdown starts with an . Rmd file that contains a combination of markdown (content with simple text formatting) and R code chunks. The . Rmd file is fed to knitr, which executes all of the R code chunks and creates a new markdown (. md) document which includes the R code and its output.


2 Answers

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 = ''.

like image 103
Yihui Xie Avatar answered Oct 05 '22 16:10

Yihui Xie


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 = h
output: 
  pdf_document:
    fig_cap: yes
    keep_tex: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.pos= "h")
```

## R Markdown

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>.

\newpage

## Including Plots

You 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.
like image 24
bthorne Avatar answered Oct 05 '22 15:10

bthorne