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

假如想象 提交于 2019-12-20 09:16:55

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/35734525/reduce-space-between-code-chunks-and-code-output-in-rmarkdown-beamer-presentatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!