0

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

N00bDev
  • 67
  • 8

2 Answers2

2

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

Wayne
  • 6,607
  • 8
  • 36
  • 93
  • Thank you! I wasn't aware of "raw" – N00bDev Oct 12 '22 at 17:16
  • 2
    Yes, it is one of those very useful things that is hard to find until you learn about it. It often comes up when dealing with paths on Windows-based machines. Or with regular expressions. [This looks like a good intro](https://blog.devgenius.io/beauty-of-raw-strings-in-python-fa627d674cbf) that I probably should have included. – Wayne Oct 12 '22 at 17:45
-1

You may find this Latexify library helpful! You should be able to generate clean Latex descriptions from your function definitions.