You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R Markdown中创建双重嵌套标签页?

好问题!默认情况下,R Markdown的原生.tabset功能只支持最多两层嵌套标签页——这就是你用<div>加四级标题没生效的原因。不过别担心,有两种靠谱的方法能实现你想要的三层嵌套标签页,我来给你详细拆解:

方法一:手动用Bootstrap组件构建第三层标签页(适配原生html_document)

因为原生html_document依赖Bootstrap框架,我们可以直接用Bootstrap的Tab组件手动搭建第三层标签页,替换你原来的四级标题部分即可。下面是完整的可运行代码:

---
output:
  html_document:
    theme: paper
    highlight: tango
    number_sections: false
    toc: false
    toc_float: false
---
# Result Discussion {.tabset}
We will discuss results here
## Quarterly Results {.tabset}
This content pertains to Quarterly Results
### By Product
Quarterly performance by Products

<!-- 手动构建第三层Bootstrap标签页 -->
<ul class="nav nav-tabs" id="productCaseTabs" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="caseA-tab" data-bs-toggle="tab" data-bs-target="#caseA" type="button" role="tab" aria-controls="caseA" aria-selected="true">Case A</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="caseB-tab" data-bs-toggle="tab" data-bs-target="#caseB" type="button" role="tab" aria-controls="caseB" aria-selected="false">Case B</button>
  </li>
</ul>
<div class="tab-content" id="productCaseTabsContent">
  <div class="tab-pane fade show active" id="caseA" role="tabpanel" aria-labelledby="caseA-tab">
    Quarterly performance by Products in Case A
  </div>
  <div class="tab-pane fade" id="caseB" role="tabpanel" aria-labelledby="caseB-tab">
    Quarterly performance by Products in Case B
  </div>
</div>

End of section quarterly performance by Products

### By Region
Quarterly performance by Region

<!-- 同样构建Region下的第三层标签 -->
<ul class="nav nav-tabs" id="regionCaseTabs" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="regionCaseA-tab" data-bs-toggle="tab" data-bs-target="#regionCaseA" type="button" role="tab" aria-controls="regionCaseA" aria-selected="true">Case A</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="regionCaseB-tab" data-bs-toggle="tab" data-bs-target="#regionCaseB" type="button" role="tab" aria-controls="regionCaseB" aria-selected="false">Case B</button>
  </li>
</ul>
<div class="tab-content" id="regionCaseTabsContent">
  <div class="tab-pane fade show active" id="regionCaseA" role="tabpanel" aria-labelledby="regionCaseA-tab">
    Quarterly performance by Region in Case A
  </div>
  <div class="tab-pane fade" id="regionCaseB" role="tabpanel" aria-labelledby="regionCaseB-tab">
    Quarterly performance by Region in Case B
  </div>
</div>

End of section quarterly performance by Region

Final Words about Quarterly Results
## Yearly Results {.tabset}
This content pertains to Yearly Results
### By Product
Yearly performance by Products
### By Region
Yearly performance by Region
Final Words about Yearly Results

关键细节说明:

  • nav nav-tabs:用来创建标签栏的样式
  • data-bs-toggle="tab" 和 data-bs-target="#id":关联标签按钮和对应的内容面板
  • tab-content 和 tab-pane:存放每个标签的内容,active 类指定默认显示的标签

方法二:用bslib包实现自动多层嵌套(更简洁)

如果你愿意改用bslib主题,它对嵌套标签页的支持更友好,不需要手动写HTML,直接给各级标题加.tabset类就能实现任意层级的嵌套。代码如下:

---
output:
  html_document:
    theme: 
      version: 5
      bootswatch: paper  # 保持你原来的主题风格
    highlight: tango
    number_sections: false
    toc: false
    toc_float: false
---
# Result Discussion {.tabset}
We will discuss results here
## Quarterly Results {.tabset}
This content pertains to Quarterly Results
### By Product {.tabset}
#### Case A
Quarterly performance by Products in Case A
#### Case B
Quarterly performance by Products in Case B
End of section quarterly performance by Products
### By Region {.tabset}
#### Case A
Quarterly performance by Region in Case A
#### Case B
Quarterly performance by Region in Case B
End of section quarterly performance by Region
Final Words about Quarterly Results
## Yearly Results {.tabset}
This content pertains to Yearly Results
### By Product
Yearly performance by Products
### By Region
Yearly performance by Region
Final Words about Yearly Results

关键细节说明:

  • bslib基于Bootstrap 5,扩展了R Markdown的tabset功能,支持无限层级嵌套
  • 只要给需要转成标签页的标题(比如### By Product)加上.tabset类,其下的子标题就会自动变成标签页
  • 这种方法代码更简洁,后续维护也更方便

内容的提问来源于stack exchange,提问作者jroy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:49:50