2

I'm struggling to put a citation inside a figure caption. I'm writing my document in rmarkdown using markdown citations i.e. [@bibtexCitationkey], but using LaTeX for figures and tables. I'd like to be able to put citations within figure and table captions, but I've tried the following without success.

---
title: "Table tests"
csl: harvard-cite-them-right.csl  
output:
  pdf_document: default
  word_document: default
  html_document: default
header-includes:
- \usepackage{graphicx}
- \graphicspath{ {./figs&tabs/} }

bibliography: library.bib
---

\begin{figure}[h]
\begin{center}
\includegraphics{gr1_lrg.jpg}
\caption {Caption text [@Levin2013] or caption text \cite{Levin2013}}

\label{f:ckd}
\end{center}
\end{figure}

This produces: Caption text @Levin2013 or caption text [?]

In the citations in markdown format are working in the rest of the document, there's no problem with my .bib

2
  • Did you try the second syntax without the @, so \cite{Levin2013}?
    – Marijn
    Apr 7, 2021 at 18:35
  • Thanks @Marijn. Just tried that, but same output Apr 7, 2021 at 18:51

1 Answer 1

3

Note that:

  1. It seems that you want also HTML and Word outputs, but LaTeX code in Rmarkdowm only is useful for the PDF output, otherwise is omitted. Maybe not a good idea use it?

  2. Rmarkdown inside LaTeX command or environment is parsed by LaTeX rules. So [@Levin2013] inside a \caption{} is not a cite, is just plain text.

  3. Bibliography in Rmarkdown is processed by default by pandoc-citeproc, so even in the LaTeX version the bibliograhy is not a \bibliography command, nor \printbibliography nor even a thebibliography with \bibitems, but just directly the references with \hypertarget, so \cite-like commands have nothing to do.

  4. You can force the use of a true bibtex or biblatex bibliography, but even then, the above apply, and you cannot nest freely LaTeX in markdown, or markdown in LaTeX.

  5. R chunks is another way to make figure and tables, but add another layer of complexity of how the file is exported, so you cannot use directly markdown nor LaTeX inside fig.cap for instance, although there are tricks to do that (see the example).

  6. Cross-references are limited in basic rmarkdown, but bookdown is a interesting extension of rmarkdown that avoild also the need of use \label{} and/or \ref{} commands in figures, that will work only for PDF outputs.

  7. Not very often, but sometimes in complex documents, recognition of LaTeX code inside Rmarkdown fail miserably. Then is regarded as plain text, causing fatal errors and headaches.

Considering this, there are several options, and one is raw LaTeX floats, but it should be the last option, and without markdown:


mwe


MimimalWorkingExample.Rmd

---
linkcolor: blue
bibliography: /usr/local/texlive/2020/texmf-dist/bibtex/bib/msc/biblio.bib
output:
  bookdown::pdf_document2:
    keep_tex: yes
    toc: no
    citation_package: biblatex
biblio-style: authoryear
biblatexoptions: maxbibnames=9
---

(ref:rudolph) *nice* cite: [@Lam94].

I recommend the figure \@ref(fig:foo) method: 

```{r foo, fig.cap= 'Caption \\emph{with} *very* (ref:rudolph)', echo=F, fig.align = 'center', out.width='10%'}

 knitr::include_graphics("/usr/local/texlive/2020/texmf-dist/tex/latex/mwe/example-image-a.png")
```

You can use also the figure \@ref(fig:bah) method:

![Caption text [@Lam94](\#fig:bah)](example-image-b.png){width=10%}

But I do not recommend the figure \ref{baz} method:

\begin{figure}[h]\centering 
\includegraphics[width=.1\linewidth]{example-image-c}
\caption {Caption text \autocite{Lam94}.\label{baz}}
\end{figure}
7
  • thanks for this clear answer. I had been thinking of starting to use bookdown. I think you're right that I need to avoid using laTeX floats, though I have some large tables that I need packages such as longtable so I think I'll need to keep using LaTeX for tables. In your first example is it necessary to create the (ref:rudolph) outside the code chunk, or can I just include [@citationkey] inside the code chunk? May 3, 2021 at 8:24
  • Also, @Fran, because I use LaTeX for some equations with mathfont I think I will be knitting to PDF for the the final piece of work as the formatting looks best, but my supervisor likes to be able to comment on drafts using microsoft word, hence I'm trying to be able to knit to both as much as possible. May 3, 2021 at 8:27
  • @MarkDavies The [@citationkey] included directly will be regarded as plain text, as it is the *very* word in the first caption of my example. Another alternative \\cite{citationkey} but then will work only for LaTeX. You can a caption inside the chunk, using some like fig.cap=mycap and inside the R chunk mycap <- "My caption" but the problem is the same. If R make the caption, just read plain text.
    – Fran
    May 3, 2021 at 17:03
  • @MarkDavies rmarkdown table are exported to longtable by default, and you can aldo do that from R chunk with with kable or xtable, for instance. I also include LaTeX in Rmardown as far I needed, but if you want a Word version of this mix first export to pure LateX and only then from LaTeX to any compatible format (odt,html, rtf).
    – Fran
    May 3, 2021 at 17:42
  • too bad the recommended approach does not seem to work in case of Word doc output
    – mspices
    Feb 28, 2023 at 9:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .