如何在渲染到ioslides_presentation
时在R Markdown演示文稿中添加目录?
如下所示,但在ioslides中:
---
title: ""
author: ""
date: ""
output:
html_document:
df_print: paged
number_sections: yes
theme: united
toc: yes
toc_float:
collapsed: yes
smooth_scroll: yes
---
对于输出ioslides的文档:
---
title: ""
author: ""
date: ""
output:
ioslides_presentation: null
beamer_presentation: default
slidy_presentation: default
---
答案 0 :(得分:1)
由于RM arkdown ioslides不支持toc
子选项,请参阅例如here我们必须做一个解决方法。
@ShKlinkenberg提供了解决方案here。关键是使用JavaScript脚本来添加此功能。
以下独立.Rmd
示例使用@ ShKlinkenberg的脚本:
---
title: "ToC in IOslides"
output: ioslides_presentation
---
<!-- Script for adding ToC !-->
<script>
document.addEventListener('DOMContentLoaded', function() {
TableOfContents();
}
);
function TableOfContents(container, output) {
var output = output || '#toc';
// Get all elements with class: section or subsection
idfromclass = document.querySelectorAll('.section, .subsection');
// Create the list element:
var list = document.createElement('ul');
// Iterate through all found elements
for(var i = 0; i < idfromclass.length; i++) {
// Create the list item:
var item = document.createElement('li');
// Set its contents:
var id = idfromclass[i].id
// Replace - in id with whitespace
var titleText = id.replace(/-/gi, " ");
// Add text to list element
item.appendChild(document.createTextNode(titleText));
// Add subsection class
item.className = idfromclass[i].className
// Add it to the list:
list.appendChild(item);
}
// Return generated HTML to toc div in slide
document.querySelector(output).innerHTML = list.innerHTML;
// Generate instruction message if no classes are defined
if (idfromclass.length == 0) { document.querySelector(output).innerHTML = "Add {.section} or {.subsection} to slide name to generate TOC"; }
};
</script>
## Table of content
<div id="toc"></div>
# Section 1 {.section}
## Subsection A {.subsection}
# Section 2 {.section}
这将呈现如下目录: