如何为R markdown TOC添加标题

use*_*015 6 r r-markdown

是否可以为R降价文档生成的TOC添加标题.

具体来说,我现在有这样的事情:

---
title: "The Rebel Base Locations"
author: "Darth Vader"
date: "A long time ago in a galaxy far far away"
output: 
  html_document:
    css: custom.css
    toc: true
    toc_depth: 3
    number_sections: true
---
Run Code Online (Sandbox Code Playgroud)

我想要的是TOC有一个名为"内容"的标题.我能用一些css把它推到左边.

Mol*_*olx 3

您可以在 html 模板中的目录之前添加一个新字段。下列的此说明,执行以下操作:

  1. 将 html 模板复制到您的 .rmd 文件并将$toc$字段更改为如下所示:
$if(toc)$
<div id="$idprefix$TOC">
<h1 class="toctitle">$toctitle$</h1>
$toc$
</div>
$endif$
Run Code Online (Sandbox Code Playgroud)
  1. 然后,添加toctitletemplate字段添加到您的 RMarkdown 文件中。例如:
toctitle: "Contents"
output: 
  html_document:
    template: toctitle.html
    css: custom.css
    toc: true
    toc_depth: 3
    number_sections: true
Run Code Online (Sandbox Code Playgroud)

这应该在目录之前添加标题。您可以进行其他修改,例如更改为h1另一个标签和/或使用 CSS 上的字段类。如果您因不使用默认模板而遇到问题(mathjax 无法工作),您可以更改默认样式,totctitle以后不添加到不同的 rmd 文件应该不是问题。