简体   繁体   English

图标题,使用 knitr 和 Markdown 到 html 的引用

[英]figure captions, references using knitr and markdown to html

I'm writing an Rmd file, to be processed by knitr into HTML.我正在编写一个 Rmd 文件,由 knitr 处理成 HTML。 It contains some R chunks that generate figures, which get stored as data URIs in HTML.它包含一些生成图形的 R 块,这些块被存储为 HTML 中的数据 URI。

1) How do I add a caption to such an image? 1) 如何为这样的图像添加标题? I'd like to have a caption that says something like "Figure 3: blah blah blah", where the "3" is automatically generated.我想要一个标题,上面写着“图 3:等等等等”,其中“3”是自动生成的。

2) How do I later on reference this image, ie, "as you can see in Figure 3, blah blah". 2) 稍后我如何引用此图像,即“正如您在图 3 中看到的那样,等等”。

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 . 我迟到了,但是我想提一下我最近建立的一个小包,用于knitr字幕和交叉引用knitr It is called kfigr and you can install it using devtools::install_github('mkoohafkan/kfigr') . 它被称为kfigr ,你可以使用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并在加载时定义这些钩子)。

EDIT: kfigr is now available on CRAN! 编辑:kfigr现在可以在CRAN上使用!

  1. You can create the figure numbers with a simple counter in R; 您可以使用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 v1不会,但v2 (基于Pandoc)将会。
  2. 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. 没有直接的方法来插入标签作为数字的标识符,因此可能无法将参考数字与纯Markdown交叉。 Once you've got issues like this, think (1) do I really need it? 一旦你遇到这样的问题,想想(1)我真的需要吗? (2) if it is intended to be a document with a complicated structure, I think it is better to use LaTeX directly (Rnw documents). (2)如果它是一个结构复杂的文件,我认为最好直接使用LaTeX(Rnw文件)。

Also very late to the party I changed Yihuis suggestion here that he also linked above to do referencing. 派对也很迟,我在这里改变了Yihuis的建议,他也将上面的内容联系起来进行参考。

```{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")`

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). 这里描述了另一个(但我不知道它是否符合你的#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/

Another solution: 另一种方案:

https://github.com/adletaw/captioner https://github.com/adletaw/captioner

From the README: 来自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)

I did both (figure numbers + references) with bookdown.我用 bookdown 做了两个(数字+参考)。 I added in output section in the header of the file:我在文件头的输出部分添加了:

output:
  bookdown::html_document2:
    fig_caption : TRUE

Then I created a figure in a R code chunk like follows:然后我在 R 代码块中创建了一个图形,如下所示:

{r, my-fig-label,echo=F, eval=T, fig.align = 'center', fig.cap="This is my caption"}
knitr::include_graphics(here::here("images", "my_image.png"))

This produces an automatic number under your figure.这会在您的数字下生成一个自动数字。 You can refer to it with \\@ref(fig:my-fig-label) .你可以用\\@ref(fig:my-fig-label)来参考它。

Using the official bookdown documentation 4.10 Numbered figure captions :使用官方bookdown文档4.10 Numbered figure captions

---
output: bookdown::html_document2
---

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

```{r mtcars, fig.cap = "Another amazing plot"}
plot(mpg ~ hp, mtcars)
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM