【问题标题】:Include images for pdf and html format包括 pdf 和 html 格式的图像
【发布时间】:2018-07-04 19:31:21
【问题描述】:

我正在尝试找到一种简单的方法将图像包含在 knit pdfshtml 中。编写一组易于转换为任何一种格式的指令会很好。我在这里读到:

Insert picture/table in R Markdown

包含图形的最佳方式是使用代码块。似乎它应该适用于编织 pdf 和 html。但是,它仅包含 pdf 中的图形,而 html 中缺少它。以下是 MWE。

---
title: "a title"
author: "a"
date: "March 3, 2016"

# header-includes: \usepackage{graphicx}
# output:
#   pdf_document:
#     toc: true
#     toc_depth: 3                    # default = 3
#     number_sections: true           # add section numbering to headers

output:
  html_document:                  
    toc: true                     
    toc_float:                    
      collapsed: false              
      toc_depth: 4                  
    number_sections: true         
    highlight: tango              
    theme: simplex
---

### Support Vector Machines (SVMs)

SVM optimal hyperplane:

```{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%', fig.align = 'center'}
# https://bookdown.org/yihui/bookdown/figures.html
knitr::include_graphics("./All_Figures/Chapter8/8.3.pdf")
```

【问题讨论】:

    标签: rstudio markdown


    【解决方案1】:

    简答:您的图片是 PDF 文件


    包含一个最小的、可重现的示例总是有帮助的。由于未提供您的图像的路径,我在这里创建了一个创建本地文件cars.pdf

    ---
    title: Images in Knitr
    output: 
      html_document: default
      pdf_document: default
    ---
    
    ```{r}
    # Create an example plot
    pdf("cars.pdf")
    plot(cars)
    dev.off()
    
    ```
    
    ```{r}
    knitr::include_graphics("cars.pdf")
    ```
    

    比较 HTML 和 PDF,我们可以看到您报告的行为,即图像未显示在 HTML 中:

    问题源于图像被保存为 PDF。据我了解,HTML 没有原生方式来显示此类图像。因此,它不会在输出中显示图像。

    作为一种解决方法,您最好将图形保存为 PNG 或 JPEG。

    【讨论】:

      猜你喜欢
      • 2019-01-29
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2011-06-12
      • 2014-06-05
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多