# Display command output as math : IPython.display.Math?

**URL:** <https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125>\
**Category:** JupyterLab\
**Created:** [January 23, 2020, 10:30am UTC](https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125 "2020-01-23T10:30:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlaurens](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@jlaurens](https://discourse.jupyter.org/u/jlaurens)\
**Post date:** [January 23, 2020, 10:30am UTC](https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125/1 "2020-01-23T10:30:11Z")

</div>

Hi there,

my first topic.

I am using notebooks for teaching maths and python to under graduate students. Here is an example of the python code I use to have a math output

Given some symbolic computation

```auto
import sympy as sym
x = sym.symbols('x')
I = sym.integrate(1/(1+x**2), (x, 0, 1))

```

This is the display related part

```auto
sym.init_printing(use_latex='mathjax', use_unicode=True)
import IPython.display.Math
IPython.display.Math(rf'\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{{d}}t = {sympy.latex(I)}')

```

The output is  
 ![Capture d’écran 2020-01-23 à 11.25.41](https://canada1.discourse-cdn.com/flex031/uploads/jupyter/original/2X/6/6801ff6a19a3db3ca112c5ee47f9ba3ba28cb219.png)

As JupyterLab does not know about iPython object, how can I convert this last code to JupyterLab ?

Thanks

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [January 23, 2020, 4:36pm UTC](https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125/2 "2020-01-23T16:36:38Z")

</div>

Building on my example from [here](https://discourse.jupyter.org/t/how-to-add-markdown-inside-a-function-to-pretty-print-equation/2719/2) that works in JupyterLab, I converted your code. Works in current JupyterLab:

```python
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](https://realpython.com/python-f-strings/) (and even the [`str.format()` method](https://pyformat.info/)) 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](https://dpb.bitbucket.io/data-within-literal-curly-brackets-using-python-format.html) 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.

---

<div class="post-metadata">

**Author:** ![jasongrout](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/jasongrout/32/371_2.png) [@jasongrout](https://discourse.jupyter.org/u/jasongrout)\
**Post date:** [January 23, 2020, 7:50pm UTC](https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125/3 "2020-01-23T19:50:48Z")

</div>

And I’ll add that this works for me in JupyterLab:

```python
from IPython.display import Math
Math(rf'\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{{d}}t = ')

```

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [January 23, 2020, 7:59pm UTC](https://discourse.jupyter.org/t/display-command-output-as-math-ipython-display-math/3125/4 "2020-01-23T19:59:10Z")

</div>

That is nice because more in line with original post and doesn’t require cutting off dollar signs like mine to combine:

```python
from IPython.display import Math
Math(rf'\int_{{t=0}}^1 \frac{{1}}{{1+t^2}}\,\text{{d}}t = {sym.latex(I)}')

```
