我试图在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"}绘图(压力)
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.我的设置如下所示。我很确定这在之前的版本中是有效的,但是我不知道是哪一个版本。
> 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 发布于 2017-04-06 12:08:42
chunk选项fig.pos仅在认为必须写出LaTeX figure环境而不是纯Markdown ![]()时使用,并且仅当指定了图形标题(fig.cap)时才写入LaTeX,并且至少指定了以下选项之一:fig.align、out.width、out.extra。如果您想要强制knitr为图形编写LaTeX代码并使用fig.pos,您可以设置chunk选项out.extra = ''。
发布于 2018-01-06 03:54:28
我知道Yihui Xie已经回答了这个问题,但我有一个替代的解决方案,它可以避免包含out.extra = ''或任何其他给出的选项,同时也不会干扰没有字幕的图形。
只需添加latex包'float'并使用\floatplacement{figure}{H}来确保每个带有标题的图形在文本中按照您想要的顺序呈现。或者它可以添加到RMarkdown编织pdf时使用的.tex文件中,但我对此相当陌生,还没有时间自己研究这个选项。
我通过查看来自Chestar Ismay的包中的.tex文件找到了这个修复
只需在YAML中添加三行代码,就可以很容易地解决这个问题。我没有足够的声誉来发布它工作的屏幕截图,但你可以复制我所做的并亲自尝试!
---
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"}绘图(压力)
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.https://stackoverflow.com/questions/42486617
复制相似问题