51

使用 knitr 和 pandoc 转换为 PDF 时,我试图控制绘图的位置。我的 .Rmd 文件如下所示:

# My report

Some text some text some text some text some text some text some text some text some text


```{r myplot, echo=FALSE, fig.pos="placeHere", results='hide'}

library(ggplot2)

ggplot(mtcars, aes(mpg, drat)) + geom_point()

```

Some text some text some text some text some text some text some text some text some text

\usepackage{graphicx}
\begin{figure}[placeHere]
  \centering
    \includegraphics[width=0.5\textwidth]{placeHere}
\end{figure}

Some text some text some text some text some text some text some text some text some text

我正在使用此处提供的功能转换为 PDF:http: //quantifyingmemory.blogspot.co.uk/2013/02/reproducible-research-with-r-knitr.html

如何将绘图放置在第二个和第三个文本块之间?乳胶代码不能像目前那样工作。

编辑:这就是我现在正在尝试的。

# My report

   ```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=FALSE)
library(ggplot2)
```

```{r, echo=FALSE, fig.height=3}

ggplot(mtcars, aes(disp, hp)) + geom_point()


```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


```{r, echo=FALSE, fig.height=3}



ggplot(mtcars, aes(vs, am)) + geom_point()


```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 


```{r, echo=FALSE, fig.height=6}



ggplot(mtcars, aes(disp, cyl)) + geom_point()

```


```{r, echo=FALSE, fig.height=6}

ggplot(mtcars, aes(hp, qsec)) + geom_point()


```


Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 




```{r, echo=FALSE, fig.height=3}

ggplot(mtcars, aes(hp, wt)) + geom_point()

```



Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 



```{r, echo=FALSE, fig.height=5}

ggplot(mtcars, aes(mpg, drat)) + geom_point()

```




Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 
4

8 回答 8

98

我提出了一个替代解决方案。我建议不要以事后的方式将 [H] 符号插入到 Latex 文档中,而是重新定义图形环境以忽略任何位置参数并使用 [H]。

为此,请在与重新定义图形环境的 .Rmd 文件相同的目录中创建一个 .tex 文件,并更新 .Rmd 中的 YAML 标头以在编译期间包含该文件。

这是 .tex 文件的示例:

\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
    \expandafter\origfigure\expandafter[H]
} {
    \endorigfigure
}

这是包含它的示例 .Rmd(假设您将 .tex 文件称为“preamble-latex.tex”):

---
title: "example"
author: "you"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
  rmarkdown::pdf_document:
    fig_caption: yes        
    includes:  
      in_header: preamble-latex.tex
---

```{r, fig.cap='Markdownvellous!'}
plot(1:10, 1:10)
```
于 2015-11-19T10:30:40.903 回答
30

