Knitr 忽略 fig.pos?

Knitr ignoring fig.pos?

我正在尝试在 RMarkdown 文档中插入一个图形,但无法将其显示在正确的位置。下图显示了问题:使用图片标题时,图片出现在页面顶部,而不是文档中相关段落的下方。

这是这个最小工作示例的代码:

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

这里是 LaTeX 输出的相关部分;请注意 fig.pos 选项被忽略:

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.

我的set-up描述如下。我很确定这在以前版本的 knitr 中有效,但我没有记下可能是哪个版本。

> 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  

块选项fig.pos仅在knitr认为它必须写出LaTeX figure环境而不是纯Markdown时使用 ![](),它只在指定图形标题(fig.cap)时写入 LaTeX, 至少指定了以下选项之一:fig.alignout.widthout.extra。如果你想强制 knitr 为图形编写 LaTeX 代码并使用 fig.pos,你可以设置块选项 out.extra = ''.

我知道 Yihui Xie 已经回答了这个问题,但我有一个替代解决方案,可以避免包含 out.extra = '' 或给出的任何其他选项,同时也不会干扰数字没有 字幕。

只需添加 Latex 包 'float' 并使用 \floatplacement{figure}{H} 即可确保每个带有标题的图形都按照您想要的顺序在文本中呈现。 或者 可以将它添加到 RMarkdown 编织 pdf 时使用的 .tex 文件中,但我对此很陌生,还没有时间自己研究该选项。

我通过查看 thesisdown package from Chestar Ismay

中的 .tex 文件找到了此修复程序

只需在 YAML 中添加三行即可轻松修复。我没有足够的声誉 post 它的工作屏幕截图,但你可以复制我所做的并自己尝试!

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