Reputation: 4024
I'm trying to do computer algebra in R using Ryacas and RMarkdown.
Here's a quick example.
```{r}
library(Ryacas)
x = Sym("x")
y = Sym("y")
x0 = Sym("x0")
y0 = Sym("y0")
x1 = Sym("x1")
y1 = Sym("y1")
yacas("TeXForm(Solve( (y - y0) / (x - x0) == (y1 - y0) / (x1 - x0), y) )")
```
How can I get text from the R output parsed as LaTeX?
Edit:
results = asis
did work, but the output is wrapped in single rather that double dollar signs. Only the double dollar sign expressions seem to parse correctly (try knitting the below).
$$\left( y = - \left( \frac{ - y_{0}}{x - x_{0}} - \frac{y_{1} - y_{0}}{x_{1} - x_{0}} \right) \left( x - x_{0}\right) \right) $$
$\left( y = - \left( \frac{ - y_{0}}{x - x_{0}} - \frac{y_{1} - y_{0}}{x_{1} - x_{0}} \right) \left( x - x_{0}\right) \right) $
Upvotes: 3
Views: 256
Reputation: 4024
Another option (which I think is far preferable):
```{python3, results = "asis"}
from sympy import *
def LaTeX(math):
return("$$" + str(math) + "$$")
x, y = symbols("x_0, y_0")
print(LaTeX(x + y))
```
Upvotes: 0
Reputation: 4024
Thanks baptiste and Konrad Rudolph.
```{r, results = "asis"}
library(Ryacas)
library(magrittr)
library(stringi)
print.Expr = function (x, ...) {
answer = yacas(x, ...)
answer$YacasForm %<>% stri_replace_all_fixed("$", "$$") %>% stri_sub(2, -3)
print(answer)
}
print.Sym = function (x, ...) {
answer = yacas(unclass(x), ...)
answer$YacasForm %<>% stri_replace_all_fixed("$", "$$") %>% stri_sub(2, -3)
print(answer)
}
x = Sym("x")
y = Sym("y")
TeXForm(x + y)
```
Upvotes: 1