使用knitr::include_graphics将图像插入RMarkdown时的Alt文本?

5
在使用 output: html_document 的 RMarkdown 文档中插入图片时,我们可以使用
```{r pressure, echo=FALSE, fig.cap="A caption", out.width = '100%'}
knitr::include_graphics("path/to/image.png")
```

如何给图片添加 alt文本


1
这是您要找的吗?https://www.rstudio.com/blog/knitr-fig-alt/ - franzbischoff
@franzbischoff 非常酷,看起来是一种新的方法,可以在不需要同时执行a)添加fig.cap和b)设置fig_caption:false的情况下添加alt标签。现在更简单了。 - stevec
1个回答

4

knitr v1.31(2021年1月27日)之后,您可以使用代码块选项 fig.alt 提供 alt 文本。

knitr v1.31 之前,您可以使用代码块选项 fig.cap 生成 alt 文本。如果您查看 .html 输出文件的 HTML 源代码,您将看到带有 alt 属性的 <img> 标签。

<img src="...base64..." alt="A caption" ... />

然而,使用fig.cap的副作用是它也会生成一个图标题。要禁用图标题,可以在输出格式中关闭它,例如:

output:
  html_document:
    fig_caption: false

确实如此!有没有一种方法可以生成无需 fig.cap 选项的 alt 文本?(即针对未加标题的图像?) - stevec
3
是的,在 html_document 中,fig_caption: false:https://bookdown.org/yihui/rmarkdown/html-document.html#figure-options - Yihui Xie
2
嗨Yihui,@franzbischoff指出了一种可能的新方法来添加alt标签。 - stevec
1
@stevec 感谢提醒!我已经相应地更新了答案。 - Yihui Xie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接