简体   繁体   English

避免 Rmarkdown pdf 中的双倍间距(块内)

[英]Avoid double spacing in Rmarkdown pdf (within chunk)

I am trying to make my own APA7 style Rmarkdown template (with pdf output).我正在尝试制作自己的 APA7 样式 Rmarkdown 模板(输出为 pdf)。 Feel pretty good so far.目前感觉还不错。 The only thing left for me to fix is the double spacing in code chunks.唯一留给我修复的是代码块中的双倍间距。

I have quickly found this thread which resolves the issue on how to double space with the following code.我很快找到了这个线程,它解决了如何使用以下代码加倍空间的问题。

header-includes:
    - \usepackage{setspace}\doublespacing

However, the double spacing also apply to code chunks which I would prefer them to be single space.但是,双倍间距也适用于代码块,我希望它们是单个空格。 I know I could add something like \singlespacing before and after the chunks, but since I use many chucks, is there a cleverer way?我知道我可以在块之前和之后添加像\singlespacing这样的东西,但是因为我使用了很多卡盘,有没有更聪明的方法?

It there a way to avoid the double spacing produced by \doublespacing in code chunks?有没有办法避免代码块中\doublespacing产生的双倍间距?


EDIT编辑

Here is a reproducible example.这是一个可重现的例子。

---
output: pdf_document
header-includes:
  - \usepackage{setspace}\doublespacing
---

I am trying to make my own APA7 style Rmarkdown template (with pdf output). Feel pretty good so far. The only thing left for me to fix is the double spacing in code chunks.

```{r}
# This chuck is also double spaced
# But I would prefer it to be single spaces
```

在此处输入图像描述

Rmarkdown defines a Shaded environment, which is used to set these code snippets. Rmarkdown 定义了一个Shaded环境,用于设置这些代码片段。 You can patch it to automatically add \singlespace :您可以修补它以自动添加\singlespace

---
output: 
  pdf_document:
    keep_tex: true
header-includes:
  - \usepackage{setspace}\doublespacing
  - \usepackage{etoolbox}
  - \AtBeginEnvironment{Shaded}{\singlespace}
---

I am trying to make my own APA7 style Rmarkdown template (with pdf output). Feel pretty good so far. The only thing left for me to fix is the double spacing in code chunks.

```{r}
# This chuck is also double spaced
# But I would prefer it to be single spaces
```
I am trying to make my own APA7 style Rmarkdown template (with pdf output). Feel pretty good so far. The only thing left for me to fix is the double spacing in code chunks.

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM