2016-01-20 65 views
3

我试过使用反引号和撇号,但他们需要语言。我只想要一个带有高亮背景的纯文本格式化代码块。是否可以在R Markdown/knitr中创建伪代码的代码块?

使用eval=FALSEtidy=FALSE作品,直到达到一个关键字,如if,随后的代码将在.Rmd文件和输出PDF两种颜色突出显示。不通过选项指定语言将删除背景突出显示输出中代码的功能。

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

添加块选项eval = FALSE做你想要的吗? – atiretoo

+0

这不起作用,因为块中的代码被检查有效性,即使eval = FALSE 编辑:看起来事情近年来发生了变化 - eval = FALSE应该足够了。对不起! –

回答

3

下面是呈现到HTML就好了一些示例代码。它也可以正确呈现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. 
``` 
+0

不使用括号来设置语言的作品,但它删除了背景突出显示代码的选项。 – TomNash

+0

我也没有得到突出显示的背景,当我运行它到任何输出格式。我使用R 3.2.3,RStudio 0.99.491和knitr 1.11 – atiretoo

+0

这是我使用的方法(没有语言部分),然后编织到HTML并从那里打印。 – TomNash

1

用整齐= FALSE

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

pseudocode 

``` 

编辑:新增亮点,以防万一你色泽均匀

+0

这将删除表示它为输出文件中的代码的灰色背景。 – TomNash

相关问题