10

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: screencap.

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

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Tom Harrop
  • 678
  • 2
  • 7
  • 23
  • 1
    It is not easy at the moment, and I have been working on this problem these days. A mature solution may appear in the next one or two months. If you only care about PDF output, what you could do is perhaps `fig.cap = "A figure \\label{refkey}"`, then `\ref{refkey}`. You may need to run `pdflatex` twice on the LaTeX output. I haven't tested it, though. – Yihui Xie Oct 23 '15 at 20:37
  • Thanks for the reply. I'm trying to cite a bibliography reference in the figure caption, not another figure in the same PDF. Is this what you meant? – Tom Harrop Oct 23 '15 at 20:44
  • In that case, you can probably put `\\citep{Nobody06}` in the figure caption, but you have to compile the LaTeX document by yourself since Pandoc cannot resolve citations generated by `\cite{}` by default. Or just use the development version of rmarkdown. – Yihui Xie Oct 24 '15 at 02:04
  • I ran `devtools::install_github('rstudio/rmarkdown')` which installed `rstudio/rmarkdown@eaee966`, but now I get `(?)` in **both** the text block and the figure caption. I see that this is now being rendered as `Some text \citep{Nobody06}.` in the `.tex` file, where it was previously being rendered as `Some text (Nobody 2006)`. I'm also getting a `.bbl` file generated in the directory but it is zero bytes. – Tom Harrop Oct 26 '15 at 09:35
  • Oh I didn't consider this case of using `references` in YAML. I was only considering using `bibliography` in YAML with a .bib file. In the case of `references`, I don't think there is anything we can do in rmarkdown -- it is totally up to Pandoc whether it can parse and render citations in figure captions. I'm not sure if the .bib approach works, though. – Yihui Xie Oct 26 '15 at 17:51
  • I tried with a bibliography file too. The result was the same but I only tested `.bibtex` and not `.bib`. Will check latter tomorrow and report. Thanks. – Tom Harrop Oct 26 '15 at 18:14
  • I get the same problem with `.bib` and `.bibtex` files, and I'm still getting `(?)` with the dev version (`rstudio/rmarkdown@c22ee06`). I've added these details to the question. – Tom Harrop Oct 27 '15 at 09:09

1 Answers1

4

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

enter image description here

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

Michael Harper
  • 14,721
  • 2
  • 60
  • 84