数字标题,使用knitr的引用和markdown到html

我正在写一个Rmd文件,由knitr处理成HTML。 它包含一些生成数字的R块,它们以HTML中的数据URI的forms存储。

1)如何为这样的图像添加标题? 我想要有一个像“图3:等等等等”的标题,其中“3”是自动生成的。

2)我以后如何引用这个图像,即“如图3所示,等等”。

  1. 您可以在R中用一个简单的计数器创buildgraphics编号; 看到这里的一个例子 。 问题是markdown渲染器是否会为你提供数字标题:R Markdown v1不会,但是v2 (基于Pandoc)会。
  2. 我不知道。 没有直接的方法来插入一个标签作为数字的标识符,所以可能无法将参考数字与纯粹的Markdown交叉。 一旦你有这样的问题,想想(1)我真的需要它吗? (2)如果打算成为结构复杂的文档,我认为最好直接使用LaTeX(Rnw文档)。

我迟到了,但是我想提一下我最近用knitr做数字字幕和交叉引用的一个小软件包。 它被称为kfigr ,你可以使用devtools::install_github('mkoohafkan/kfigr')来安装它。 它仍然在积极发展,但主要function在那里。 一定要看看小插曲,它显示了一些用法的例子,并定义了一些钩子graphics标题和锚点(我可能以后select有包装importknitr和负载定义这些钩子)。

编辑:kfigr现在在CRAN上可用!

也很晚,我在这里改变了艾慧的build议,他还把上面的链接做了参考。

 ```{r functions, include=FALSE} # A function for captioning and referencing images fig <- local({ i <- 0 ref <- list() list( cap=function(refName, text) { i <<- i + 1 ref[[refName]] <<- i paste("Figure ", i, ": ", text, sep="") }, ref=function(refName) { ref[[refName]] }) }) ``` ```{r cars, echo=FALSE, fig.cap=fig$cap("cars", "Here you see some interesting stuff about cars and such.")} plot(cars) ``` What you always wanted to know about cars is shown in figure `r fig$ref("cars")` 

这两种方法的一个方法在这里描述: http : //rmflight.github.io/posts/2012/10/papersinRmd.html

另一个在这里描述(但我不知道你的#2是否)。 http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/

另一个scheme

https://github.com/adletaw/captioner

从自述文件:

 captioner() returns a captioner function for each set of figures, tables, etc. that you want to create. See the help files for more details. For example: > fig_nums <- captioner() > fig_nums("my_pretty_figure", "my pretty figure's caption") "Figure 1: my pretty figure's caption" > fig_nums("my_pretty_figure", cite = TRUE)