Search code examples
rknitrbookdown

latex R chunk output not decorated


When using bookdown, if I have an R chunk where I want to show output but not the R code, the default decorations are missing in the pdf output.

For example, this example has a nicely formatted chunk (gray background, coloured text, etc.)

```{r test1, echo=TRUE}
1:4
```

But this one does not have any decoration.

```{r test2, echo=FALSE}
1:4
```

The contrast is easily seen below.

enter image description here

How can I produce the second example with the gray background and coloured text?


Solution

  • You can give the output block a language name, e.g.,

    ```{r test2, echo=FALSE, class.output='r'}
    1:4
    ```
    

    By default, text output is generated to

    ```
    ```
    

    With class.output='lang', text output is generated to

    ```lang
    ```
    

    Without the language name, code blocks will not be syntax highlighted.