Python Forum
Latex image too big on tkinter using sympy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Latex image too big on tkinter using sympy
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 \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 ) 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.resize((height, width))
    photo = ImageTk.PhotoImage(image)

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)
Gribouillis likes this post
Hi, 2 observations:

1) your default output is 600x800, that is a classic 3/4.
You seem to resize to 1/3. Maybe it does not matter, but it usually does.

2) I use a different syntax to resize some png cards:

crd ='cards/' + 'card1.png')
img = ImageTk.PhotoImage(crd.resize((167, 242)))

Hope it helps,

Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Tkinter TEXT background image _ShevaKadu 5 1,147 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter: Image to Label Maryan 10 473 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Scrollable big image in a window (TKinter) Prospekteur 3 352 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 1,229 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  Create image on a Toplevel using tkinter ViktorWong 3 1,921 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Tkinter Logo Image Evil_Patrick 1 997 May-25-2020, 05:12 AM
Last Post: DT2000
  tkinter and image paths menator01 2 631 Apr-14-2020, 08:46 PM
Last Post: menator01
  [Tkinter] Image editor using Tkinter and PIL Raulica 2 1,559 Dec-03-2019, 10:10 PM
Last Post: joe_momma
  Unable to put background image on Tkinter Frame jenkins43 2 5,130 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  Zooming a tkinter image GalaxyCoyote 2 3,657 Nov-09-2019, 04:36 PM
Last Post: GalaxyCoyote

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020