knitr输出中的r输入的普通代码块

发布于 2025-01-29 02:35:58 字数 1212 浏览 5 评论 0 原文

目标

我想转换为乳胶的R/Markdown文档中的 的.RMD文档,

```{r}
x <- 1 + 1
```

是设置 knitr 选项,以便整个文档默认情况下使用所有代码输入的普通代码块。因此,对于带有我希望

```
x <- 1 + 1
```

使用突出显示选项

我希望可以使用 imak>亮点= false 选项。更确切地说,对于上面的简单示例, knit()默认情况下会产生一个R型(即,带有 ighlight = true true ):

```r
x <- 1 + 1
```

设置 knitr ::后opts_chunk $ set(亮点= false)产生了文本锁定:

```text
x <- 1 + 1
```

但是我想拥有一个没有任何特殊语言的普通块,请参见上文。

结合Lang选项,

我可以通过所需的内容

knitr::opts_chunk$set(highlight = TRUE, lang = "")

,因此可以启用突出显示,但将 lang 设置为空字符串。这确实产生了我想拥有的普通代码。

不过,至少有一个缺点(除了这种解决方案的骇人听闻的感觉之外)。也就是说,如果在同一文档中我想在一个特定块的选项中启用突出显示,我必须设置 lang =“ r” 现在而不是 right = right = true = true ,例如,

```{r, lang="r"}
x <- 1 + 1
```

我想知道是否有更好的解决方案?

pandoc (我尝试的2.9.x)的旧版本中的背景

转换为 {verbatim} latex输出中的代码块。

但是, pandoc (我尝试的2.17.x)的最新版本将文本个铃声转换为 {shaded} 而不是将普通的块转换为 {verbatim }

Goal

In an R/Markdown document that I want to convert to LaTeX I want to set knitr options so that the entire document uses plain code chunks for all code inputs by default. Thus, for a .Rmd document with

```{r}
x <- 1 + 1
```

I want to obtain the output

```
x <- 1 + 1
```

Using the highlight option

I had hoped that the highlight=FALSE option could be used for this but this generates text-chunks rather than plain chunks. More precisely, for the simple example above, knit() produces an R-chunk by default (i.e., with highlight = TRUE):

```r
x <- 1 + 1
```

After setting knitr::opts_chunk$set(highlight = FALSE) a text-chunk is produced:

```text
x <- 1 + 1
```

But what I would like to have a plain chunk without any special language, see above.

Combination with lang option

I can obtain what I want via

knitr::opts_chunk$set(highlight = TRUE, lang = "")

Thus, I do enable highlighting but set the lang to an empty string. This indeed yields the plain code I want to have.

There is at least one disadvantage, though (apart from the rather hacky feel of this solution). Namely, if in the same document I do want to enable highlighting in the options of one specific chunk, I have to set lang = "r" now instead of highlight = TRUE, e.g.,

```{r, lang="r"}
x <- 1 + 1
```

So I wonder whether there is a better solution for this?

Background

In older versions of pandoc (I tried 2.9.x) text-chunks were converted to {verbatim} code chunks in LaTeX output.

However, more recent versions of pandoc (I tried 2.17.x) text-chunks are converted to {Shaded} instead and only plain chunks are converted to {verbatim}.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2025-02-05 02:35:58

另一个黑客解决方案是(我什至不敢解释它):

```{r}
knitr::opts_hooks$set(highlight = function(options) {
  if (!options$highlight) {
    options$highlight = TRUE
    options$lang = ''
  }
  options
})

knitr::opts_chunk$set(highlight = FALSE)
```

```{r}
1 + 1
```

```{r, highlight=TRUE}
2 + 2
```

语言名称 text right = false = false 当前在 knitr ,因此您无法更改它:我愿意使其可配置,但我不确定如何(我希望不添加新的新在亮点= false 的情况下设置语言的块选项。

Another hacky solution is (I don't even dare to explain it):

```{r}
knitr::opts_hooks$set(highlight = function(options) {
  if (!options$highlight) {
    options$highlight = TRUE
    options$lang = ''
  }
  options
})

knitr::opts_chunk$set(highlight = FALSE)
```

```{r}
1 + 1
```

```{r, highlight=TRUE}
2 + 2
```

The language name text when highlight = FALSE is currently hard-coded in knitr, so you cannot change it: https://github.com/yihui/knitr/blob/907184f82/R/hooks-md.R#L171 I'm open to making it configurable, but I'm not sure how (I hope not to add a new chunk option for setting the language in the case of highlight = FALSE).

清泪尽 2025-02-05 02:35:58

我不确定我是否完全了解您想要什么,但是在YAML标题中是否有帮助?

---
output:
  html_document:
    theme: null
    highlight: null
---

I'm not sure I entirely understand what you want, but does this in the yaml header help?

---
output:
  html_document:
    theme: null
    highlight: null
---
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文