Eng

Rus

Main page

Converting TeX markup to SVG with Matplotlib

import io import matplotlib.pyplot as plt # set "Computer Modern" font plt.rc("mathtext", fontset="cm") def SVG_from_TeX(formula, fontsize=12, dpi=600): img = plt.figure(figsize=(1, 1)) img.text(0, 0, formula, fontsize=fontsize) buffer = io.BytesIO() img.savefig( buffer, dpi=dpi, transparent=True, format="svg", bbox_inches="tight", pad_inches=0.0, ) plt.close(img) return buffer.getvalue() svg_bytes = SVG_from_TeX(r"$D_{ik}^{k} = min(D_{ik}^{k-1}, D_{ik}^{k-1} + D_{kk}^{k-1}) = min(D_{ik}^{k-1}, D_{ik}^{k-1}) = D_{ik}^{k-1}$") open("out.svg", "wb").write(svg_bytes)