How to render latex code in a string from an rmarkdown code chunk?

Viewed 251

In R, using an ordinary rmarkdown code chunk (ordinary here means opposite of inline code), how to render some latex code?

Here's an illustrative example:

---
title: "beta test"
output:
  html_document
---

```{r}
a = 42
b = "$\\beta$"
c = "$a + 2$"
d = "a + 2"
a
b
c
d
```

`r a`

`r b`

`r c`

`r d`

that generates:

enter image description here

I would like to render the latex code, e.g., the $\\beta$ as it is in the case of the inline code.

My ultimate goal is to generate rendered latex code programmatically from "triple backticks" code chunks.

1 Answers

That's Mathjax code rather than LaTeX. output:html_document indicates that you're producing a html doc.

Anyway, you may try results="asis" and use just one backslash \beta. However, this seems not to be compatible anymore with inline code; a workaround would be to use two variables.

---
 title: "beta test"
 output:
   html_document
---

```{r, results='asis'}
 a = 42
 b = "$\\beta$"
 b1 = "$\beta$"
 c = "$a + 2$"
 d = "a + 2"
 a
 b
 b1
 c
 d
```

`r a`

`r b`

`r b1`

`r c`

`r d`

Produces

enter image description here

The inline code

Related