Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render Latex markup using Python?

Tags:

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

EDIT:

I have python code like:

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

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

like image 661
kame Avatar asked Oct 26 '10 21:10

kame


2 Answers

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()
like image 158
Bernardo Kyotoku Avatar answered Oct 26 '22 06:10

Bernardo Kyotoku


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.

like image 38
Paul Rougieux Avatar answered Oct 26 '22 08:10

Paul Rougieux