I'd like to store a string of inline latex math in an r object, then output this string so that when rmarkdown is knitted, a latex equation is output in pdf. I have an error when I include "\beta" in the string. Here's the example:
---
title: "Untitled"
author: "Tedward"
date: "February 12, 2015"
output: pdf_document
---
```{r}
b_0<-"$\beta_0$"
```
`r b_0`
error:
>output file: Test_beta.knit.md
>
>! Package inputenc Error: Keyboard character used is undefined
>(inputenc) in inputencoding `utf8'.
>
>See the inputenc package documentation for explanation.
>Type H <return> for immediate help.
> ...
>
>l.97 $^^H
>
>Try running pandoc with --latex-engine=xelatex.
>pandoc: Error producing PDF from TeX source
>Error: pandoc document conversion failed with error 43
>Execution halted
This is the same error I'd get if I have copied and pasted a beta character into the rmarkdown file. I thought about (and tried for a bit) to change the encoding to so as to include beta as a character, but realized that wouldn't solve my problem. I need the line of text to read $\beta$, not $β$. I've tried modifying the string to not include "$" :
```{r}
b_0<-"\beta_0"
```
$`r b_0`$
And the error is the same. I think what I'm finding is that R is reading "\beta" and converting it into β, which is not what I want.
What I'd like to be output is β with 0 as subscript.
Please forgive my ignorance of how these things work (I'm not sure how r, rmarkdown, latex, knitr, and pandoc all interact to produce the final pdf).
My ultimate objective is to create a large table that has latex equations (which include \beta) in it. I'd like to store the equations as variables so they are easy to edit in one location. If you know another way to achieve this, I'd appreciate it. I imagine this is possible in LaTex, but I'm more experienced with rmarkdown and hence that's my preference. My thinking/hope is that there's probably something simple that I'm missing here.
I'm working in Rstudio (0.98.1091.) on os x 10.10.2, with R 3.0.3
After testing my comment, it seems it works fine without additional complications.
A backslash is a special character in R strings, so you need to escape it with another backslash. Knitting:
---
title: "beta test"
author: "Gregor"
date: "Friday, February 13, 2015"
output: pdf_document
---
```{r}
b0 = "$\\beta_0$"
```
Let's see if it works: `r b0`.
Yields:
Backslashes in strings is a pretty common R-FAQ. For more info, see here or here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With