Search code examples
jupyter-notebooklatex

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!


Solution

  • 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!