【问题标题】:How to hold figure position with figure caption in pdf output of knitr?如何在knitr的pdf输出中保持带有图形标题的图形位置?
【发布时间】:2015-06-24 03:01:25
【问题描述】:

我正在使用 knitr(1.9.5 和 1.9.17)和 rmarkdown(0.5.3.1),并希望在 pdf 输出中保持图形位置。当使用块选项fig.pos="H" 时,生成的 pdf 文件工作正常。

但是,当fig_caption: yes 设置在 yaml 头文件。

我应该如何解决这个问题?感谢您的任何建议。

编辑:

学习了Latex的浮动环境。我将float 包添加到标题中。

\usepackage{float}

但是在figure 环境中生成的tex 文件总是使用htbp,而对于使用任何fig.pos 选项。手动将htbp更改为H后,所有人物的位置都保持不变。

这是我的 rmd 文件示例:

---
title: "Untitled"
output:
  pdf_document:
    fig_caption: yes
    includes:
        in_header: mystyles.sty
---

# Section 1


Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


```{r fig1, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```

# Section 2

More test

```{r fig2, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```

# Section 3

```{r fig3, echo=FALSE, fig.height=8.5, fig.pos="H"}
plot(cars)
```

More test

【问题讨论】:

  • 我在这里找到了易辉的答案,可能回答了我的问题:stackoverflow.com/questions/16626462/…
  • 我也遇到过类似的问题。我假设 fig.pos 实际上并没有从 .Rmd 解析到 Pandoc,因为在检查 .tex 输出时,它只列出了您的图的 4 个选项 [htpb]。
  • @kristang,正如 Yihui 在他的回答中提到的,我们不能对 Markdown 的格式有太多期望。只需编写一些 R 脚本将 htpb 替换为 H。将发布我的脚本。
  • 当然。我只是觉得很奇怪,当它没有被解析时它被列为一个选项。

标签: r knitr r-markdown


【解决方案1】:

正如 Andrew 所指出的,这个 fig.pos 不能在块中工作,但如果将它放在全局选项中它确实可以工作:

```{r global_options, include=FALSE}
knitr::opts_chunk$set(fig.pos = 'H')
```

编辑:上面显然曾经工作,需要在序言中\usepackage{float}

header-includes:
 \usepackage{float}

另请参阅hereCookbook 了解其他方式。

【讨论】:

  • 我发现它可以工作,除了我必须使用fig.pos = 'h'(即小写而不是大写)...我还发现这个“global_options”块的位置很重要,它必须被放置在 YAML 标头之后和 include=TRUE 的任何块之前。
  • 很好的观察@InTheRed。请注意,据我所知,“global_options”块名称是任意的。重要的部分是knitr::opts_chunk$set() 命令 - 设置 knitr 块默认选项 - 应该在 knitr-ing 期间尽快执行。所以它应该在文档顶部的某个地方。
  • 这对我不起作用,我的数字仍在浮动。
【解决方案2】:

Figure position in markdown when converting to PDF with knitr and pandoc 的代码帮助了我,帮助其他人发现它有用。

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

```{r}
knitr::knit_hooks$set(plot = function(x, options)  {
  knitr::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()
```

