Search code examples
pythonjupyter-notebooklatex

Latex in Jupyter Notebook iPython


I have a python project in jupyter notebook, and I want to display the final output with latex.

x = 5
print("The number you have inputted is ", complex(x, y), " ^ 1/", n, sep = '')

I want this to be formatted using latex:

Input to be formatted


I read a bunch of forums but the fraction wasn't working for 2digit numbers

n=10
from IPython.display import display, Markdown
display(Markdown(rf"""$ { complex(x, y) } ^ \frac{1}{n} $"""))

The best I could get:

Input to be formatted


Any suggestions would be very helpful :)


Solution

  • You can directly display it as Latex using Latex. To keep the curly brackets as literals for Latex you need to use double {{ }}. Otherwise the \frac does not obtain the full operands.

    from IPython.display import display, Latex
    
    x=1
    y=0
    n=10
    
    display(Latex(rf'$ { complex(x, y) } ^\frac{{1}}{{{n}}}$'))
    

    Output in Jupyterlab: enter image description here