在Knitr的Ryacas乳胶表达

时间:2012-08-26 20:13:09

标签: latex knitr

我有以下RYacas表达式:

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

乳胶表达如下:

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


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

我希望能够以Knitr理解的格式编写这个等式,但唯一的解决方案是将乳胶表达式复制到knitr

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

当我编写R块时

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

```

它不起作用。有任何想法吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

这里的挑战是处理字符串和引号。请考虑以下.Rmd文件

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

产生以下输出:

enter image description here