Tuesday, 7 November 2023

How to use LaTeX typesetting in TKinter Label texts

I have some Labels in TKinter, with label text such as Jx. However, this is intended to be $J_x$ and rendered according to standard LaTeX typesetting. For example, consider this MWE:

from tkinter import *
from tkinter import ttk
import tkinter.font as font
from tkinter import Tk
from turtle import bgcolor

import tkinter as tk

class UI:
    def __init__(self, master):
        self.master = master
        helv15 = font.Font(family="Helvetica",size=15,weight="bold")
        self.JxEntryLabel = Label(self.master, text = "Jx :", bg = 'white', fg = fontcolor, font = helv15)
        self.JxEntryLabel.place(relheight = 0.05, relwidth = 0.1, relx = 0.15, rely = 0.15) 

root=Tk()
app=UI(root)
root.title('Test_UI')
root.geometry("1200x600")
root.configure(background='white')

root.mainloop()

How can I edit/add to this MWE to ensure the Label button text typesets using LaTeX such that the x is a subscript to J and it is in math font? i.e. typing $J_x$ just shows $J_x$. For Matplotlib this is usual quite easy to achieve for plots, but I am not sure how to use the same tricks here.

Note: this question is different from this post, where the objective was to take latex input and display it in realtime, though I believe that parts of the answer can be reused to answer this question. However, I am not sure how. Any help is appreciated.



from How to use LaTeX typesetting in TKinter Label texts

No comments:

Post a Comment