【问题标题】:Including a Figure and its caption without including the plot (rmarkdown, knitr)包括一个图形及其标题而不包括情节(rmarkdown,knitr)
【发布时间】:2019-09-24 14:04:42
【问题描述】:

我正在使用rmarkdownknitrrticles package 撰写论文。本文将提交给PLOS ONE,我正在使用rticles R 包中提供的PLOS ONE LaTeX 模板。

PLOS ONE 要求图形标题出现在文本中(图形应该出现的地方),而图形要与主要手稿分开上传。

因此,我想知道是否有办法在使用knitr 生成的 PDF 文件中包含图形标题,而不包含绘图?

下面是所需结果的屏幕截图。 LaTeX 模板也可以在 Overleaf 上找到。

【问题讨论】:

    标签: r r-markdown knitr


    【解决方案1】:

    您可以隐藏情节(最初由 @duckmayer 建议)并在 LaTeX 包 caption 的帮助下添加标题:

    ---
    title: Title of submission to PLOS journal
    author:
      - name: Alice Anonymous
        email: alice@example.com
        affiliation: Some Institute of Technology
        corresponding: alice@example.com
    address:
      - code: Some Institute of Technology
        address: Department, Street, City, State, Zip
    abstract: |
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    
    author_summary: |
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    
    output: rticles::plos_article
    header-includes:
      - \usepackage{caption}
    ---
    
    # Introduction
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    
    ```{r pressure, echo=FALSE,  fig.show='hide', results='asis', fig.path="plots-"}
    plot(pressure)
    cat("\n\\captionof{figure}{Here is a figure caption.}\n")
    ```
    

    结果:

    在这种情况下,要提交的图形名称为plots-pressure-1.pdf

    【讨论】:

      【解决方案2】:

      我会使用块选项 out.widthout.height 将最终输出中的绘图缩小到 0 x 0 尺寸。 (有关所有块选项,请参阅这个出色的文档:https://yihui.name/knitr/options/#plots)。

      例如:

      ---
      title: "SO Answer"
      author: "duckmayr"
      date: "9/24/2019"
      output: html_document
      ---
      
      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = TRUE)
      ```
      
      Here's how to make the figure with the caption but without the plot:
      
      ```{r pressure, fig.cap='Here is a figure caption.', out.width=0, out.height=0}
      plot(pressure)
      ```
      

      更新:绘图文件在哪里?

      如果添加一行如

      knitr::opts_chunk$set(fig.path = "figures/")
      

      到上面的setup 块,绘图将保存在figures/ 子目录中。比如编织后

      ---
      title: "SO Answer"
      author: "duckmayr"
      date: "9/24/2019"
      output: html_document
      ---
      
      ```{r setup, include=FALSE}
      knitr::opts_chunk$set(echo = TRUE)
      knitr::opts_chunk$set(fig.path = "figures/")
      ```
      
      Here's how to make the figure with the caption but without the plot:
      
      ```{r pressure, fig.cap='Here is a figure caption.', out.width=0, out.height=0}
      plot(pressure)
      ```
      

      我可以看到~/figures/中的文件:

      [duckmayr@duckmayr-pc ~]$ ls ~/figures/
      pressure-1.png
      

      (由于您要编织成 PDF,默认情况下,您的绘图图像也将保存为 PDF,而不是 PNG)。

      【讨论】:

      • 感谢您的回答。它也隐藏了标题。
      • 感谢您的破解!我也在使用它,但我想知道是否有另一种(更清洁的)使用 knitr 的方法......更准确和理想地,情节仍然会生成(以便它可以与手稿分开提交)但它不会包含在 PDF 中。
      • @LadislasNalborczyk 该图仍会生成,但根据您的设置,可能会在编织过程中被删除。将很快添加更新以解决此问题。
      猜你喜欢
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2022-06-17
      • 1970-01-01
      • 2017-04-01
      相关资源
      最近更新 更多