如何在Rmarkdown中添加目录?

uma*_*ani 78 r rstudio r-markdown

我正在使用RStudio来编写markdown文档,并希望在文档顶部添加目录(TOC),以便用户可以单击相关部分进行阅读.关于rpubs有一些相关的例子,但现在我似乎无法找到它们.请注意,我不使用pandoc和很新的Rmd&knitr.有没有办法在不使用的情况下添加TOC 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.
Run Code Online (Sandbox Code Playgroud)

我尝试在RStudio v 0.98.864中运行它,它工作了!但遗憾的是它不适用于0.98.501和0.98.507.我正在研究0.98.501中的论文,在更新RStudio之后,我的一些分析没有用.所以,我又回到了0.98.501.我现在应该怎么做?我真的想要TOC,但不会损害其他分析的输出.

MrF*_*ick 61

语法是

---
title: "Sample Document"
output:
  html_document:
    toc: true
    theme: united
---
Run Code Online (Sandbox Code Playgroud)

文档中

  • 这与我放在Rmd文件之上(标题之前)并单击编织HTML完全相同.生成的文档没有目录,创建时没有任何错误.还有其他选择可以改变一些地方吗? (2认同)
  • 我现在尝试了RStudio版本0.98.501,.507和.897(预览版),但这个元数据的东西不起作用. (2认同)

Man*_*mar 50

包含更多选项的语法:

---
title: "Planets"
author: "Manoj Kumar"
date: "March 3, 2016"
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
---
Run Code Online (Sandbox Code Playgroud)

  • 我认为这是`toc_depth`而不是`depth` (4认同)
  • @Daniel - 尝试在您希望其出现的位置(代码行)使用锚链接 HTML,例如:`&lt;a href="#top"&gt;返回顶部&lt;/a&gt;`。希望这有效。 (2认同)

MSD*_*MSD 14

如果您正在使用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
Run Code Online (Sandbox Code Playgroud)

因此,在yaml(---之间的块)之后,我添加了一个新页面\newpage,然后使用\tableofcontents了一个目录,使用\listoffigures了一个数字列表,一个表列表\listoftables和一个新页面.

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

点击此处了解更多信息:https://www.sharelatex.com/learn/Table_of_contents