2

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

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.

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

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

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

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
Community
  • 1
  • 1
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • Try http://tex.stackexchange.com/ – 3Dave Apr 12 '17 at 20:43
  • Thanks, @DavidLively. I've come across advice suggesting that questions like this are a better fit for SO because it would help to have someone who knows `rmarkdown` and LaTeX. You might be right though. – Eric Green Apr 12 '17 at 20:46
  • Fair enough. My LaTeX skills are way rusty. The question is fine here, though. – 3Dave Apr 12 '17 at 20:58

1 Answers1

2

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.

Add vspace before source code

The hook to be modified:

```{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)
  }
})
```

How to call this modification:

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

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

How to call this modification:

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

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.

Create an external tex file to be called in YAML

This text inside

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

Call tex file in YAML

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

The hook function to include in your code

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)
  }
})

How to call it

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

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}%
}
Sébastien Rochette
  • 6,536
  • 2
  • 22
  • 43
  • Thanks, @StatnMap. This looks great. I put the pandoc trick in the .tex file I call in YAML, but I'm getting an error. `! Undefined control sequence. l.145 \newcommand{\nopandoc`. Should it be obvious that I am missing a latex package? – Eric Green Apr 20 '17 at 20:56
  • I dont think you need other package. I put the Rmd file and the header tex file to be downloaded (zipped) on my website : https://statnmap.com/vspace_rmarkdown/ . Unzip it. Then you can run it by clicking on the `knit` button in Rstudio or you can run it with this command in R : `rmarkdown::render("Fig_Vspace_rmarkdown.Rmd", output_format = "pdf_document")` – Sébastien Rochette Apr 21 '17 at 07:10
  • i replaced my included .tex file with yours and solved the error, so thanks. everything runs, but i'm struggling to modify the space between sections. in [this rmd file](https://www.dropbox.com/s/2bgrl7ruzfnclu6/Fig_Vspace_rmarkdown_v2.Rmd?dl=0), i move the parameters to the setup chunk and set them to be very small...but the pdf output still shows big spaces. – Eric Green Apr 23 '17 at 13:28
  • It is because in the `\renewenvironment{Shaded}` set in the header tex file, there are also `vspace` set. You should choose between this renewenvironment and hook modifications. Or define the desired combination. – Sébastien Rochette Apr 23 '17 at 19:52
  • [I made a few tweaks](https://www.dropbox.com/s/3hbzdlspqscrk0v/vspace.zip?dl=0) to get the PDF looking just as I want. Really great. The only remaining issue I can see is that `vspaceplot` prevents figure labels from working. For instance, a reference to Figure 1 in the text prints as "Figure ??". Is it possible that the pandoc "trick" is interfering with figure numbering? – Eric Green Apr 24 '17 at 10:16
  • I use the "pandoc trick" with bookdown and references to figures without any problem, so I do not think this is a reason. Sometimes references with bookdown do not work only because there is a lack of spaces in the code. But I can not tell you more... – Sébastien Rochette Apr 24 '17 at 21:07
  • Thanks, @StatnMap. Posting here to document my attempts. Maybe it helps someone else. [I modified the markdown files](https://www.dropbox.com/sh/fjyvexey1u2rfm8/AAB98qfsfka401EdTAGF0J-Sa?dl=0) to use `bookdown::pdf_document2` and show (a) when I define `vspaceplot` in a chunk, the figure IS NOT numbered and (b) when I do not define `vspaceplot` the figure IS numbered. – Eric Green Apr 25 '17 at 20:24
  • @SébastienRochette following along a couple of years later and am unable to access your code at statnmap.com/vspace_rmarkdown? – Chuck P Feb 06 '20 at 15:31
  • You can find it here: https://statnmap.com/files/Vspace_rmarkdown.zip – Sébastien Rochette Feb 06 '20 at 15:47