One was installed in the afternoon pylatex modular , Intend to LaTex The mathematical formula described by grammar is transformed into pictures . Stumbling through the document for a long time , Find out pylatex It's a heavy weapon , It's not concise enough . I searched the Internet ,CSDN There are ready-made Python Code , It's just not free , want 11 individual C The coin . Feel in your pocket , There are some loose silver , I just bought one . After downloading , Read the source code , It's just a package http request , Images are generated online ! This is robbery , It's too unethical !
It took a minute , Adjust your mood , Think about it or write it yourself . Soon ,10 Line code ,LaTex Turn the mathematical formula into a picture . Although it's a little bit crude , It's cool to use . If you need color 、 Transparency or something , Please add it yourself .
import matplotlib.pyplot as plt
def formula2img(str_latex, out_file, img_size=(5,3), font_size=16):
fig = plt.figure(figsize=img_size)
ax = fig.add_axes([0, 0, 1, 1])
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_xticks([])
ax.set_yticks([])
plt.text(0.5, 0.5, str_latex, fontsize=font_size, verticalalignment='center', horizontalalignment='center')
plt.savefig(out_file)
Let's see how the output works :
str_latex = r'$10^x$'
formula2img(str_latex, r'd:\f1.png', img_size=(3,2), font_size=64)
str_latex = r'$s=\frac{1-z^{-1}}{T}$'
formula2img(str_latex, r'd:\f2.png', img_size=(5,3), font_size=64)
str_latex = r'$H(z)=\frac{Y(z)}{X(z)}=\frac{1}{RC{\frac{1-z^{-1}}{T}+1}}=\frac{T}{RC(1-Z^{-1})+T}$'
formula2img(str_latex, r'd:\f3.png', img_size=(18,3), font_size=64)
str_latex = r'$Y(n)=(\frac{1}{1+\frac{RC}{T}})X(n)+(\frac{\frac{RC}{T}}{1+\frac{RC}{T}})X(n-1)$'
formula2img(str_latex, r'd:\f4.png', img_size=(18,3), font_size=64)