0

I want to write title in python figure, which contain variable n=100. I want to write as $t_n=0.1$ (n is subscript). I try as follows:

import matplotlib.pyplot as plt

n=100
plt.figure()
plt.title(f'$t_{n:3d}=0.1$')
plt.show()

But it shows as follows:

enter image description here

It shows that 1 is subscript and 00 is normal text. I want 100 is subscript.

If I change the code into plt.title(f'$t_{{n:3d}}=0.1$') the result is

enter image description here

The result is not as my desired. So, how to do it?

0

1 Answer 1

1

The problem you're encountering is because of f-string. The first layer of curly braces, {n:3d}, is interpreted as the formatting curly braces. When you add two braces, {{n:3d}}, it is interpreted as a single curly brace. So your latex actually reads $t_{n:3d}$.

To overcome this, you need another level of braces, {{{n:3d}}}. The first two braces will escape and the last (interior) one will be accessed by the f-string in python.

Not the answer you're looking for? Browse other questions tagged or ask your own question.