2016-01-20 113 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

相關問題