在图形/代码块的上方和下方添加垂直空间 [英] add vertical space above and below figures/code chunks

查看:90
本文介绍了在图形/代码块的上方和下方添加垂直空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Rmd编织为PDF.我想在图形/代码块的上方和下方添加垂直空间,以将这些内容与正文分开.通过将以下内容添加到我的序言

I am knitting from Rmd to PDF. I'd like to add vertical space above and below figures/code chunks to separate this content from body text. I've done this successfully for Rnw files by adding the following to my preamble see this answer:

\renewenvironment{knitrout}{\vspace{1em}}{\vspace{1em}}

我尝试将这一行添加到Rmd文件的yaml部分的header-includes中,但是没有用.我打开了保留临时tex文件的选项,我发现tex中没有knitrout环境.

I tried adding this line to header-includes in the yaml section of my Rmd file, but it did not work. I turned on the option to keep the interim tex file, and I see there is no knitrout environment in the tex.

相反,(i)代码块包装在\begin{Shaded}中,(ii)在\begin{figure}中是图形,在(iii)在\begin{longtable}中是表.

Instead, (i) code chunks are wrapped in \begin{Shaded}, (ii) figures in \begin{figure}, and (iii) tables in \begin{longtable}.

我尝试通过将以下内容添加到header-includes来解决代码块(i)的问题:

I tried solving the problem for code chunks (i) by adding the following to header-includes:

\renewenvironment{Shaded}{\vspace{1em}}{\vspace{1em}}

这增加了空间,但是放下了阴影背景.我不确定如何解决此问题或解决数字(ii)和表格(iii).

This added space, but dropped the shaded background. I'm not sure how to fix this or address the figures (ii) and tables (iii).

R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: macOS Sierra 10.12.4

推荐答案

我认为您需要修改钩子: https://yihui.name/knitr/hooks/
我举几个例子.一旦了解了它的工作原理,就可以添加自己的修改.

I think you need to modify hooks: https://yihui.name/knitr/hooks/
I give you some examples. Once you understand how it works, you can add your own modifications.

要修改的钩子:

```{r setup_source, include=FALSE}
hook_source_def = knitr::knit_hooks$get('source')
knitr::knit_hooks$set(source = function(x, options) {
  if (!is.null(options$vspaceecho)) {
    begin <- paste0("\\vspace{", options$vspaceecho, "}")
    stringr::str_c(begin, hook_source_def(x, options))
  } else {
    hook_source_def(x, options)
  }
})
```

如何调用此修改:

```{r vspace, vspaceecho='2cm', echo=TRUE}
summary(cars)
```

在R输出之后添加vspace

要修改的钩子

Add vspace after R output

The hook to be modified

```{r setup_output, include=FALSE}
hook_output_def = knitr::knit_hooks$get('output')
knitr::knit_hooks$set(output = function(x, options) {
  if (!is.null(options$vspaceout)) {
    end <- paste0("\\vspace{", options$vspaceout, "}")
    stringr::str_c(hook_output_def(x, options), end)
  } else {
    hook_output_def(x, options)
  }
})
```

如何调用此修改:

```{r vspaceafter, vspaceout='1cm'}
summary(cars)
```

在图形之前和之后添加vspace

您可以创建一个新环境来嵌入图形.这需要添加一个技巧,使pandoc忽略\begin\end,否则pandoc不会将环境中的代码视为markdown语法.

Add vspace before and after a figure

You can create a new environment to embed a figure. This requires to add a trick to make pandoc ignore \begin and \end, otherwise pandoc do not consider code inside environment as markdown syntax.

里面的文字

\newenvironment{plotspace}[1]{#1}
% -- command for pandoc trick with \begin and \end -- %
\newcommand{\nopandoc}[1]{#1} 

在YAML中调用tex文件

output: 
  pdf_document:
    keep_tex: yes
    include:
      in_header: header.tex

要包含在代码中的挂钩函数

hook_plot_def = knitr::knit_hooks$get('plot')
knitr::knit_hooks$set(plot = function(x, options) {
  if (!is.null(options$vspaceplot)) {
    begin <- paste0("\\nopandoc{\\begin{plotspace}}\n\\vspace{", options$vspaceplot, "}\n")
    end <- paste0("\n\\vspace{", options$vspaceplot, "}\n\\nopandoc{\\end{plotspace}}")
    stringr::str_c(begin, hook_plot_def(x, options), end)
  } else {
    hook_plot_def(x, options)
  }
})

如何称呼

```{r plotvspace, vspaceplot='2em', fig.cap='Figure with vspace'}
plot(cars)
```

在新环境中检索阴影

在尝试更新Shaded环境时,可以在附加的header.tex文件中进行以下操作.

Retrieve Shaded in a renew environment

As you tried to renew Shaded environment, you can do it as follows in your additional header.tex file.

\renewenvironment{Shaded}
{
  \vspace{2cm}%
  \begin{snugshade}%
}{%
  \end{snugshade}%
  \vspace{5cm}%
}

这篇关于在图形/代码块的上方和下方添加垂直空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