简体   繁体   English

是否可以在R Markdown / knitr中创建伪代码的代码块?

[英]Is it possible to create a code block of pseudocode in R Markdown/knitr?

I've tried using backticks and tildes, but they require languages. 我尝试过使用反引号和波浪号,但它们需要语言。 I just want a formatted code block of plain text with highlighted background. 我只想要一个带有突出显示背景的纯文本代码块。

Using eval=FALSE and tidy=FALSE works until reaching a keyword such as if , the subsequent code will be color highlighted in both the .Rmd file and the output PDF. 使用eval=FALSEtidy=FALSE直到达到关键字(如if ,后续代码将在.Rmd文件和输出PDF中以颜色突出显示。 Not specifying a language through the options removes the capability of background highlighting the code in the output. 不通过选项指定语言会删除背景突出显示输出中的代码的功能。

```{r, eval=FALSE,tidy=FALSE}
loop through each species
    loop through each measurement of the current species
         if measurement ...
etc.
```

Here's some sample code that renders to HTML just fine. 这里有一些示例代码可以很好地呈现给HTML。 It also renders correctly to Word and PDF. 它还可以正确呈现Word和PDF。

---
title: "Untitled"
output: html_document
---

Here is some plain text.

Next, let's write some pseudo code.  Note that you don't _have_ to specify a language if using plain backticks.

```
object <- [some kind of calculation]
```

If you set `eval = FALSE` you can get the highlighted background

```{r, eval = FALSE}
object <- [some kind of calculation]
Note that this; is not valid R code
```

You may find it interesting that your example works just fine for me.

```{r, eval=FALSE}
loop through each species
    loop through each measurement of the current species
    ...
etc.
```

Use tidy = FALSE 使用tidy = FALSE

```{r, tidy=FALSE, eval=FALSE, highlight=FALSE }

pseudocode

```

edit: Added highlight, just in case you uniform color 编辑:添加突出显示,以防您统一颜色

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

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