# How to display D-stroke symbol in matplotlib

I want to display the D-stroke symbol (\DJ) using the following code, but I failed. Could anyone tell me how to make it correct?

As shown in the figure, the symbol $\mu$ works well but the $\DJ$ symbol doesn't appear.

Any advice and suggestions will be greatly appreciated!

import matplotlib as mpl
mpl.use("pgf")
pgf_with_custom_preamble = {
"text.usetex": True,    # use inline math for ticks
"pgf.rcfonts": False,   # don't setup fonts from rc parameters
"pgf.preamble": [
'\\usepackage[utf8]{inputenc}'
'\\usepackage[T1]{fontenc}'
'\\usepackage{tgtermes}'
]
}
mpl.rcParams.update(pgf_with_custom_preamble)
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x = [1,2,3,4,5]
y = [10, 20, 30, 40, 50]
plt.plot(x,y)
plt.xlabel('$\mu$')
plt.title('$\DJ$')

• Welcome to TeX.SX! I think this is marginally on topic, because it's not specified how the translation to TeX is done. – egreg Feb 25 '17 at 18:13

The symbol \DJ belongs to text mode and creates a warning in math mode:

LaTeX Warning: Command \DJ invalid in math mode ...


Solution is using text mode for the symbol (\mbox{\DJ} inside math or \text{\DJ} if package amstext or amsmath is loaded):

...
plt.xlabel(r'$\mu$')
plt.title(r'\DJ')
plt.savefig('test.pdf')


• Dear Heiko, thank you very much! It works now! – Zidan Feb 25 '17 at 19:01