knitr: can I cite an article in a figure caption using the fig.cap chunk option?

末鹿安然 提交于 2019-12-30 03:17:39

问题


I'd like to cite an article in my figure caption. I've tried using the Rmarkdown/pandoc [@citekey] and the latex \\citep{citekey} forms in the fig.cap chunk option without any luck.

Here is a reproducible example:

---
output:
  rmarkdown::tufte_handout
references:
- id: Nobody06
  title: 'My Article'
  author:
  - family: Nobody
    given: Jr
  issued:
    year: 2006
---

Some text [@Nobody06].

```{r figure, fig.cap="A figure [@Nobody06]"}
library(ggplot2)
qplot(1:10, rnorm(10))
```

# References

This produces the correct citation in the text block but either [@Nobody06] (when I use the RMarkdown form) or (?) (when I use the Latex form) in the figure caption.

Here is a screencap: .

Does anyone know if it's possible to use citations in thefig.cap field?


回答1:


The bookdown package extends the functionality of rmarkdown and provides some useful tools. Text-references can be used to address this problem. As described by the package author, text references can be used:

You can assign some text to a label and reference the text using the label elsewhere in your document.

This works really well with citations, as shown below:

---
output: bookdown::tufte_handout2
references:
- id: Nobody06
  title: 'My Article'
  author:
  - family: Nobody
    given: Jr
  issued:
    year: 2006
---


(ref:crossref) Some text [@Nobody06].

```{r figure, fig.cap="(ref:crossref)"}
library(ggplot2)
qplot(1:10, rnorm(10))
```

# References

You'll notice that the output format has been adjusted to bookdown::tufte_handout2 which allows the bookdown features to work. You can find a full list of the output formats here.

Read more about text references here: https://bookdown.org/yihui/bookdown/markdown-extensions-by-bookdown.html#text-references



来源:https://stackoverflow.com/questions/33284343/knitr-can-i-cite-an-article-in-a-figure-caption-using-the-fig-cap-chunk-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!