I'm writing an Rmd file, to be processed by knitr into HTML. It contains some R chunks that generate figures, which get stored as data URIs in HTML.
我正在编写一个Rmd文件,由knitr处理成HTML。它包含一些生成数据的R块,这些数据以HTML的形式存储为数据uri。
1) How do I add a caption to such an image? I'd like to have a caption that says something like "Figure 3: blah blah blah", where the "3" is automatically generated.
1)如何为这样的图像添加标题?我希望有这样一个标题:“图3:blah blah blah blah blah blah blah blah”,其中的“3”是自动生成的。
2) How do I later on reference this image, i.e., "as you can see in Figure 3, blah blah".
2)我以后如何参考这张图片,即,“如图3所示,等等”。
5 个解决方案
#1
15
- You can create the figure numbers with a simple counter in R; see one example here. The problem is whether the markdown renderer will render the figure caption for you: R Markdown v1 won't, but v2 (based on Pandoc) will.
- 您可以使用R中的一个简单计数器创建数字;看到一个例子。问题是markdown渲染器是否会为您呈现图形标题:R markdown v1不会,但v2(基于Pandoc)会。
- I do not know. There is no direct way to insert a label as an identifier for figures, so it is probably not possible to cross reference figures with pure Markdown. Once you've got issues like this, think (1) do I really need it? (2) if it is intended to be a document with a complicated structure, I think it is better to use LaTeX directly (Rnw documents).
- 我不知道。没有直接的方法来插入一个标签作为数字的标识符,所以可能不可能用纯标记来交叉引用数据。一旦你遇到这样的问题,想一想(1)我真的需要它吗?(2)如果它是一个结构复杂的文件,我认为最好直接使用LaTeX (Rnw文档)。
#2
24
I'm late to the party, but I wanted to mention a small package I recently built to do figure captioning and cross-referencing with knitr
. It is called kfigr
and you can install it using devtools::install_github('mkoohafkan/kfigr')
. It is still in active development but the main functionality is there. Be sure to check out the vignette, it shows some usage examples and defines some hooks for figure captions and anchors (I may later choose to have the package import knitr
and define those hooks on load).
我参加聚会迟到了,但我想提一下我最近构建的一个小程序包,用于与knitr进行图形字幕和交叉引用。它被称为kfigr,您可以使用devtools: install_github(“mkoohafkan/kfigr”)安装它。它仍在积极开发中,但主要功能仍然存在。一定要检查一下vignette,它显示了一些用法示例,并定义了一些图标题和锚点的钩子(稍后我可能会选择将包导入knitr,并定义那些挂载的钩子)。
EDIT: kfigr is now available on CRAN!
编辑:kfigr现在可以在CRAN上使用!
#3
9
Also very late to the party I changed Yihuis suggestion here that he also linked above to do referencing.
也很晚到党的时候,我在这里改了益慧的建议,他也在上面链接做参考。
```{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")`
#4
5
One way to do both of these is described here: http://rmflight.github.io/posts/2012/10/papersinRmd.html
其中一种方法是:http://rmflight.github.io/posts/2012/10/papersinRmd.html
Another is described here (but I don't know if it does your #2). http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/
这里描述了另一个(但我不知道它是否符合你的第二条)。http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/
#5
2
Another solution:
另一个解决方案:
https://github.com/adletaw/captioner
https://github.com/adletaw/captioner
From the README:
的自述:
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)
#1
15
- You can create the figure numbers with a simple counter in R; see one example here. The problem is whether the markdown renderer will render the figure caption for you: R Markdown v1 won't, but v2 (based on Pandoc) will.
- 您可以使用R中的一个简单计数器创建数字;看到一个例子。问题是markdown渲染器是否会为您呈现图形标题:R markdown v1不会,但v2(基于Pandoc)会。
- I do not know. There is no direct way to insert a label as an identifier for figures, so it is probably not possible to cross reference figures with pure Markdown. Once you've got issues like this, think (1) do I really need it? (2) if it is intended to be a document with a complicated structure, I think it is better to use LaTeX directly (Rnw documents).
- 我不知道。没有直接的方法来插入一个标签作为数字的标识符,所以可能不可能用纯标记来交叉引用数据。一旦你遇到这样的问题,想一想(1)我真的需要它吗?(2)如果它是一个结构复杂的文件,我认为最好直接使用LaTeX (Rnw文档)。
#2
24
I'm late to the party, but I wanted to mention a small package I recently built to do figure captioning and cross-referencing with knitr
. It is called kfigr
and you can install it using devtools::install_github('mkoohafkan/kfigr')
. It is still in active development but the main functionality is there. Be sure to check out the vignette, it shows some usage examples and defines some hooks for figure captions and anchors (I may later choose to have the package import knitr
and define those hooks on load).
我参加聚会迟到了,但我想提一下我最近构建的一个小程序包,用于与knitr进行图形字幕和交叉引用。它被称为kfigr,您可以使用devtools: install_github(“mkoohafkan/kfigr”)安装它。它仍在积极开发中,但主要功能仍然存在。一定要检查一下vignette,它显示了一些用法示例,并定义了一些图标题和锚点的钩子(稍后我可能会选择将包导入knitr,并定义那些挂载的钩子)。
EDIT: kfigr is now available on CRAN!
编辑:kfigr现在可以在CRAN上使用!
#3
9
Also very late to the party I changed Yihuis suggestion here that he also linked above to do referencing.
也很晚到党的时候,我在这里改了益慧的建议,他也在上面链接做参考。
```{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")`
#4
5
One way to do both of these is described here: http://rmflight.github.io/posts/2012/10/papersinRmd.html
其中一种方法是:http://rmflight.github.io/posts/2012/10/papersinRmd.html
Another is described here (but I don't know if it does your #2). http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/
这里描述了另一个(但我不知道它是否符合你的第二条)。http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/
#5
2
Another solution:
另一个解决方案:
https://github.com/adletaw/captioner
https://github.com/adletaw/captioner
From the README:
的自述:
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)