Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use inline R code in a bookdown theorem or example environment

I use bookdown to generate documents in both html and PDF. How could I use results from inline R code in theorem and example environments?

Here is what I tried:

---
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`$.
```

and I get Result

like image 294
QuantIbex Avatar asked Sep 26 '17 15:09

QuantIbex


1 Answers

You could go with explicit Latex tags:

---
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

like image 105
eipi10 Avatar answered Sep 19 '22 21:09

eipi10