Search code examples
rlatextex

How to convert R expressions to LaTeX/TeX without using a CAS?


I would like to be able to convert R expressions (ex. 1/2*x^2) or formulas into a character string that contains the LaTeX (ex. "\frac{1}{2} \times x^{2}" - or something similar). I know that expression() can be used for something similar, generating mathematical notation in figures, but I'm not sure how to convert a similar type of expression into LaTeX. I want to be able to include this into a Sweave document.

Please give an example (like with the 1/2*x^2), instead of just saying "use the tikzDevice package". It sounds like the Hmisc package might be able to do what I want, but I haven't figured out how yet.

I'm wanting to avoid using a CAS (computer algebra system), because I want it not have to rely on a complex external application, like that Ryacas uses, and want it to be able to run quickly.


Solution

  • latexTranslate provides valid LaTeX output from string input but only partial expansion. The translation of x/y will not be converted to the prefix version, \frac {1} {2}.

    require(Hmisc)
    latexTranslate("1/2*x^2")
    #[1] "1/2*x$^{2}$"