maycca
maycca

Reputation: 4090

Rmarkdown: Modify Figure X label to Figure SX in pdf or word output

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!

Upvotes: 1

Views: 625

Answers (1)

canovasjm
canovasjm

Reputation: 513

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)

Upvotes: 4

Related Questions