R Markdown HTML数字图 [英] R Markdown HTML Number Figures

查看:105
本文介绍了R Markdown HTML数字图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于HTML格式的R Markdown脚本,有人知道如何为标题中的数字编号吗?

Does anyone know how to number the figures in the captions, for HTML format R Markdown script?

对于PDF文档,标题将显示如下内容:

For PDF documents, the caption will say something like:

图X:一些字幕文字

Figure X: Some Caption Text

但是,HTML版本的等效标题只是说:

However, the equivalent caption for the HTML version will simply say:

某些字幕文字

Some Caption Text

这使得按数字进行交叉引用完全没有用.

This makes cross-referencing figures by number completely useless.

这是一个最小的示例:

---
title: "My Title"
author: "Me"
output:
  pdf_document: default
  html_document: default
---

```{r cars, fig.cap = "An amazing plot"}
plot(cars)
```


```{r cars2, fig.cap = "Another amazing plot"}
plot(cars)
```

我尝试在每种输出格式中设置tocfig_captionnumber_sections,但这似乎并没有改变结果.

I have tried setting toc, fig_caption and number_sections within each of the output formats, but this does not seem to change the result.

推荐答案

提供的其他答案相对过时,因此使用 bookdown 包使此操作变得非常容易.该软件包提供了许多改进,其中包括Word,HTML和PDF中的内置图形编号.

The other answers provided are relatively out of date, and this has since been made very easy using the bookdown package. This package provides a number of improvements which includes the built-in numbering of figures across Word, HTML and PDF.

要使用 bookdown ,您需要先安装软件包install.packages("bookdown"),然后使用

To be able to use bookdown, you need to first install the package install.packages("bookdown") and then use one of the output formats. For HTML, this is html_document2. Taking your example:

---
title: "My Title"
author: "Me"
date:  "1/1/2016"
output: bookdown::html_document2
---


```{r cars, fig.cap = "An amazing plot"}
plot(cars)
```


```{r cars2, fig.cap = "Another amazing plot"}
plot(cars)
```

这些图将被编号为Figure 1Figure 2.假设代码块已命名并具有标题,我们可以使用语法\@ref(fig:foo)交叉引用输出,其中foo是块的名称,即\@ref(fig-cars).您可以在此处

These Figures will be numbered Figure 1 and Figure 2. Providing the code chunk is named and has a caption, we can cross reference the output using the the syntax \@ref(fig:foo) where foo is the name of the chunk i.e. \@ref(fig-cars). You can learn more about this behaviour here

进一步阅读

  • R Markdown: The definitive Guide: Chapter 11 provides a great overview of bookdown
  • Authoring books with bookdown provides a comprehensive guide on bookdown, and recommended for more advanced details.

这篇关于R Markdown HTML数字图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