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
  tkinter showing image in button rwahdan 3 111 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 559 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 1,864 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Photo Tkinter TEXT background image _ShevaKadu 5 2,186 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter: Image to Label Maryan 10 1,094 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Scrollable big image in a window (TKinter) Prospekteur 3 730 Sep-14-2020, 03:06 AM
Last Post: Larz60+
  Adding an image to a tkinter window djwilson0495 2 2,429 Aug-23-2020, 11:07 AM
Last Post: ebolisa
  Create image on a Toplevel using tkinter ViktorWong 3 2,866 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Tkinter Logo Image Evil_Patrick 1 1,757 May-25-2020, 05:12 AM
Last Post: DT2000
  tkinter and image paths menator01 2 1,106 Apr-14-2020, 08:46 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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