1

I am trying to use Rmarkdown to automatically label Figure's caption, but instead of counting Figure 1, I want it to be Figure S1, i.e. just adding S there. An example here and here suggests that this is not possible using pdf output. Ok, I am fine with .doc file, but still my figure caption is not print out? I wonder what could be wrong?

Minimal example for R markdown:

    ---
title: Supporting Information
subtitle: "Iron(I) etc"
author: "Some people here"
abstract: "Added the addresses here since there is no abstract in the SI"
output:
  word_document:
    fig_caption: yes
---

```{r, include=F}
library(captioner)

figS<- captioner(prefix = "Figure S", auto_space = TRUE, levels = 1, type = NULL,  infix = ".")

figS("Figure S1", "Single-crystal X-ray structure of some text (1)", display=FALSE)

```

```{r Xray, fig.cap=figS("This is my figure description"), echo=FALSE}
plot(cars)
```

This prints correctly out Figure S1. But, now my actual figure description is missing?

enter image description here

My desired output is pdf, but if not, I can do with word. Thanks for suggestion how to fix this!

maycca
  • 3,848
  • 5
  • 36
  • 67
  • 1
    I edited my answer to remove the blank space between `S` and the number. So, instead of `Figure S 1` now we have `Figure S1` – canovasjm Mar 25 '21 at 21:20

1 Answers1

4

You can use some LaTeX commands under header-includes: in the YAML section, as follows:

---
title: "Untitled"
output: pdf_document
header-includes:
  - \renewcommand{\figurename}{Figure S}
  - \makeatletter
  - \def\fnum@figure{\figurename\thefigure}
  - \makeatother
---

```{r, fig.cap="This is my figure description", fig.height=3}
plot(pressure)
```

```{r, fig.cap="Another figure description", fig.height=3}
plot(iris$Sepal.Length, iris$Petal.Width)
```

enter image description here

(The argument fig.height in the R code chunks is not necessary; I used it only to get both plots in the same page and take the screenshot)

canovasjm
  • 501
  • 1
  • 3
  • 11