首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RMarkdown:在报告开头浮动TOC和TOC

RMarkdown:在报告开头浮动TOC和TOC
EN

Stack Overflow用户
提问于 2022-08-07 14:30:17
回答 1查看 51关注 0票数 2

我想两者兼而有之:在报告开始时,一个浮动的TOC和一个TOC (以Rmarkdown表示)。我认为只有one才有可能.

在这里,我手动添加了一个TOC。

代码语言:javascript
复制
---
title: "Report"
author: "Anon"
date: '2022-07-20'
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    collapsed: true
    smooth_scroll: true
---

## Table of Contents 

- R Markdown
- Including Plots



## R Markdown

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

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:



## Including Plots

You can also embed plots, for example:

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

我跟随这个问题,但我可以有浮动的TOC或一个TOC在开始。我两样都要。这是客户的要求之一。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-07 15:05:24

html_document使用的模板不能做到这一点,但是可以很容易地对其进行编辑,以便两者兼顾。

  1. 若要查找默认模板,请使用

system.file("rmd/h/default.html",package = "rmarkdown")

  1. 在编辑器中获取该文件的副本,并找到以下行:

代码语言:javascript
复制
$if(toc_float)$
$else$
$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$
$endif$

把它们改成

代码语言:javascript
复制
$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$

(即使块成为无条件的,而不是以没有设置toc_float为条件)。保存此文件,例如保存到"toc2.html“。

  1. 在YAML中为您的文档添加指定此模板的选项,即它应该是

代码语言:javascript
复制
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    collapsed: true
    smooth_scroll: true
    template: toc2.html

这样就行了!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73268290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档