jke*_*ead 19 r figure knitr r-markdown
我试图在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.
Run Code Online (Sandbox Code Playgroud)
这是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.
Run Code Online (Sandbox Code Playgroud)
我的设置如下所述.我很确定这在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
Run Code Online (Sandbox Code Playgroud)
Yih*_*Xie 22
该块选项fig.pos时才使用knitr认为它有写出一个LaTeX figure代替环境纯粹降价的![](),只有当一个图题(将其写入乳胶fig.cap)被指定,并且这些选项中的至少一个已被指定:fig.align,out.width,out.extra.如果您想强制knitr为数字和使用编写LaTeX代码fig.pos,您可以设置chunk选项out.extra = ''.
小智 16
我知道谢毅辉已经回答了这个问题,但我有一个替代解决方案,可以避免包含out.extra = ''或者给出任何其他选项,同时也不会干扰没有字幕的数字.
只需添加乳胶包'float'并使用它\floatplacement{figure}{H}来确保每个带有标题的图形在文本中按照您想要的正确顺序呈现.或者它可以添加到.texRMarkdown编织pdf时使用的文件中,但我对此很新,并且没有时间自己查看该选项.
我通过查看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"}
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.
Run Code Online (Sandbox Code Playgroud)