在左侧添加一个目录,在Rmarkdown中包含HTML文档

在左侧添加一个目录,在Rmarkdown中包含HTML文档,html,css,r,knitr,r-markdown,Html,Css,R,Knitr,R Markdown,我试图在Rmarkdown生成的HTML文档中添加目录。首先,我发现在HTML文件中添加一个CSS文件,其中包含以下代码,这似乎是一种简单的方法: #TOC { position: fixed; left: 0; top: 0; width: 200px; height: 100%; overflow:auto; } body { max-width: 800px; margin: auto; margin-left:210px; line-height:

我试图在Rmarkdown生成的HTML文档中添加目录。首先,我发现在HTML文件中添加一个CSS文件,其中包含以下代码,这似乎是一种简单的方法:

#TOC {
  position: fixed;
  left: 0;
  top: 0;
  width: 200px;
  height: 100%;
  overflow:auto;
}
body {
  max-width: 800px;
  margin: auto;
  margin-left:210px;
  line-height: 20px;
}
但是我想从Rmarkdown修改CSS。为了解决我发现的问题,如何添加自定义CSS标记。但这并不是我想要的,我不知道如何正确地把这些和答案结合起来

我的代码如下所示:

---
title: "R"
output: html_document
toc: yes
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r results="asis"}
cat("TOC {
  position: fixed;
  left: 0;
  top: 0;
  width: 200px;
  height: 100%;
  overflow:auto;
}
body {
  max-width: 800px;
  margin: auto;
  margin-left:210px;
  line-height: 20px;
}")
```

我想我错过了一些重要的事情。提前谢谢

如r的代码所示,您需要在YAML头中添加这些,但需要注意空格:

title: "cssTest"
    output:
    html_document:
      css: custom.css
      toc: yes

然后,css文件应该是一个外部css文件。从你的Rmd代码。在这里,它是一个与Rmd文件位于同一目录中的文件,称为
custom.css

Hmisc包非常好而且很简单,只需将以下内容添加到启动块并运行它。它将以html格式呈现:

hidingTOC(
buttonLabel = "Contents",
levels = 3,
tocSide = c("right", "left"),
buttonSide = c("right", "left"),
posCollapse = c("margin", "top", "bottom"),
hidden = FALSE
)

tocSide指定您希望TOC位于哪一侧。有关更具体的文档,请参见:

因此,无法从Rmarkdown中修改.css吗?我不知道。您可以使用
file=custom.css
尝试
cat
。这可能是因为在运行Pandoc for HTML代码之前先运行R代码。。。