我不知道pandoc在将 Markdown 文档转换为 LaTeX 时设置图形浮动选项的这种选项。如果您选择 Markdown 是因为它的简单性,那么即使使用强大的工具(如pandoc. 底线:Markdown 不是 LaTeX。它是为 HTML 而不是 LaTeX 设计的。

两种方法:

  1. 使用 Rnw 语法(R + LaTeX)而不是 Rmd(R Markdown)(示例);fig.pos='H'然后您将能够\usepackage{float}在序言中使用块选项;在这种情况下,您拥有 LaTeX 的全部功能,并且不会涉及 pandoc

  2. 破解由 pandoc 生成的 LaTeX 文档,例如

    library(knitr)
    knit('foo.Rmd')  # gives foo.md
    pandoc('foo.md', format='latex')  # gives foo.tex
    x = readLines('foo.tex')
    # insert the float package
    x = sub('(\\\\begin\\{document\\})', '\\\\usepackage{float}\n\\1', x)
    # add the H option for all figures
    x = gsub('(\\\\begin\\{figure\\})', '\\1[H]', x)
    # write the processed tex file back
    writeLines(x, 'foo.tex')
    # compile to pdf
    tools::texi2pdf('foo.tex')  # gives foo.pdf
    

如果您不喜欢这些解决方案,请考虑向 Github 上的 pandoc请求新功能,然后坐等。

于 2013-07-15T06:57:42.987 回答
18

我在 RSTUDIO 中使用 KnitR 和 markdown,我的案例的解决方案是在序言中添加\usepackage{float}

    ---
title: "Proyect 2"
author: "FV"
date: "2016-12-3"
output:
  pdf_document:
    fig_caption: yes
    fig_crop: no
    fig_height: 2
    fig_width: 3
    highlight: haddock
    keep_tex: yes
    number_sections: yes
    toc: yes
    toc_depth: 2
  html_document:
    fig_caption: yes
    theme: journal
    toc: yes
    toc_depth: 2
header-includes: 
- \usepackage{graphicx}
- \usepackage{float}
---

然后在第一行添加这行代码(fig.pos='H'):

```{r echo=FALSE,warning=FALSE}
 library(knitr)
  opts_chunk$set(fig.path='figure/graphics-', 
                 cache.path='cache/graphics-', 
                 fig.align='center',
                 external=TRUE,
                 echo=TRUE,
                 warning=FALSE,
                 fig.pos='H'
                )
  a4width<- 8.3
  a4height<- 11.7
```
于 2017-01-12T15:32:59.290 回答
11

如果您正在寻找的只是手动控制放置数字的位置,请使用此网页:http ://www.rci.rutgers.edu/~ag978/litdata/figs/ ,我发现如果您添加一个反斜杠“ \" 在您的绘图命令之后的某处,绘图不会浮动,而是会打印在其当前位置。

如果您只想显示一些图,则可以为每个图修改该选项。

在您的示例中:

# My report

```{r setup, include=FALSE}
# set global chunk options
knitr::opts_chunk$set(cache=FALSE)

library(ggplot2)
```

Some text Some text Some text Some text Some text Some text Some text Some       text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

```{r, echo=FALSE, fig.height=3}
ggplot(mtcars, aes(disp, hp)) + geom_point()
```
\

Some text Some text Some text Some text Some text Some text Some text Some       text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some textSome text Some text Some text 

(ETC)

于 2016-08-31T22:07:10.633 回答
9

我有一些项目从 .Rmd 转换为 .pdf(主要是投影仪幻灯片演示),并希望图表不浮动(浮动数字确实不适用于幻灯片演示)。

我使用的方法是在 .md 文件的行之后添加一个转义空格。这意味着图表在段落内部而不是它自己的段落,这意味着 pandoc 不会将其包装在图形环境中(这也意味着我不能对其使用标题),因此将其准确地放置在那个位置。

我使用 Makefile 为我完成所有转换,因此在运行 R 和 knitr 后,它将自动运行 Perl 脚本(尽管它可以使用 R 或其他工具完成),找到插入图的位置并添加转义空间行尾。

于 2013-07-16T17:57:08.783 回答
6

使用编织钩

我不知何故偶然发现了这个问题,并想添加另一种方法。在这里,我利用了 knitr hooks 提供的出色灵活性。我只是将绘图钩子更改为使用 knitr 函数hook_plot_tex()。之后,我可以使用fig.pos我们在 Rnw 文档中习惯使用的 chunk 选项来定位figure环境(fig.cap必须设置才能调用figure环境)。

这适用于 OP 提供的示例。我想他们也可以在(不知何故)更复杂的例子中工作。为什么这可以很容易地完成并且不是 Rmd 文档的默认设置,我不确定。或许易辉可以澄清一下。

这是代码:

---
title: "Example"
author: "Martin"
output: pdf_document
---

```{r}
knitr::knit_hooks$set(plot = function(x, options)  {
  hook_plot_tex(x, options)
})
```


```{r myplot, echo=FALSE, results='hide', fig.cap='Test', fig.pos='h'}
library(ggplot2)
ggplot(mtcars, aes(mpg, drat)) + geom_point()
```

没有fig.pos='h',情节通常会跳转到第二页。

于 2018-03-20T22:48:47.030 回答
0

这是你所追求的:

```{r setup, include=FALSE}
# set global chunk options
opts_chunk$set(cache=FALSE)
library(ggplot2)
```

# My report

Some text some text some text some text some text some text some text some text some text

Some text some text some text some text some text some text some text some text some text

```{r myplot, echo=FALSE}

ggplot(mtcars, aes(mpg, drat)) + geom_point()

```

Some text some text some text some text some text some text some text some text some text
于 2013-05-22T00:15:57.580 回答
0

解决方案不是太简单,也许其他人将能够简化它。

基本步骤。(Windows 7的)

  1. 您可以将参数添加fig.pos="H"到 knitr 选项中,无论是全局还是每个单独的块。注意大写的 H。这指示 Latex 将图形浮动准确地放置在Rmd文件中调用它们的位置。

  2. 但是,这需要乳胶使用的包,您可以在 pandoc 用于构建 pdf 文件的模板中指定。您可以通过添加行来做到这一点\usepackage{float}

  3. 但是,您首先需要找到当前模板文件进行修改。我在任何地方都找不到这个,但你可以让 pandoc 使用以下命令将模板的内容打印到控制台:pandoc -D latex

  4. 将此模板代码剪切并粘贴到一个空的文本文件中。

  5. 添加行:\usepackage{float}

  6. 以文件名保存在"default.latex"目录中,例如C:\Users\YOURNAME\pandoc\templates

  7. 将选项添加--data-dir=C:/Users/YOURNAME/pandoc/templates"到您对 pandoc 的调用中,或者Pandoc.convert("my file.md", format="pdf", options=c("--data-dir=C:/Users/YOURNAME/pandoc/templates"))如果在 R 中使用pander。

我希望这对你有用。

于 2013-05-22T11:13:36.210 回答