如何在bookdown定理或示例环境中使用内联R代码

时间:2017-09-26 15:55:46

标签: r latex r-markdown bookdown theorem

我使用bookdown来生成html和PDF文档。如何在定理和示例环境中使用内联R代码的结果?

以下是我的尝试:

---
title: "Test"
output:
  bookdown::pdf_book:
    toc: false
html_document:
    toc: false
---

```{r}
a <- 2
b <- 3
```

If $a = `r a`$ and $b = `r b`$, then $a + b = `r a + b`$.

```{theorem}
If $a = `r a`$ and $b = `r b`$, then $a + b = `r a + b`$.
```

```{example}
If $a = `r a`$ and $b = `r b`$, then $a + b = `r a + b`$.
```
我得到了 Result

1 个答案:

答案 0 :(得分:1)

您可以使用显式Latex标记:

---
title: "Test"
output:
  bookdown::pdf_book:
    toc: false
html_document:
    toc: false
---

```{r}
a <- 2
b <- 3
```

\begin{theorem}

If $a = `r a`$ and $b = `r b`$, then $a + b = `r a + b`$.

\end{theorem}

\begin{example}

If $a = `r a`$ and $b = `r b`$, then $a + b = `r a + b`$.

\end{example}

enter image description here