4

I am looking for a way to automatically number examples throughout an R markdown document.

I know that automatic numbering is possible with a list, such as:

1. Item 1
1. Item 2
1. Item 3

The problem is that this will not work when the items appear in different sections, i.e.

# Section 1
1. Item 1
# Section 2 
1. Item 2

In this case, the numeration resets, so both of these Items end up with the number 1.

How do I resume numeration in R markdown across sections?

Waylan
  • 37,164
  • 12
  • 83
  • 109
Namenlos
  • 475
  • 5
  • 17

1 Answers1

3

There isn't a built-in mechanism for numbering across sections in markdown (or in most of the output formats that you'd be knitting your document into), but there's a hack you could try: you can define and manually increment an R variable to keep track for you. The trade-off is that you'll lose the list formatting. But if you're OK with just numbered items, you can define an R variable in a chunk at the beginning:

```{r, echo=FALSE}
mycounter <- 0
```

Then use it inline (where you're writing normal text, not in a chunk) in your write-up later. You have to increment the variable yourself:

`r mycounter<-mycounter+1; mycounter`. Item 1 

# Another Section

`r mycounter<-mycounter+1; mycounter`. Item 2

This will produce:

  1. Item 1

Another Section

  1. Item 2

As a general point though, numbering across sections is likely to confuse your readers.

cmaimone
  • 546
  • 4
  • 8