I recently found a post showing how to include Sympy output from python chunks in an rmarkdown document. It is rather easy to include LaTeX generated by Sympy in knitted output, but the output is not rendered in Rstudio as equations.

Is it possible to get Knitr to render LaTeX equations in such a way that Rstudio displays them as such? I have been playing around with the render option without succeeding.

Can you share an example of this ? This would help understand what you are talking about exactly, and help see what the IDE need to print as latex equation.

In an rmarkdown document, I include the reticulate package

```{r , include=FALSE}
library(reticulate)
```

Including a python chunk generates an equation display in the knitted output both for html and pdf.

```{python, results='asis'}
from sympy import *
x = symbols('beta_1')
a = Integral(cos(x)*exp(x), x)
print('$$' + latex(a) + '$$')
```

The displayed output in the rmarkdown, if I execute the current chunk (Ctrl-Shift-Enter) it displays the Latex code in the notebook rather than rendering the equation, however:

Python 3.6.11 (/Users/......)
Reticulate 1.18 REPL -- A Python interpreter in R.
$$\int e^{\beta_{1}} \cos{\left(\beta_{1} \right)}\, d\beta_{1}$$

Since I can't use Rstudio to work interactively with Sympy, I currently use Jupyter and convert to rmarkdown with:

To make this works, the python chunk result should be seen not as a character string printed but as an already formated result. With R, you need to use a special asis output.

I am not really sure you can do that with python chunk directly. Maybe reticulate allows that.
But it could be a limitation for python chunk in the IDE. Or an issue because result="asis" is not respected in the IDE (but works when knitting) - Maybe you could open a Feature Request / issue in the IDE repo ?
But it is possible related to this issue too: https://github.com/rstudio/rstudio/issues/1626

Using a mix of R and Python, you could make that works though.

---
title: "hello"
output: html_document
---
```{r , include=FALSE}
library(reticulate)
```
```{python, results='asis'}
from sympy import *
x = symbols('beta_1')
a = Integral(cos(x)*exp(x), x)
s = latex(a)
```
```{r}
knitr::asis_output(sprintf("$$%s$$", py$s))
```

Thanks, this is almost a solution. The equation display rendered is too big to be practical though, so I will continue to work with Sympy in Jupyter, converting it to Rmarkdown to integrate it with other work.

The simplest way to do this is to insert a python chunk in the imported code when a result is to be displayed with print('$$' + latex(a) + '$$'), as above. I will still mark your answer as a solution, since it could be used.