Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce space between code chunks and code output in rmarkdown beamer presentation

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

like image 685
RyanStochastic Avatar asked Mar 01 '16 22:03

RyanStochastic


People also ask

How do I show code chunks in R markdown?

If you wrap this code within a markdown code block, the rendered output will display the verbatim R code chunk — including backticks. Start and end the R code chunk you want to display: ```{}...```

How do I hide chunks of code in R markdown?

include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.

How do you collapse all chunks in R?

Expand — Shift+Alt+L. Collapse All — Alt+O.

What is knitr in RMarkdown?

RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.


1 Answers

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

like image 68
Martin Schmelzer Avatar answered Oct 06 '22 23:10

Martin Schmelzer