2017-02-27 32 views
8

我試圖在RMarkdown文檔中插入一個數字,但無法使它出現在正確的位置。下圖顯示了該問題:使用數字標題時,該數字顯示在頁面的頂部,而不是在文檔的相關段落下。Knitr忽略fig.pos?

enter image description here

下面是這個最小工作示例代碼:

--- 
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的輸出的相關部分;請注意0​​選項被忽略:

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. 

我的設置如下所述。我很確定這在以前版本的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 
+0

你MCVE編譯如預期(圖是段落之間,'\ {開始}人物[htbp]'在TEX文件)在我的機器上(同一knitr/rmarkdown版本,但我在Linux上) 。也許檢查你的pandoc版本(17.1)。 – scoa

+0

我有1.19.2.1。可能需要降級... – jkeirstead

+0

是的,我降級到1.17.2,一切都很好。謝謝。 – jkeirstead

回答

9

knitr認爲它,而不是寫出來的一個LaTeX figure環境單純降價![]()的塊選項fig.pos僅使用,並且指定了圖題(fig.cap)只有當它寫乳膠,這些選項中的至少一個已被指定:fig.align,out.widthout.extra。如果您想強制編寫LaTeX代碼並使用fig.pos,則可以設置塊選項out.extra = ''

0

我知道這已經回答了Yihui Xie,但我有一個避免了需要包括out.extra = ''或任何一邊還與被渲染沒有字幕數據的干擾被賦予了另一種選擇的替代解決方案。

只需添加乳膠包'float'並使用\floatplacement{figure}{H}即可確保每個帶有標題的人物在文本內以正確的順序呈現。 或者它可以被添加到.tex文件,當RMarkdown編織一個pdf時,但我對此很新,並且沒有時間親自查看該選項。

我從Chestar Ismay

.tex文件中的thesisdown包這是剛剛加入三線入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.