我正在尝试在 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/