Joe
Joe

Reputation: 13

In Jupyter Notebook, how do you use latex symbols like $\times$ or $\nu$?

I'd like to express a label on one of my plots in scientific notation. I write it like this in Jupyter:

... myLabel = $c=0.46 \times 10^{15} cm^2/n_\textrm{eq}$ plt.text(0.55,39,myLabel,fontsize=16) ...

The problem is that the \t character creates a tab and then prints the "imes". Same problem with the \textrm. I've also run into this problem trying to write the greek letter nu, but I got around it with an italic v.

I've googled around and found no good solution. Any fix will be greatly appreciated!

Upvotes: 1

Views: 892

Answers (2)

J.y B.y
J.y B.y

Reputation: 605

Could the method "Latex" from the display module of IPython be of service here? (Note that you do need to add quotes to your string definition, and to double you backslashes for them to be left untouched on input):

from IPython.display import Latex
myLabel = "$c=0.46 \\times 10^{15} cm^2/n_{\\textrm{eq}}$" 
print(myLabel)
Latex(myLabel)

In a standard colab.research.google.com python notebook, it yields the following result:

enter image description here

Upvotes: 0

Esther
Esther

Reputation: 436

You could try something like this post provides.

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['text.usetex'] = True

t = np.linspace(0.0, 1.0, 100)
s = np.cos(4 * np.pi * t) + 2

fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
ax.plot(t, s)

ax.set_xlabel(r'\textbf{time (s)}')
ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
             r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')

screenshot

First, set plt.rcParams['text.usetex'] = True. Then, instead of using the $ symbol, try it with r'c=0.46 \times 10^{15} cm^2/n_\textrm{eq}', as in the previous example.

I hope this is helpful!

Upvotes: 2

Related Questions