Reduce space between code chunks and code output in rmarkdown beamer presentation
Asked Answered
C

1

20

I'm building a presentation using rmarkdown and LaTeX/Beamer. I would like to reduce the spacing between the displayed R-commands and R-output. I believe this is related to the paragraph spacing options in LaTeX/Beamer.

Is this something I should do in rmarkdown (chunk options, knit_hooks, or something else?), in the pandoc Yaml header (some pandoc option?), or in the LaTeX beamer template file? I feel like it should be in the LaTeX template file.

Below is a working example of a minimal markdown file, and a .tex template file I'm using to control some beamer settings.

example.Rmd

---
title: "Untitled"
author: "Ryan"
date: "March 1, 2016"
output:
  beamer_presentation:
    pandoc_args: '--latex-engine=xelatex'
    includes:
      in_header: latex-topmatter.tex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Vertical Spacing is too much

Here is a working example.

- some
- bullets

Example code:

```{r, echo = TRUE}
a <- 1
a
a+a
```

latex-topmatter.tex

% declare overall beamer theme to use as baseline
\usetheme{default}

% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}

% make console-output smaller:
\makeatletter
\def\verbatim{\tiny\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\makeatother

% set vertical spacing between paragraphs:
% \parskip{0pt}
% \addtobeamertemplate{blocks}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block begin}{}{\setlength{\parskip}{0pt}}
% \addtobeamertemplate{block end}{}{\setlength{\parskip}{0pt}}
% % \setlength{\emergencystretch}{0em}
\setlength{\parskip}{0pt}

I've tried making the font of the R-commands or R-output smaller, which does not seem to affect the paragraph spacing.

I've tried using knit_hooks() as in this example: https://github.com/ramnathv/slidify/issues/189, which mostly works - but then i can't seem to reduce the fontsize of the code and output.

I've also tried using \parskip{0pt}, and several other beamer options or parskip options, which are commented in the above latex-topmatter.tex section. None of them seem to change the spacing between chunks of text, R-code, or R-output. Am I even looking in the right place?

parskipNotWorking

Cleodell answered 1/3, 2016 at 22:5 Comment(2)
Did you ever find a solution to this? Thanks.Whipperin
@Whipperin see belowNepheline
N
13

Here is a working example. Notice the definitions at the end of the header file:

  • Source code chunks are contained inside a Shaded environment which in turn uses \OuterFrameSep for its spacing. So we need to redefine that.
  • With \preto we prepend the commands \topsep=-10pt \partopsep=-10pt to every verbatim environment. This affects the spacing of output chunks.

example.Rmd

---
title: "Untitled"
author: "Martin"
date: "January 4, 2017"
output:
  beamer_presentation:
    keep_tex: yes
    pandoc_args: --latex-engine=xelatex
    includes:
      in_header: latex-topmatter.tex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Vertical Spacing is just right

Here is a working example.

- some
- bullets

Example code:

```{r, echo = TRUE}
a <- 1
a
a+a
```

latex_topmatter.tex

% declare overall beamer theme to use as baseline
\usetheme{default}

% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\tiny,commandchars=\\\{\}}

% make console-output smaller:
  \makeatletter
\def\verbatim{\tiny\@verbatim \frenchspacing\@vobeyspaces \@xverbatim}
\makeatother


\setlength{\parskip}{0pt}


\setlength{\OuterFrameSep}{-4pt}
\makeatletter
\preto{\@verbatim}{\topsep=-10pt \partopsep=-10pt }
\makeatother

enter image description here

Nepheline answered 4/1, 2017 at 15:56 Comment(2)
Thanks for solving that, Martin! Can you suggest any other way of finding this solution? Posting my question here and waiting 9 months is one way. In hunting through latex documentation i regularly struggle to find what i need, this question being just one example.Cleodell
I can't. I am actuallly surprised that it took that long given that the answer is not that difficult.Nepheline

© 2022 - 2024 — McMap. All rights reserved.