R Markdown 添加白色 Space 到 HTML 输出

R Markdown add White Space to HTML Output

我发现了一些将 whitespace 添加到 R Markdown 文档的建议,包括 <br>\newpage 和其他一些内容。

这些不适用于我的 HTML 输出,也许有更好的方法。我想在下面的示例中做两件事:

(1) 在标题和第一个header

之间添加额外的白色space

(2)在第一段和第二段之间加白space

让我们使用如下所示的默认 R Markdown 文档。我如何为 HTML 输出完成这个额外的白色 space?

---
title: "Here is the title"
output: html_document
---

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

## R Markdown

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

SECOND PARAGRAPH 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:

使用div elements to divide sections within your R Markdown file. Inside each div tag, assign each margin-bottom属性一个保证金值。接近 100% 的值会增加白色的数量 space。

感谢@Martin Schmelzer 对 SO post .

的回答

---
title: "Here is the title"
output: html_document
---
<div style="margin-bottom:100px;">
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
</div>
## R Markdown
<div style="margin-bottom:50px;">
</div>
FIRST PARAGRAPH 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>.

SECOND PARAGRAPH 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:

由于问题是关于 html_document 的样式,您必须使用一些 CSS 规则。有许多方法可以获得所需的格式。

为了找到一组实现您的目标的 CSS 规则,有必要检查呈现的 HTML 文档。这是问题中提供的默认 R Markdown 文档的相关 HTML 片段:

<div class="fluid-row" id="header">
  <h1 class="title toc-ignore">Here is the title</h1>
</div>

<div id="r-markdown" class="section level2">
  <h2>R Markdown</h2>
  <p>FIRST PARAGRAPH 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 <a href="http://rmarkdown.rstudio.com" class="uri">http://rmarkdown.rstudio.com</a>.</p>
  <p>SECOND PARAGRAPH you click the <strong>Knit</strong> 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:</p>
</div>

这是一种使用 margin property and the :first-of-type pseudo-class:

的解决方案
---
title: "Here is the title"
output: html_document
---

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

```{css echo=FALSE}
/* Define a margin before h2 element */
h2  {
  margin-top: 6em;
}

/* Define a margin after every first p elements */
p:first-of-type {
  margin-bottom: 3em;
}
``` 

## R Markdown

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

SECOND PARAGRAPH 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:

如果您在最终文档中使用这些 CSS 规则,您可能会感到失望,因为您会在每个 h2 元素之前和每个第一个 [=23] 元素之后得到一个 space =] 元素。因此,您可能更喜欢选择带有 div 标识符的元素:

```{css echo=FALSE}
#r-markdown  {
  margin-top: 6em;
}

#r-markdown p:first-of-type {
  margin-bottom: 3em;
}
``` 

添加一行白色的简单解决方案space是使用$~$

这添加了一行白色 space 并且在 html 输出下对我来说很可靠。

# R Markdown

Some text

$~$

More text after a white space