Nov-03-2020, 12:47 AM
I'm trying to create small images of mathematical forumulas using tkinter, sympy, and latex. The images generated are way too big and I don't know how to reduce the size. When I searched online, people said that using
I made a test program to experiment:
\documentclass{standalone}
will make the generated image the minimum size sufficient to fit the contents, but that hasn't happened when I tried it. The images generated seem to be 600 by 800 points no matter what I try. I've tried various things like adding the option [convert={size=25x50}]
to the document class and \usepackage[active,tightpage,textmath]{preview}
(per this page https://tex.stackexchange.com/questions/...e-any-page ) and \documentclass[tightpage]{standalone}
but nothing worked.I made a test program to experiment:
import tkinter as tk import sympy as sp from PIL import Image, ImageTk from io import BytesIO import os def latex(s, label, fontsize=1, height=100, width=300): stream = BytesIO() sp.preview(r"$%s$\end{document}" % s, euler=False, viewer="BytesIO", output="ps",\ outputbuffer=stream, premable=r"\documentclass[tightpage]{standalone}\begin{document}") # [convert={size=25x50}] image = Image.open(stream) image.resize((height, width)) image.load(scale=fontsize) photo = ImageTk.PhotoImage(image) label.config(image=photo) label.image=photo stream.close() def loadLatex(): latex(var.get(), label, fontsize=10, height=1, width=2) win = tk.Tk() label = tk.Label(win) var = tk.StringVar() tk.Entry(win, textvariable=var).grid(row=0, column=0) tk.Button(win, text="Latex", command=loadLatex).grid(row=1, column=0) label.grid(row=2, column=0) win.mainloop()