Search code examples
pythonlatexjupyterf-string

LaTex - Is it possible to display nice equations, and normal text together while using display(Math())?


Can I print normal text while using display(Math()) with F-string?

I would like to use F-string and mix it with nice equations, but I can't figure out how to print normal text. As you can see in the first image the text isn't normal(font). I also had to add \ after each string in order to create a space.

I have tried some options like \text, \text{} \, etc... but it doesn't work with F-string.

I'm using Jupyter if it helps.

Thank you!

display(Math(f"Press\ '1'\ to\ compute\ {x}^{y}\ or\ '2'\ to\ compute\\frac{x}{y}"))

How to change this to normal font?

This is how it should look


Solution

  • You need to double-bracket the brackets you normally use with \text{} and add in using Python's raw string in combination with your f-string (based on here):

    from IPython.display import Math
    x = 2
    y = 4
    Math(rf'\text{{Press 1 to compute }} {x}^{y} \text{{or 2 to compute }} \frac{x}{y}')
    

    Actual display with formatting shown:

    enter image description here