【问题标题】:How to remove white space at the end of an Rmarkdown HTML output如何删除 Rmarkdown HTML 输出末尾的空格
【发布时间】:2019-03-26 18:28:57
【问题描述】:

markdown 结尾处的空白是否存在共同的罪魁祸首?这是我的 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 导致了空白,特别是 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)
```

【问题讨论】:

  • @tarleb 我通常用我的问题来做代表。这个问题似乎有点不同,因为它是用 knitr 渲染 HTML。我试图提供回答这个问题可能需要的东西。我会看看我是否可以制作一个最小的 rmarkdown 示例。谢谢。
  • 谢谢!我能够重现它并四处寻找。似乎是由浮动目录引起的。我已经添加了答案。

标签: html r markdown r-markdown knitr


【解决方案1】:

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

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

为我工作!

【讨论】:

  • 这也解决了我的问题。似乎这应该是公认的答案。
【解决方案2】:

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

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

【讨论】:

  • 谢谢! “但是,R Markdown 似乎确实公开了一种通过 YAML 设置选项的方法。”你的意思是“不暴露”?
  • 哎呀,是的,错字。谢谢!
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
相关资源
最近更新 更多