在RMarkdown中的图形周围添加空间

问题描述:

我想在RMarkdown中的图形周围添加空格.我正在编织PDF,真的不喜欢数字(或方程式)与文本或下一个数字的距离如此之近.

I would like to add space around figures in RMarkdown. I am knitting to PDF and really don't like how close figures (or also equations) are to the text or to the next figure.

---
output: pdf_document
---

```{r pressure, echo=FALSE}
plot(pressure)
```

```{r pressure2, echo=FALSE}
plot(pressure)
```

两个图之间的空间太小,使用ggplots时变得更加模糊.

There is just too little space between the two plots and this gets more fuzzy when using ggplots.

现在我使用乳胶溶液

\vspace{10pt}

但是,如果我可以为整个文档进行全局设置,那就太好了.

but it would be nice if I could make a setting globally for the entire document.

关于绘图前后的间距,您可以使用简单的编织钩子:

Concerning the spacing before and after plots you can use a simple knitr hook:

```{r, echo = F}
library(knitr)
if(is_latex_output()) {
  plot_default <- knit_hooks$get("plot")
  knit_hooks$set(plot = function(x, options) { 
    x <- c(plot_default(x, options), "\\vspace{25pt}")
  })
}
```

在这种情况下,我们改变了绘图钩子,即在每个绘图输出之后仅添加了25pt的间距.

Here we alter the plot hook in that sense that we just add a spacing of 25pt after each plot output.

关于方程式,您只需在文档开头添加以下四个长度定义:

Concerning equations you can just add these four length definitions at the beginning of your document:

\setlength{\abovedisplayskip}{25pt}
\setlength{\belowdisplayskip}{25pt}
\setlength{\abovedisplayshortskip}{25pt}
\setlength{\belowdisplayshortskip}{25pt}

使用align环境创建的前两个Alternate方程.后两个是使用$$ ... $$创建的.

The first two alter equations created using the align environment. The latter two those created using $$ ... $$.