Rmarkdown: cross-reference image included with markdown syntax
Asked Answered
M

3

11

I would like to cross-reference an image which I include with the markdown ![caption with spaces](path/to/image.png) syntax.

I would like to be able to cross-reference this image as \@ref(fig:caption-with-spaces).

I am using bookdown::pdf_document2.

Is this possible?

Mailman answered 15/10, 2020 at 1:13 Comment(3)
Did you mean to use round parentheses, or is the use of curly braces intended? Please double-check.Tolle
I did mean to include curly brackets, as that is the syntax :)Mailman
Apologies, just checked my file - did make a mistake, i meant parentheses, but my question remains :) I'll update the question. Thanks !Mailman
T
8

R Markdown adds a figure ID when generating figures via R, but not for pure Markdown images. In this case, you can add an ID yourself:

![Caption with spaces](path/to/pictures.png){#fig:caption-with-spaces}

The id can be chosen freely, but should start with fig:.


If you'd like a keep everything in pure Markdown, but don't want to add identifiers manually, you can use a pandoc Lua filter:

local stringify = (require 'pandoc.utils').stringify
function Image (img)
  if img.identifier == '' then
    img.identifier = 'fig:' .. stringify(img.caption):gsub('%s', '-'):lower()
    return img
  end
end

Use by adding a pandoc_args parameter to your output settings:

output:
  bookdown::pdf_document2:
    pandoc_args: ['--lua-filter', 'auto-image-ids.lua']
Tolle answered 15/10, 2020 at 9:48 Comment(3)
Ah, nice solution. I didn't realise that this was possibleMent
Thanks @jwalton. I didn't get to upvote your answer yet, would you undelete? The knitr solution is a nice alternative and it would be a loss omit it here.Tolle
Thanks guys, to confirm, after adding this figure id, I can then cross-reference it using \@ref(fig:caption-with-spaces)?Mailman
M
10

Labels can be attached to images included in markdown using the syntax ![caption](path/to/image){#fig:anchor_name}.

That said, there are two further options

  1. Use LaTeX's \includegraphics command.
  2. Use knitr's include_graphics function.

A LaTeX solution would look something like:

\begin{figure}
   \includegraphics{path/to/picture.png}
   \caption{Caption with spaces}
   \label{fig:example}
\end{figure}

A knitr solution would look like

```{r, fig.cap = "Caption with spaces", label="example"}
knitr::include_graphics("path/to/picture.png")
```

With both of these solutions you can cross-reference the resulting image with \ref{fig:example}.

Ment answered 15/10, 2020 at 9:41 Comment(1)
It's important to realise that the fig.cap is essential for the label to work.Ingenerate
T
8

R Markdown adds a figure ID when generating figures via R, but not for pure Markdown images. In this case, you can add an ID yourself:

![Caption with spaces](path/to/pictures.png){#fig:caption-with-spaces}

The id can be chosen freely, but should start with fig:.


If you'd like a keep everything in pure Markdown, but don't want to add identifiers manually, you can use a pandoc Lua filter:

local stringify = (require 'pandoc.utils').stringify
function Image (img)
  if img.identifier == '' then
    img.identifier = 'fig:' .. stringify(img.caption):gsub('%s', '-'):lower()
    return img
  end
end

Use by adding a pandoc_args parameter to your output settings:

output:
  bookdown::pdf_document2:
    pandoc_args: ['--lua-filter', 'auto-image-ids.lua']
Tolle answered 15/10, 2020 at 9:48 Comment(3)
Ah, nice solution. I didn't realise that this was possibleMent
Thanks @jwalton. I didn't get to upvote your answer yet, would you undelete? The knitr solution is a nice alternative and it would be a loss omit it here.Tolle
Thanks guys, to confirm, after adding this figure id, I can then cross-reference it using \@ref(fig:caption-with-spaces)?Mailman
O
0

You can add an image with R as:

{r schematic, echo=FALSE, fig.cap="Diseño de circuito esquemático", fig.align="center"}
knitr::include_graphics("schematic.png")

Then to reference the image: \ref{fig:schematic}

Note: I tried other ways to reference the image like \@ref(fig:schematic) but this did not work for me

Ossetic answered 9/4, 2022 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.