在pdf输出中的示例/练习块中包含代码块

问题描述 投票:0回答:1

Bookdown通过添加environments for theorems, examples and exercises(及更多)来扩展R降价。我想在示例环境中放置代码块:

```{example, label="ex1"}
Here is an example:

```{r eval=FALSE}
x <- 1+1
```

```

上面的代码不起作用,因为代码块已经在```环境中。

我发现了以下解决方法,当我将该文档编织到html页面时,该方法很好用:

```{example, label="ex1"}
Here is an example:

<pre><code>
x <- 1+1
</code></pre>

```

[不幸的是,当我将文档编织为pdf文件时,这不起作用-相反,我只得到一个空白。有没有办法做到这一点同时适用于html和pdf输出?

r r-markdown bookdown
1个回答
0
投票

该解决方案非常简单,并且根据您的输出格式独立。您可以使用此表示法包括R个块。

````markdown
`r ''````{r}
plot(cars)
```
````

结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.