Frank Harrell
Frank Harrell

Reputation: 2230

rmarkdown section numbering depth for html documents

I am numbering sections and specifying table of contents depth using the following in the yaml header:

output:
  html_document:
    toc: yes
    toc_depth: 3
    number_sections: true
    toc_float: 
      collapsed: false
    code_folding: hide
    theme: cerulean

For sections that are automatically numbered, I'm getting depth 4 sections (#### ....) numbered. How can I specify the depth limit for section numbering? I know how to suppress numbering using #### .... {-} but would enjoy having something more automatic.

Upvotes: 4

Views: 2461

Answers (1)

Martin Schmelzer
Martin Schmelzer

Reputation: 23919

I am not aware of any other built in solution. And I think that the effort needed to add {-} is not that high.

Anyways, you could add this chunk at the beginning of your document:

```{r, results='asis', echo = F}
toc_depth <- rmarkdown::metadata$output$html_document$toc_depth
sel <- paste0("h",(toc_depth+1):10, collapse = " > span, ")
cat(paste0("<style>",
           sel, 
           " > .header-section-number { display: none; } </style>"))
```

It reads out the toc_depth YAML option and then prints some lines of CSS to simply hide all elements of class .header-section-number that belong to a heading greater than toc_depth.

Upvotes: 6

Related Questions