【讨论】:

    【解决方案3】:

    对我来说,添加 float 包,然后在 YAML 中添加 \floatplacement{figure}{H} 解决了如下问题:

    ---
    title: "test"
    date: "`r Sys.Date()`"
    output: 
      pdf_document :
        keep_tex: true
        number_sections: true
    header-includes:
     \usepackage{booktabs}
     \usepackage{longtable}
     \usepackage{array}
     \usepackage{multirow}
     \usepackage[table]{xcolor}
     \usepackage{wrapfig}
     \usepackage{float}
     \floatplacement{figure}{H}
    ---
    

    【讨论】:

    • 这解决了fig.cap 让我的情节占一整页的问题
    【解决方案4】:

    对我有用的选项:

    在 .tex 开头:\usepackage{float}

    在 Rmd 的开头:knitr::opts_chunk$set(fig.pos = 'H')H 大写)。

    在每个带有图像的块中:fig.cap="lorem blabla"out.extra=''(参数值=空字符串)。

    需要定义: fig_caption: yeskeep_tex: yes 在 yaml 中。

    这些选项使图像保持其位置,无论是对于include_graphics 还是由 R 代码生成的图。

    我在bookdown 环境中使用它们,按预期生成pdf和html :)

    【讨论】:

    【解决方案5】:

    虽然@Bangyou 提供的答案有效,但它使编织变得复杂。

    您还可以为 Latex 中的图形放置设置全局默认选项,包括在您的 YAML 标头中包括:

    \makeatletter\renewcommand*{\fps@figure}{H}\makeatother
    

    正如here 所解释的(以及here 部分的here)。

    这样你就可以在 RStudio 或 rmarkdown::render 中使用 knit 按钮并完成它。

    问题是,所有数字都被强制填充为 H,您将无法设置一个浮动。

    【讨论】:

    • FWIW,在这里提供的解决方案中,这似乎对我有用。
    • 在我看来,这 1 行 gem 是明确的答案。太可惜了这么少的赞成票!
    • 如果没有为每个块打开 fig.pos = 'h' 选项,这对我不起作用,我添加了一个单独的答案来总结..
    • 对我也不起作用。收到错误 ! LaTeX Error: Unknown float option \'H'.H 更改为 h。不需要将fig.pos 添加到代码块中。一切正常。
    【解决方案6】:

    更新看看这个更好的解决方案here。 (下面的问题总结还是不错的,但是点链接可以找到更好的解决方案)。

    总结一下 RStudio 中的一些测试

    只要 fig_caption: yes 不在 yaml 标头中,knitr 块参数 fig.pos = "H" 就可以工作。

    生成的 .tex 中的每个图形都像这样

    \subsection{my_section}\label{my_section}
    
    \includegraphics{path_to_fig.pdf}
    

    但如果 fig_caption: yes 在 yaml 标头中,则 .tex 看起来像这样

    \subsection{my_section}\label{my_section}
    
    \begin{figure}[htbp]
    \centering
    \includegraphics{path_to_fig.pdf}
    \caption{}
    \end{figure}
    

    fig.pos = "H" 没有被使用,"htbp" 代替。

    使用 RStudio 的解决方法:

    fig_caption: yes
    keep_tex: yes
    

    在 yaml 中以及

    header-includes: \usepackage{float}
    

    然后在生成的.tex文件中搜索[htbp]并将其替换为[H]

    然后在 RStudio 中打开 .tex 文件并使用“编译 PDF”按钮。


    示例.Rmd

    ---
    title: "Testing fig placement with captions"
    author: "Andrew Dolman"
    date: "1 September 2015"
    output: 
      pdf_document: 
        fig_caption: yes
        keep_tex: yes
    header-includes: \usepackage{float}
    ---
    
    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>.
    
    When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
    ```{r}
    summary(cars)
    ```
    
    You can also embed plots, for example:
    
    ```{r, echo=FALSE, fig.pos="H"}
    plot(cars)
    ```
    
    Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
    
    ```{r, echo=FALSE, fig.pos="H"}
    plot(cars)
    ```
    

    【讨论】:

    • 奇迹!非常感谢!
    • 这是一个很大的帮助!只需在 \begin{figure} 之后添加 [htbp] 就可以了 - 它将图形固定到位。
    【解决方案7】:

    正如 Yihui 在他的回答 (Figure position in markdown when converting to PDF with knitr and pandoc) 中提到的,我们不能对 mardown 的格式期望太多。要解决此问题,只需编写一些 R 脚本将 htbp 替换为 H

    与 knitr 包中的 knit 相比,我发现 rmarkdown 中的 render 更好地导出 tex 文件。请记住在 rmarkdown 文件的 yaml 标头中添加 keep_tex: yes

    library(rmarkdown)
    render('filepath.Rmd')
    x <- readLines('filepath.tex')
    pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
    x[pos] <- gsub('htbp', 'H', x[pos])
    writeLines(x, 'filepath.tex')
    tools::texi2pdf('filepath.tex', clean = TRUE)  # gives foo.pdf
    
    file.remove('filepath.tex')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多