R Markdown将空格添加到HTML输出

sta*_*tor 3 r pandoc knitr r-markdown

我已经发现了几个建议添加空格至R降价文件,包括<br>,\newpage和一些其他的东西.

这些对我的HTML输出不起作用,也许有更好的方法.我想在下面的例子中做两件事:

(1)在标题和第一个标题之间添加额外的空格

(2)在第一段和第二段之间添加额外的空格

让我们使用下面显示的默认R Markdown文档.我如何为HTML输出实现这个额外的空白区域?

---
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:
Run Code Online (Sandbox Code Playgroud)

Her*_*ndo 12

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

这增加了一行空白,并且对我来说可靠的 html 输出。

# R Markdown

Some text

$~$

More text after a white space
Run Code Online (Sandbox Code Playgroud)


RLe*_*sur 8

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

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

<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>
Run Code Online (Sandbox Code Playgroud)

这是一个使用margin属性:first-of-type伪类的解决方案:

---
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:
Run Code Online (Sandbox Code Playgroud)

如果您CSS在最终文档中使用这些规则,您可能会感到失望,因为您将在每个h2元素之前和每个第一个p元素之后获得一个空格.因此,您可能更喜欢选择具有div标识符的元素:

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

#r-markdown p:first-of-type {
  margin-bottom: 3em;
}
``` 
Run Code Online (Sandbox Code Playgroud)


Cri*_*uno 6

使用div元素来划分R Markdown 文件中的部分。在每个div标签内,为每个margin-bottom属性分配一个边距值。接近 100% 的值会增加空白的数量。

感谢@Martin Schmelzer 对 SO post Rmarkdown html whitespace 的回答。

HTML 输出的 SS

---
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:
Run Code Online (Sandbox Code Playgroud)