Rmarkdown中HTML文档的章节编号深度

4

我使用以下内容在yaml头部对节进行编号并指定目录深度:

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

对于自动编号的部分,我得到了深度为4的章节(#### ....)编号。如何指定章节编号的深度限制?我知道可以使用#### .... {-}来取消编号,但更希望有自动化的方法。

1个回答

6

我不知道是否有其他内置的解决方案。但是我认为添加{-}所需的努力并不高。

无论如何,您可以在文档开头添加此代码块:

```{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>"))
```

它读取了toc_depth YAML选项,然后打印出一些CSS行来简单地隐藏所有属于大于toc_depth的标题的.header-section-number类元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接