Building on my example from here that works in JupyterLab, I converted your code. Works in current JupyterLab:
from IPython.display import Markdown, display
def printmd(string):
display(Markdown(string))
import sympy as sym
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))
equation = r'$$\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{d}t$$'
printmd('**THE EQUATION:**')
printmd(f'{equation}')
printmd('**THE RESULT:**')
printmd(f'$$ = {sym.latex(I)}$$')
printmd('**COMBINED:**')
printmd(r'$$\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{d}t' + f' = {sym.latex(I)}$$')
printmd('**OR, COMBINED:**')
printmd(equation[:-2] + f' = {sym.latex(I)}$$') #alternative way to leave off right-side dollar signs
Related notes: Python’s new f-strings are awesome; however, mixing f-strings (and even the str.format()
method) with integral notation is a bit of a nightmare because of the braces. And just best avoided, if possible. Luckily, you weren’t substituting in the left side, but I imagine approaches like this will help with that. In current versions of Jupyter, both JupyterLab and the classic interface you can use dollar signs to ‘fence’ LaTeX code in markdown cells and have it rendered in the notebook via MathJax. The trick I show there making a printmd()
function just brings the markdown rendering to the code cells as well.