11

I am having trouble generating \label{} for plots when using knitr to go from a *.Rmd file to a *.md file via knitr; and then converting to *.pdf via pandoc.

An MWE for my *.Rmdis included below:

```{r Setup, include=FALSE, results="hide", warning=FALSE}
opts_chunk$set(dev="cairo_pdf", fig.lp="fig:", echo=FALSE, results="hide", 
               message=FALSE, warning=FALSE)
```

```{r mwe-plot, fig.cap = "MWE plot."}
library(ggplot2)
ggplot(mtcars, aes(factor(cyl))) +
  geom_bar() 
```

I knit:

knit("mwe.Rmd") 

Then I use pandoc

pandoc -o mwe.pdf mwe.md 

I should be able to cross-reference the plot with Figure \ref{fig:mwe-plot} in my *.Rmd source. But it seems that the \label{fig:mwe-plot} hasn't been created in mwe.tex if I run:

pandoc -o mwe.pdf mwe.md

Thank you!

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Tom
  • 223
  • 2
  • 9

1 Answers1

12

The issue is that you are writing a R markdown file and the options related to LaTeX don't work (they have no effect) in such documents. fig.cap works, but fig.lp won't and you won't get any \label{} added at all because the output hook for Rmd documents is markdown and in general there is no label etc there.

In this case you need to write the \label{} manually in fig.cap as if you were adding this explicitly in a LaTeX document. For example:

```{r mwe-plot, fig.cap = "\\label{fig:mwe-plot}MWE plot."}
library(ggplot2)
ggplot(mtcars, aes(factor(cyl))) +
  geom_bar()
```

Now knitr will dump that caption verbatim into the markdown file using the markdown image markup conventions (we need to escape the backslash when entering the string in R, hence the \\ in the fig.cap argument). Pandoc will then be able to work with this caption and the label and the references to it should all resolve themselves.

The other option is more complicated; there is nothing stopping you from writing your own custom hooks to do this for you, but you'll have to study the LaTeX hook and the MD hook to see how to combine elements of both that you need.

Note that this issue (chunk options that pertain to LaTeX outputs) applies to all such chunk options when writing an Rmd file. This is sort of implied in the Options page of the KNitr website but it still caught me by surprise when I first started using Knitr with markdown and using pandoc to render.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • Thanks! I have been hoping to work on "the other option" you mentioned and I just have not found time for it yet :) – Yihui Xie Jun 08 '14 at 05:37
  • I believe that my question is relevant to your answer (it's a bit different though, as I explicitly generate labels for cross-referencing as a part of dynamic chunk generation). Would love to hear from you, Gavin, and/or @Yihui about this: http://stackoverflow.com/q/26883864/2872891. Thanks! – Aleksandr Blekh Nov 13 '14 at 04:02
  • @GavinSimpson, please, how do you afterwards cross-reference the figure? I tried to call chunk name `\@ref{mwe-plot}` in my text like ''Look at figure `\@ref{mwe-plot}`'' but it prints it in a same way, does not correctly add the figure number. I am using `output: bookdown::pdf_book: base_format: rticles::elsevier_article`. Thank you – maycca Jul 28 '20 at 13:45
  • @maycca, just use the raw LaTeX code `\ref{mwe-plot}` directly. – venrey Mar 30 '22 at 01:02