是否有人可以在 R markdown html 文档的 float 目录中创建一个按钮来折叠第三或第二级结果标题?我使用第三级副标题进行图形描述,以便在目录中自动获得“图形表格”,但有时当图形很多时不想看到它们。
我希望在 yaml
header 中包含 toc_depth=3
,并让 float 目录最初打开并显示所有三个级别,但可以选择单击某处,以便TOC 中仅显示 2 个级别。理想情况下,最好还能够仅显示一级,并有选择地扩展特定的顶级标题以包括二级标题(然后是第三级标题,等等)。
最佳答案
好吧,我已经尝试通过在 RMarkdown 文件底部添加一个小的 JavaScript 函数来实现一些东西。我不确定这是否适合用例!这是 .Rmd 文件的完整内容:
编辑
仅在回复评论时才将额外按钮添加到折叠 3 级标题
---
output:
html_document:
toc: true
toc_float:
collapsed: true
---
# Chapter 1
## Section 1-1
### Figure 1-1-1
### Figure 1-1-2
# Chapter 2
## Section 2-1
### Figure 2-1-1
### Figure 2-1-2
---
Select the menu on the left to expand / collapse table of contents (TOC)
entries. Press button below to collapse all TOC except the top level headings.
<button id="btnCollapseHeading" onclick="collapseTOC()">Collapse sub-headings</button>
If you only want to collapse level 3 headings press this button.
<button id="btnCollapseLevel3" onclick="collapseLevel3()">Collapse Level 3 only</button>
<script>
function collapseTOC() {
var x = document.getElementsByClassName("tocify-subheader");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
}
function collapseLevel3() {
var x = document.getElementsByClassName("tocify-subheader");
var i;
for (i = 0; i < x.length; i++) {
if (x[i].getAttribute("data-tag") == "3") {
x[i].style.display = "none";
}
}
}
</script>
我还添加了一个示例来尝试 https://github.com/markdly/rmd-toc-button
关于r - 折叠 rmarkdown html 文档中 float 目录的次要级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51860859/