如何删除 Rmarkdown HTML 输出末尾的白色 space

How to remove white space at the end of an Rmarkdown HTML output

降价结束时是否有白色 space 的常见罪魁祸首?这是我的 HTML 输出的结尾。我的选择:

```
{r }
knitr::opts_chunk$set(fig.width=6, fig.asp=.618, fig.align="center",
fig.path='Figs/', warning=FALSE, message=FALSE, cache=TRUE)
```

虽然我手边没有 reprex,但我确实尝试了几次重新运行,发现 YAML 导致了白色 space,特别是 toc_float: true.

date: "`r format(Sys.time(), '%B, %d %Y')`"
output:
  html_document:
    theme: united
    highlight: textmate
    code_folding: show
    toc: true
    toc_float: true
editor_options:
  chunk_output_type: inline
always_allow_html: yes

编辑:这是一个可重现的例子:

---
date: "`r format(Sys.time(), '%B, %d %Y')`"
output:
  html_document:
    theme: united
    highlight: textmate
    code_folding: show
    toc: true
    toc_float: true
editor_options:
  chunk_output_type: inline
always_allow_html: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## 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:

```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```

这似乎是由 HTML 输出中包含的 tocify 脚本引起的。如果 toc_float 设置为 true(或者如果它包含更多选项),则包含该脚本。

添加白色 space 的选项原则上可以通过 tocify extendOffset 选项进行配置。但是,R Markdown 似乎没有提供通过 YAML 设置选项的方法。目前,摆脱它的唯一方法是取消设置 toc_float.

根据 https://community.rstudio.com/t/floating-table-of-contents-and-plots-produce-extra-whitespace-at-bottom/12606/8,您可以通过在 .Rmd 文件底部插入以下 html 代码来保留 toc_float 并删除多余的白色 space:

<div class="tocify-extend-page" data-unique="tocify-extend-page" style="height: 0;"></div>

对我有用!