R Markdown将空格添加到HTML输出

问题描述 投票:2回答:3

我已经找到了几个建议,可以在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:
r knitr r-markdown pandoc
3个回答
3
投票

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

感谢@Martin Schmelzer对SO post 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:

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;
}
``` 

0
投票

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

这增加了一行空白区域,并且通过html输出可靠地为我工作。

# R Markdown

Some text

$~$

More text after a white space
© www.soinside.com 2019 - 2024. All rights reserved.