Search code examples
rpdflatexr-markdownfigure

How to remove space between custom figure caption and auto-generated number in RMarkdown?


I'm trying to change the figure captions in my RMarkdown file to have the repeated style of "Figure S1", "Figure S2", and so on. To do this, I tried running the following basic code in RMarkdown:

---
title: "Test"
author: "Shawn Hemelstrand"
date: "`r Sys.Date()`"
output: 
  pdf_document
header-includes:
- \usepackage{caption}
- \captionsetup[figure]{name=Figure S}
---

```{r plot,echo=FALSE, fig.cap="Some informative description."}
plot(iris$Petal.Width,
     iris$Petal.Length)
```

However, you can see from the output below that it creates an unnecessary space between the "S" and the number of the figure.

enter image description here

I want "S1" here and not "S 1". How can I fix this?


Solution

  • You can redefine the figure's counter, e.g.

    ---
    title: "Test"
    author: "Shawn Hemelstrand"
    date: "`r Sys.Date()`"
    format:
      pdf:
        fig-labels: 
    header-includes:
    - \usepackage{caption}
    - \setcounter{figure}{0}
    - \renewcommand{\thefigure}{S\arabic{figure}}
    ---
    
    ```{r plot,echo=FALSE, fig.cap="Some informative description."}
    plot(iris$Petal.Width,
         iris$Petal.Length)
    ```
    

    Result:

    enter image description here