Akshay Sabib
Akshay Sabib

Reputation: 31

Ryacas latex expression in Knitr

I have the following RYacas expression:

longrun_cost <- (c * mu) + h * ( lambda / (mu-lambda))

The latex expression of this is as follows :

fmt <- sprintf("TeXForm(%s)", longrun_cost)
yacas(fmt, retclass = "unquote")


$c \mu  + \frac{h \lambda }{\mu  - \lambda } $

I want to be able to write this equation in a format that Knitr would understand, but the only solution was copy the latex expression to knitr as

$$c \mu  + \frac{h \lambda }{\mu  - \lambda } $$

When I compose an R chunk

```{r}
longrun_cost <- (c * mu) + h * ( lambda / (mu-lambda))
fmt <- sprintf("TeXForm(%s)", longrun_cost)
yacas(fmt, retclass = "unquote")

```

It does not work. Any ideas? Thanks a lot!

Upvotes: 3

Views: 287

Answers (1)

Peter
Peter

Reputation: 7790

The challenge here is to handle character strings, and quotation marks. Consider the following .Rmd file

---
title: "Ryacas \\LaTeX\\ Expression in Knitr"
output: pdf_document()
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE)
```


Set up chunk:
```{r} 
library(Ryacas)
longrun_cost <- expression((c * mu) + h * ( lambda / (mu-lambda)))
fmt <- sprintf("TeXForm(%s)", longrun_cost)
eqn_string <- sprintf("$%s$", yacas(fmt, retclass = "unquote"))
```

The next few chunks will show the different ways that the Ryacas expression
can be rendered.

# Chunk 1
A chunk with `results = "markup"`.
```{r}
eqn_string
```

# Chunk 2
A chunk with `results = "asis"`.
```{r, results = "asis"}
cat(eqn_string)
```

```{r}
print(sessionInfo(), local = FALSE)
```

which produces the following output:

enter image description here

Upvotes: 2

Related Questions