2017-02-27 20 views
8

RMarkdown文書に図形を挿入しようとしていますが、正しい場所に表示するのが難しいです。下の図は、問題を示しています。図のキャプションを使用すると、Figureがドキュメントの関連する段落の下に表示されるのではなく、ページの上部に表示されます。ここでfig.posを無視しているKnitr?

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出力の関連する部分です。 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. 

私の設定は以下のとおりです。私はこれが以前のバージョンの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は、私のマシン上で(図は段落の間にある、 '\' TeXファイルに[htbp] {数字}を始める)(同じknitr/rmarkdownバージョンが、私はLinux上でだ)コンパイル。おそらくpandocバージョン(17.1)をチェックしてください。 – scoa

+0

私は1.19.2.1を持っています。ダウングレードする必要があるかもしれません... – jkeirstead

+0

はい、私は1.17.2にダウングレードされ、もう一度うまくいっています。ありがとう。 – jkeirstead

答えて

9

knitrが、それは純粋なMarkdownを![]()の代わりにLaTeXのfigure環境を書き出すために持っていると思ったときに、チャンクオプションfig.posにのみ使用され、それが図のキャプション(fig.cap)が指定されている場合のみにLaTeXを書き込み、これらのオプションのうちの少なくとも1つがfig.align,out.width,out.extraと指定されたがあります。 knitrに数字用のLaTeXコードを書き込ませ、fig.posを使用する場合は、チャンクオプションout.extra = ''を設定することができます。

0

私は、これはすでにYihui Xieで答えた知っているが、私はout.extra = ''かもキャプションなしをレンダリングしている数値と干渉しないが与えられた他のオプションのいずれかを含む必要性を回避し、代替ソリューションを持っています。

ラテックスパッケージ'float'を単に追加して、\floatplacement{figure}{H}を使用して、キャプションが付いているすべての図がテキスト内で適切な順序で確実にレンダリングされるようにします。 これは、RMarkdownがpdfを編成するときに使用される.texファイルに追加することができますが、私はこれをかなり新しくしており、自分自身を調べる時間がありませんでした。

私はそれだけでYAMLに3行を追加することによって、かなり簡単修正ですChestar Ismay

からthesisdownパッケージで.texファイルを見て、この修正プログラムを見つけました。私はそれのスクリーンショットを投稿するのに十分な評判を持っていないが、あなたはちょうど私がやったことをコピーして自分でそれを試すことができます!

--- 
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. 
関連する問題