0

I'm trying to convert some mathematical equations to a Word (.docx) file using the code snippet below:

y = 5
cur_par.add_run(r"\frac{-e^{i/pi}}{y^n}$!").font.math = True

However I would like to replace the "y" with the variable y (=5). I could write fr"{y}" or r."{y}.format(y=y), but both will recognize the other curly brackets and assume they are variables as well, which isn't the case.

Any idea how I could combine a raw string containing {} with an f-string?

Johan Tuls
  • 105
  • 7
  • 1
    you can make a literal brace with `{{` or `}}` ... if thats your question? `y=5;print(f' {{ "y": {y} }}')` – Joran Beasley Sep 30 '22 at 05:21
  • 2
    Does this answer your question? [How to escape curly-brackets in f-strings?](https://stackoverflow.com/questions/42521230/how-to-escape-curly-brackets-in-f-strings) – Mechanic Pig Sep 30 '22 at 05:22
  • It seems it works, even though I have to pay with readability: `cur_par.add_run(fr"\frac{{-e^{{i/pi}}}}{{{y}^n}}$!").font.math = True` – Johan Tuls Sep 30 '22 at 05:27
  • @JohanTuls lol ok ... if you want to you could probably submit a MR to python with a different escape thing ... or you could use `%s` style formatting and normal curly braces ... theres a few options if your goal is to make it readable – Joran Beasley Sep 30 '22 at 05:31

0 Answers0