yaleeconjournal
yaleeconjournal

Reputation: 329

R Markdown pdf_document2 PDF figure and table numbers by section

I want to generate a pdf_document2 PDF document in R Markdown where the tables and figures are labeled according to their section number. For instance, the first figure in Section 2 would be Figure 2.1. I can do that with html_document2 but not with pdf_document2.

See below for the code that I have written. Right now the code generates continuous Figure numbers (Figures 1, 2, 3, 4) instead of Figures 1.1, 1.2, 2.1, 2.2.


---
title: "Testing Section Numbers"
author: "Authors"
date: "January 2019"
output:
  bookdown::pdf_document2:
    fig_caption: yes
    latex_engine: xelatex
    number_sections: true
    toc: yes
    toc_depth: 2
editor_options:
  chunk_output_type: console
link-citations: yes
linkcolor: blue
subparagraph: yes
citecolor: blue
urlcolor: blue
---

# R Markdown

```{r pressure1, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

```{r pressure2, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

# Including Plots

You can also embed plots, for example:

```{r pressure3, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

```{r pressure4, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

Upvotes: 3

Views: 1852

Answers (2)

You don't need the chngcntr package, you can directly use \counterwithin{figure}{section} in your header includes (the command got included into the kernel in spring 2018):

---
title: "Testing Section Numbers"
author: "Authors"
date: "January 2019"
output:
  bookdown::pdf_document2:
    fig_caption: yes
    latex_engine: xelatex
    number_sections: true
    toc: yes
    toc_depth: 2
editor_options:
  chunk_output_type: console
link-citations: yes
linkcolor: blue
subparagraph: yes
citecolor: blue
urlcolor: blue
header-includes:
  - \counterwithin{figure}{section}
---

# R Markdown

```{r pressure1, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

```{r pressure2, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

# Including Plots

You can also embed plots, for example:

```{r pressure3, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

```{r pressure4, echo=FALSE, fig.cap="This is my plot"}
plot(pressure)
```

Upvotes: 1

yaleeconjournal
yaleeconjournal

Reputation: 329

The kludgey solution is to add some LaTeX commands to the header:

header-includes:
  \let\counterwithout\relax
  \let\counterwithin\relax
  \usepackage{chngcntr}
  \counterwithin{figure}{section}

Upvotes: 3

Related Questions