How to make a figure caption in Rmarkdown?
Asked Answered
C

4

18

I am thinking about writing my thesis with rmarkdown and latex. I'm getting the hang of how it all works, however, when I try to add a figure (not an R plot) to the text and render it to pdf, the caption and in-text reference dissappear.

This is the code snippet I use to add a figure:

---
title: "Untitled"
output: pdf_document
---

see figure \ref{fig1}.

![picture \label{fig1}](figure1.png)

This is what knitr creates:

This is what pandoc creates:

Question: How do I make figure captions and in-text references to those figures in Rmarkdown that will display when rendered to pdf?

OR

How do I tell pandoc what Rmarkdown is so it will render R code and plots?

Collectivize answered 26/6, 2015 at 3:22 Comment(0)
L
16

Please see the documentation of R Markdown for PDF output, and in particular, look for fig_caption. Figure captions are turned off by default in R Markdown, and you have to turn them on (fig_caption: true). You can also find this setting from the gear button on the toolbar of RStudio IDE.

Legman answered 26/6, 2015 at 20:39 Comment(1)
Thanks very much! It took me a little bit of playing around but is working now (I did not realize how particular Rmarkdown is with tab spacing to make fig_caption work).Collectivize
W
4

Update: please check https://github.com/yihui/knitr/issues/1063.

Question: How do I make figure captions and in-text references to those figures in Rmarkdown that will display when rendered to pdf?

To get the cross-reference in the PDF produce by LaTeX you need to run LaTeX more than once. Some LaTeX IDE does it for you.

knitr is only running LaTeX once and that is the reason that you only get ??. To confirm that this was the problem I ran

library(knitr)
knitr()

in R that returned

see figure \ref{fig1}.

\begin{figure}[htbp]
\centering
\includegraphics{imagem.jpg}
\caption{picture \label{fig1}}
\end{figure}

which is a valid LaTeX code.

How do I tell pandoc what Rmarkdown is so it will render R code and plots?

Pandoc only understand Markdown (not RMarkdown). First you have to call knitr to generate the Markdown from the RMarkdown and after it call Pandoc to convert the Markdown to LaTeX.

Windcheater answered 26/6, 2015 at 10:57 Comment(0)
P
1

I just found a very useful solution here.

First, include the following chunk:

```{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]]
        })
})
``` 

After, we can add the caption of the figure/table in the figure chunk options like:

```{r, fig.cap=paste("Your caption.")}
  • See that fig.cap works better with paste.
Pearlypearman answered 21/8, 2017 at 16:41 Comment(0)
B
0

In Rmarkdown

for markdown figures

![caption \label{labelx}](link_to_figure, or path of figure){figure dimensions}

# how  to cite markdown figure 
\ref{labelx}

for latex figures

Add following in the figure environment of latex which starts with

\begin{figure}
includegraphics[]{/path to figure}
\label{label}
\end{figure}

# how to cite latex 

\ref{label}

for bookdown

add chunk names as label names like

{r label2,echo=F}

# to cite by using bookdown use 
\@ref(fig:label2)

similarly for tab we cite table as \@ref(tab:table_label)

For captioner package

add a chunk at the top of rmarkdown document

tab_cap <- captioner(prefix = "table")
fig_cap <- captioner(prefix = "figure")

where prefix is the prefix for cross referencing a figure. add this in chunk options

{r fig_cap("labelforcaptioner","caption for captioner figure"}

note that fig_cap is similar to fig_cap in chunk code for captioner. and to cite use

 `r fig_cap("label4",display='cite')
Bolzano answered 14/7, 2022 at 21:47 Comment(1)
Too many pending edits, looks like there is a missing close parenthesis right before closing curly brace below "where the prefix is the prefix"Homeric

© 2022 - 2024 — McMap. All rights reserved.