Creating a counter that exists across all .rmd files in Bookdown.

Hello all,

I am new to using Bookdown, and as a mathematics lecturer, I would like to create my own theorem/definition/lemma/... environments for use in my lecture notes. This has been relatively successful so far, but the one thing that isn't fallen in to place is the numbering of these environments.

Ideally I would like my theorems/definitions/lemmas/... to be labelled `Theorem X.Y.Z:' where X is the chapter number, Y is the section number and Z shows that the theorem is the Zth theorem/definition/lemma/... in the given section.

In Markdown, the best solution to the problem is to use counters in the style.css file, incrementing with the h1, h2, theorem environments. Something like this:

body {
    counter-reset: h1

.level1 .header-section-number {
    display: none;

.level2 .header-section-number {
    display: none;

h1 {
      counter-reset: h2;

h2 {
      counter-reset: theorem;
h2::before {
     counter-increment: h2;
     content: counter(h1)  "." counter(h2) "   ";

.thm {
.thm:before {
      content: "Theorem " counter(h1) "." counter(h2) "." counter(theorem) "  ";
      counter-increment: theorem; 

However in Bookdown the counters do not seem to exist between the .rmd files. The result being that I have been able to implement counters that behave as desired for the sections and theorems in my book, but not for the chapters. This is because at the beginning of each new .rmd file the counter is being newly defined to 1.

Is there a workaround that I am missing? Is there a hidden counter that tracks chapter number that I can use? How do the default theorem environments handle this (I have been unable to find the .css code for these)? Is there a way to create counters that exist across all the .rmd files?

All help and advice greatly appreciated.


1 Like

I believe the current counters are done by R during the rendering process, and not by CSS. And that would explain why you did not found it.

I don't think bookdown currently supports to tweak the numbering to a X.Y.Z format, and I am not sure how you would do that in a multi HTML website using CSS. (If you know, and share, maybe I can adapt for a R Markdown context)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.