2

渲染到时如何在 R Markdown 演示文稿中添加目录ioslides_presentation

类似于以下内容,但在 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
---
4

1 回答 1

1

由于tocRM arkdown ioslides 不支持子选项,请参见此处,我们必须做一个解决方法。

@ShKlinkenberg在这里提供了一个解决方案。关键是使用 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}

这将呈现一个目录,如下所示:

在此处输入图像描述

于 2018-05-28T15:48:10.607 回答