【问题标题】:How to add table of contents in Rmarkdown?如何在 Rmarkdown 中添加目录?
【发布时间】:2014-07-20 08:55:26
【问题描述】:

我正在使用 RStudio 编写 markdown 文档,并希望在文档顶部添加目录 (TOC),以便用户可以单击相关部分进行阅读。 rpubs 上有一些相关的例子,但现在我似乎找不到它们。请注意,我不使用pandoc 并且对Rmdknitr 很陌生。有什么方法可以在不使用pandoc 的情况下添加 TOC?如果必须使用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。 我现在该怎么办?我真的想要 TOC,但又不会损害其他分析的输出。

【问题讨论】:

  • 我相信 Rstudio 使用的 rmarkdown 包是 pandoc 的包装器,因此您应该能够传递相关选项。事实上,YAML front-matter 中的toc: true 应该这样做。
  • 尝试缩进,遵循rmarkdown.rstudio.com 中的示例,如果其他一切都失败则更新 Rstudio
  • @umairdurrani 好的。该示例没有任何标题。你想在目录中出现什么?
  • 感谢@baptiste,我也遇到了这个问题,但缩进可以正确解决。
  • 在标题中正确缩进是关键

标签: r rstudio r-markdown


【解决方案1】:

语法是

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

the documentation。确保这是在文档的开头。还要确保您的文档确实有标题,否则 R 无法在目录中说出您想要的内容。

【讨论】:

  • 这与我放在 Rmd 文件顶部(标题之前)并单击 knit HTML 的内容完全相同。生成的文档没有目录,并且创建时没有任何错误。有没有其他选择可以改变一些地方?
  • 我现在已经尝试了 RStudio 版本 0.98.501、.507 和 .897(预览版),但是这个元数据没有用。
  • @umairdurrani 您能否编辑您的问题以包含一个不适合您的小型示例文档。这样我们就可以尝试完全相同的事情来看看会发生什么。
【解决方案2】:

更多选项的语法:

---
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
---

【讨论】:

  • 我认为这是toc_depth 而不是depth
  • @F.Privé 我一年前写了这个答案。不确定软件包是否进行了这些更改。感谢更新。
  • 如何在每个部分添加点击返回TOC?谢谢
  • @Daniel - 尝试在您希望它出现的位置(代码行)使用锚链接 HTML,例如:&lt;a href="#top"&gt; Back To Top &lt;/a&gt;。希望这行得通。
【解决方案3】:

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

如果您想在新页面中使用它,则必须使用一些乳胶语言。这就是我所做的。

---
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 添加了一个数字列表,使用\listoffigures 添加了一个表格列表987654328@,然后是一个新的页面。

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

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

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 2017-10-06
    • 1970-01-01
    • 2022-01-21
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多