kame
kame

Reputation: 21990

How to render Latex markup using Python?

How to show an easy latex-formula in python? Maybe numpy is the right choice?

I have python code like:

a = '\frac{a}{b}'

and want to print this in a graphical output (like matplotlib).

Upvotes: 30

Views: 134216

Answers (7)

user26751670
user26751670

Reputation: 11

To echo off Andrew's answer earlier, by far the easiest way to let matplotlib handle the latex rendering. But instead of modifying the matplotlibrc file, you can just set the parameter using rcParams:

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

Include this in your code and latex rendering in a matplotlib figure (or its derivatives should work much better. See here for more documentation:

https://matplotlib.org/stable/gallery/text_labels_and_annotations/tex_demo.html

(Sorry Andrew, I would've posted a follow-up comment but I don't have the reputation)

Upvotes: 1

Paul Rougieux
Paul Rougieux

Reputation: 11409

An answer based on this one specific to Jupyter notebook, using f-string to format an $x_i$ variable:

from IPython.display import display, Latex
for i in range(3):
    display(Latex(f'$x_{i}$'))

Screenshot of the output

Note: The f-string (formatted string literal) uses curly braces to insert the value of the Python variable i. You’ll need to double the curly braces (f'{{}}') to actually use {} in the LaTeX code. Otherwise, you can use single curly braces directly in a normal Python string (not an f-string).

Side Note: I'm surprised Stack Overflow still doesn’t have a math markup.

Upvotes: 22

Wojciech Moszczyński
Wojciech Moszczyński

Reputation: 3187

Creating mathematical formulas in Pandas.

a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")

enter image description here

a = r'f(x) = \frac{\exp(-x^2/2)}{\sqrt{2*\pi}}'
ax = plt.axes([0,0,0.3,0.3]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
ax.axis('off')
plt.text(0.4,0.4,'$%s$' %a,size=50,color="green")

enter image description here

Upvotes: 8

LF-DevJourney
LF-DevJourney

Reputation: 28524

Draw with matplotlib,

import matplotlib.pyplot as plt
a = r'\frac{a}{b}'
ax=plt.subplot(111)
ax.text(0.5,0.5,r"$%s$" %(a),fontsize=30,color="green")
plt.show()

enter image description here

Upvotes: 1

Bernardo Kyotoku
Bernardo Kyotoku

Reputation: 479

As suggested by Andrew little work around using matplotlib.

import matplotlib.pyplot as plt
a = '\\frac{a}{b}'  #notice escaped slash
plt.plot()
plt.text(0.5, 0.5,'$%s$'%a)
plt.show()

Upvotes: 23

restrepo
restrepo

Reputation: 497

Without ticks:

a = r'\frac{a}{b}'
ax = plt.axes([0,0,0.1,0.2]) #left,bottom,width,height
ax.set_xticks([])
ax.set_yticks([])
plt.text(0.3,0.4,'$%s$' %a,size=40)

Upvotes: 3

Andrew Jaffe
Andrew Jaffe

Reputation: 27097

Matplotlib can already do TeX, by setting text.usetex: True in ~/.matplotlib/matplotlibrc. Then, you can just use TeX in all displayed strings, e.g.,

ylabel(r"Temperature (K) [fixed $\beta=2$]")

(be sure to use the $ as in normal in-line TeX!). The r before the string means that no substitutions are made; otherwise you have to escape the slashes as mentioned.

More info at the matplotlib site.

Upvotes: 5

Related Questions