I'm in trouble, so it's a memorandum. The environment is Python 3.8.0, matplotlib 3.1.2.
I wrote the following source with the intention of outputting the variable ʻa` as a superscript with matplotlib.
import numpy as np
import matplotlib.pyplot as plt
a = 14
x = np.arange(0, 1, 0.1)
y = x**a
plt.plot(x, y, label=rf'$y=x^{a}$') #Problem area
plt.legend()
plt.show()
When I plotted it, I got the following figure. Unfortunately, only the first character of the variable ʻa` has been identified as a superscript.
I thought it wasn't okay, and when I tried plotting with one more curly braces like $ y = x ^ {{a}} $
, it was displayed as $ y = x ^ a $. It was.
As a result, it was solved by increasing the curly braces in triple as follows.
import numpy as np
import matplotlib.pyplot as plt
a = 14
x = np.arange(0, 1, 0.1)
y = x**a
plt.plot(x, y, label=rf'$y=x^{{{a}}}$') #Zubatsu and solution
plt.legend()
plt.show()
I wrote it in Python documentation.
The part of the string outside the curly braces is treated as written. However, the double curly braces'{{' or'}}' are replaced with the corresponding single curly braces.
In other words, when the first curly braces are single, in " f'{a}'
", ʻa is replaced with
14` and the curly braces are erased, so only the first character is superscripted. It was.
Then in the case of doubles, in " f'{{a}}'
", the double curly braces were replaced with single curly braces, and{a}
was processed with ʻa` intact. So, it was displayed as $ y = x ^ a $.
So if you make a triple " f'{{{a}}}'
", double curly braces will be replaced with single curly braces, and ʻa will be replaced with
14and
{14}will be replaced. Since it was processed,
14` was correctly recognized as a superscript.
It looks like the following.
a = 14
print(f'x^{a}') # x^14
print(f'x^{{a}}') # x^{a}
print(f'x^{{{a}}}') # x^{14}
The correct Tex syntax is at the bottom.
that's all. I'll do my best in Python.