9

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

Tedward
  • 1,652
  • 15
  • 19
  • You'll almost certainly need to escape the backslash with another one, e.g. `b_0 = "$\\beta_0$"`. However, you may need a custom stitch or purl or something (I'm bad at the knitr terminology) to make sure this inline code has the equivalent of the `output = 'asis'` chunk option. But try it with the double backslash and see if that's enough. – Gregor Thomas Feb 13 '15 at 19:34

1 Answers1

12

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:

enter image description here

Backslashes in strings is a pretty common R-FAQ. For more info, see here or here.

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 1
    Gregor, you are my hero for the day. I knew it had to be something like that, thanks for your help. Thanks too for the helpful links. – Tedward Feb 13 '15 at 22:29