Search code examples
rclasslatexsymbolic-mathyacas

Coerce TeXForm("Sqrt(x^2/(z^2+1))") result to a character automatically


I have formula that I converted to LaTeX by Ryacas::TeXForm. I can`t get from the result character sting that contains LaTeX code, without doing it by hand. I tryed:

y<-"Sqrt(x^2/(z^2+1))"
Sym(y)
z<-TeXForm(y)

I got:

"$\sqrt{\frac{x ^{2}}{z ^{2} + 1} }$";

But class(z) gives both "Sym" and "character"

Then I tried force it to something like w<-"$\sqrt{\frac{x ^{2}}{z ^{2} + 1} }$" such that:

 class(w)
[1] "character"

Of course, I can do in just by copying and pasting, but how to do it automatically?


Solution

  • A possible approach via Ryacas:

    > yacas(TeXForm(y))
    "$\sqrt{\frac{x ^{2}}{z ^{2} + 1} }$";
    
    > str(yacas(TeXForm(y)))
    List of 2
     $          : NULL
     $ YacasForm: chr "\"$\\sqrt{\\frac{x ^{2}}{z ^{2} + 1} }$\";"
     - attr(*, "class")= chr "yacas"
    
    > str(yacas(TeXForm(y))$YacasForm)
     chr "\"$\\sqrt{\\frac{x ^{2}}{z ^{2} + 1} }$\";"