html - 在 Rmarkdown 中用 HTML 文档在左侧添加目录

标签 html css r knitr r-markdown

我正在尝试在 Rmarkdown 生成的 HTML 文档中添加目录。首先,我找到了this answer ,这似乎是一种简单的方法,在 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。为了解决这个问题,我找到了 this other post , 如何添加自定义 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;
}")
```

我想我遗漏了一些重要的东西。提前致谢!

最佳答案

如你的代码所示first answe r,你需要在你的 YAML header 中添加这些,但你需要注意空格:

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

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

关于html - 在 Rmarkdown 中用 HTML 文档在左侧添加目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43855100/

相关文章:

html - 用马拉雅拉姆语创建网站

javascript:在n个循环元素之后包含在div中的列表

html - 使用 Twitter Bootstrap 分解导航

html - 包含父级 <div> 的图像大小

html - Bootstrap grid .col 更高 - 相同的高度

在 dplyr 中从字符重新编码为因子中的数字

r - 标准化 R 中的定性变量以执行 glm、glm.nb 和 lm

RMarkdown - print() 命令导致套件 PDF 中的页面绑定(bind)线超出页面范围

html - 随机 CSRF token 真实性错误

html - ie7 中的流体设计错误