R Markdown将空格添加到HTML输出

时间:2018-03-03 22:19:40

标签: r knitr r-markdown pandoc

我发现了一些有关向R Markdown文档添加空格的建议,包括<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:

3 个答案:

答案 0 :(得分: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>

以下是一种使用margin property: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元素之前和每个第一个p元素之后获得一个空格。因此,您可能更愿意选择带有div标识符的元素:

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

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

答案 1 :(得分:3)

使用div元素划分R Markdown file中的部分。在每个div标记内,为每个margin-bottom属性分配一个边距值。接近100%的值会增加空白区域。

感谢@Martin Schmelzer回复SO Rmarkdown html whitespace

SS of HTML Output

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

答案 2 :(得分:0)

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

这会增加一行空格,并且对我来说使用html输出是可靠的。

# R Markdown

Some text

$~$

More text after a white space