如何在Rmarkdown转HTML时为编号列表下的标题添加缩进?
问题分析
Markdown标题属于块级元素,会自动打断编号列表的上下文,导致你写的三级标题无法被识别为列表项3的子元素,手动添加空格无效正是因为这个特性——常规Markdown标题无法直接嵌套在列表结构中。
解决方案
方案一:使用HTML列表结构强制嵌套(最可靠)
直接用HTML的有序列表标签包裹内容,将需要缩进的标题和tabs模块都放在对应的<li>元素内部,这样能严格保持层级关系:
--- title: "Indent heading under list" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
This is a heading
- I have some numbered list items under this heading
- And I want to have sub-heading under this numbered listed
- Under this new sub-heading, I want to have tabs under it
Sub-heading I want
Apple
- This is an apple
Orange
- This is an orange
- I want the above sub-heading to have indentation so that it is indented under item number 3 above
方案二:借助Pandoc扩展的纯Markdown写法
如果你偏好纯Markdown语法,可以启用Pandoc的fenced_divs扩展,用块容器将标题和tabs包裹在列表项内:
--- title: "Indent heading under list" output: html_document: pandoc_args: ["--from=gfm+fenced_divs"] --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)
This is a heading
I have some numbered list items under this heading
And I want to have sub-heading under this numbered listed
Under this new sub-heading, I want to have tabs under it
:::Sub-heading I want {.tabset .tabset-fade .tabset-pills}
Apple
- This is an apple
Orange
- This is an orange
:::
I want the above sub-heading to have indentation so that it is indented under item number 3 above
### 效果说明 两种方案都能让三级标题缩进至列表项3下方,同时保留`tabset`的标签页功能。HTML方案兼容性更好,无需额外配置扩展;纯Markdown方案需要启用Pandoc扩展,但写法更贴近常规Markdown习惯。 内容的提问来源于stack exchange,提问作者benson23
相关产品推荐
相关产品推荐

