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?
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: ```{}...```
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.
Expand — Shift+Alt+L. Collapse All — Alt+O.
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.
Here is a working example. Notice the definitions at the end of the header file:
Shaded
environment which in turn uses \OuterFrameSep
for its spacing. So we need to redefine that.\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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With