Rmarkdown HTML数字数字 [英] Rmarkdown HTML Number Figures

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

问题描述

有谁知道如何为标题中的数字编号,对于HTML格式的RMarkdown脚本?



对于pdf文档,标题会显示如下内容:

 图X:一些标题文本

然而,HTML版本的等效标题简单地说就是:

 某些标题文本

这使得通过数字交叉引用的数字完全没用。

我的RMD Header指令如下:

  --- 
标题:
我的标题
作者:

日期:
1/1/2016
输出:
pdf_document:
toc:false
number_sections:true
fig_caption:true
html_document:
toc:true
number_sections:true
fig_caption:true
---


解决方案

所以除非有人有更好的解决方案,这就是解决方案我想到了,这种方法存在一些缺陷(例如,如果图形/表格编号依赖于部分编号等),但对于基本的html文档,它的工作原理。



您可以运行以下命令:

  ```{r echo = FALSE} 
#确定文档的输出格式
outputFormat = opts_knit $ get(rmarkdown.pandoc.to)

#Figure和表格标题编号,对于HTML手动执行
capTabNo = 1; capFigNo = 1;

#函数添加表号
capTab = function(x){
if(outputFormat =='html'){
x = paste0(Table ,capTabNo,。,x)
capTabNo <<< capTabNo + 1
}; x
}

#函数添加数字
capFig = function(x){
if(outputFormat =='html'){
x = paste0(图,capFigNo,。,x)
capFigNo <<< capFigNo + 1
}; x
}
```

然后在文档的过程中,如果说你想绘制一个数字:

 ```{r figA,fig.cap = capFig(My Figure Caption )
base = ggplot(data = data.frame(x = 0,y = 0),aes(x,y))+ geom_point()
base
```

capFig 替换为 capTab 在上面,如果你想要一个表格标题。


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

For pdf document, the caption will say something like:

Figure X: Some Caption Text

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

Some Caption Text

This makes cross-referencing figures by number completely useless.

My RMD Header Directives are as follows:

---
title: 
  "My Title"
author: 
  "Me"
date:
  "1/1/2016"
output: 
  pdf_document:
    toc: false
    number_sections: true
    fig_caption: true
  html_document:
    toc: true
    number_sections: true
    fig_caption: true
---

解决方案

So unless someone has a better solution, this is the solution that I came up with, there are some flaws with this approach (for example, if the figure/table number is dependent on the section number etc...), but for the basic html document, it works.

Somewhere at the top of you document, run this:

```{r echo=FALSE}
#Determine the output format of the document
outputFormat   = opts_knit$get("rmarkdown.pandoc.to")

#Figure and Table Caption Numbering, for HTML do it manually
capTabNo = 1; capFigNo = 1;

#Function to add the Table Number
capTab = function(x){
  if(outputFormat == 'html'){
    x = paste0("Table ",capTabNo,". ",x)
    capTabNo <<- capTabNo + 1
  }; x
}

#Function to add the Figure Number
capFig = function(x){
  if(outputFormat == 'html'){
    x = paste0("Figure ",capFigNo,". ",x)
    capFigNo <<- capFigNo + 1
  }; x
}
```

Then during the course of your document, if say you want to plot a figure:

```{r figA,fig.cap=capFig("My Figure Caption")
base = ggplot(data=data.frame(x=0,y=0),aes(x,y)) + geom_point()
base
```

Substitute the capFig to capTab in the above, if you want a table caption.

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

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