如何在Rmarkdown中添加目录?

124

我正在使用RStudio编写Markdown文档,并希望在文档顶部添加目录(TOC),以便用户可以点击相关部分进行阅读。之前在rpubs上有一些相关示例,但现在似乎找不到了。请注意,我不使用pandoc,并且对Rmdknitr都很陌生。是否有任何方法可以在不使用pandoc的情况下添加目录?如果必须使用pandoc,那么哪些函数是相关的?

编辑

这是一个小样本页面:

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
---

Header 1
---------------
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
    
## Header 2
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
    
```{r}
summary(cars)
```

You can also embed plots, for example:

```{r, echo=FALSE}
plot(cars)
```
### Header 3
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

我在RStudio v 0.98.864上尝试运行了这个,它成功了!但是很遗憾,在0.98.501和0.98.507上却没有成功。我正在0.98.501版本上写我的论文,在更新了RStudio之后,一些分析无法正常工作。所以,我回退到了0.98.501版本。 现在我该怎么办?我真的想要目录,但又不想影响其他分析的输出结果。

3
我认为 Rstudio 使用的 rmarkdown 包是 pandoc 的一个封装器,因此你应该能够传递相关选项。实际上,在 YAML front-matter 中使用 toc:true 即可实现目录。 - baptiste
2
尝试缩进,按照http://rmarkdown.rstudio.com/中的示例进行更新,如果一切都失败了,请更新Rstudio。 - baptiste
1
@umairdurrani 好的。示例中没有标题。您希望目录中包含什么? - MrFlick
1
头文件中的适当缩进是关键。 - N Brouwer
1
我曾经遇到过同样的问题,但尝试了几次后我意识到你的文档必须以这个块开始。一开始我是在加载库之后才添加的,结果失败了。希望这能有所帮助。 - Yannis
显示剩余6条评论
3个回答

110

语法是

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
---

请查看文档。确保此内容位于您的文档开头。同时,请确保您的文档实际上具有标题,否则 R 无法确定您想要在目录中显示什么。


3
这就是我放在Rmd文件顶部(标题之前)并点击knit HTML的完全相同的内容。生成的文档没有目录,并且没有任何错误地创建。是否有其他选项可以更改? - umair durrani
3
我现在已经尝试了RStudio版本0.98.501、.507和.897(预览版),但这个元数据的东西不起作用。 - umair durrani
1
@umairdurrani,您能否编辑您的问题,包括一个小的样本文档,让我们可以尝试完全相同的事情,以查看会发生什么。 - MrFlick

97

更多选项的语法:

---
title: "Planets"
author: "Manoj Kumar"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output: 
  html_document:
    toc: true # table of content true
    toc_depth: 3  # upto three depths of headings (specified by #, ## and ###)
    number_sections: true  ## if you want number sections at each table header
    theme: united  # many options for theme, this one is my favorite.
    highlight: tango  # specifies the syntax highlighting style
    css: my.css   # you can add your custom css, should be in same folder
---

4
我认为这应该是“toc_depth”而不是“depth”。 - F. Privé
1
@F.Privé 我大约一年前写了这个答案,不确定这个包是否已经进行了这些更改。感谢更新。 - Manoj Kumar
1
如何在每个章节中添加一个点击事件以返回目录?谢谢。 - Daniel
2
@Daniel - 尝试使用锚点链接HTML,例如:<a href="#top">返回顶部</a>,放置在你想要它出现的位置(代码行)。希望这个方法有效。 - Manoj Kumar
1
这是最好的答案... - LeMarque

43

如果您使用 pdf_document,您可能希望在新页面中添加目录,但是 toc: true 不允许这样做。它会将目录放在文档标题、作者和日期之后--因为它在 yaml 中。

如果您想将其放在新页面中,您需要使用一些 LaTeX 语言。以下是我所做的。

---
title: \vspace{3.5in}"Title"
author: "Name"
date: "`r Sys.Date()`"
output:
   pdf_document:
      fig_caption: true
      number_sections: true
---

\newpage # adds new page after title
\tableofcontents # adds table of contents
\listoffigures
\listoftables
\newpage

因此,在yaml(---之间的块)之后,我使用\newpage添加了一个新页面,然后使用\tableofcontents添加了一个目录,使用\listoffigures添加了一个图表列表,使用\listoftables添加了一个表格列表,并在所有其他内容之前添加了一个新页。

请注意,在标题中使用\vspace{3in}会在打印yaml(标题等)之前从顶部添加3英寸的垂直空间。

阅读更多信息: https://www.sharelatex.com/learn/Table_of_contents


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接