gpt4 book ai didi

r - 如何在 R Markdown 中自动实现文本和代码块之间的不同间距?

转载 作者:行者123 更新时间:2023-12-01 16:30:34 25 4
gpt4 key购买 nike

考虑以下 R Markdown 文档:

---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---

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

Here is some example text.
I want all the body text to be double-spaced,
but I want all echoed code from code chunks to be single spaced.
In other words, not this:

```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```

enter image description here

是否有一种固定或相对轻松的方法可以使所有普通文本双倍行距,但所有代码都从单倍行距的代码块中回显?我尝试查阅 block 选项指南 here ,但找不到我要找的东西。

最佳答案

如果您要输出为 pdf,最简单的方法可能是向您的 Rmd 文档添加一些 LaTeX 命令:

---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---

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

Here is some example text. I want all the body text to be double-spaced, but I
want all echoed code from code chunks to be single spaced. In other words, not
this:

\singlespacing
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```

\doublespacing
Some additional body text. Nor hence hoped her after other known defer his.
For county now sister engage had season better had waited. Occasional mrs
interested far expression acceptance. Day either mrs talent pulled men
rather regret admire but. Life ye sake it shed. Five lady he cold in meet up.

pdf-output或者,您可以使用knitr chunk hooks 定义一个新的 chunk 选项。例如,您可以在设置 block 中包含:

```{r setup, include=FALSE}
hook_chunk = knitr::knit_hooks$get('chunk')

knitr::knit_hooks$set(chunk = function(x, options) {
regular_output = hook_chunk(x, options)
# add latex commands if chunk option singlespacing is TRUE
if (isTRUE(options$singlespacing))
sprintf("\\singlespacing\n %s \n\\doublespacing", regular_output)
else
regular_output
})

knitr::opts_chunk$set(echo = TRUE, singlespacing = TRUE)
```

一些有用的引用:

关于r - 如何在 R Markdown 中自动实现文本和代码块之间的不同间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56704022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com