Is it possible for Python to display LaTex in real time in a text box?

后端 未结 4 2161
孤独总比滥情好
孤独总比滥情好 2020-12-28 22:47

I plan on making a GUI where the user would input their mathematical expressions into a text box, such as Tkinter Entry Widget, and their expressions would be displayed in t

4条回答
  •  醉酒成梦
    2020-12-28 23:27

    This question is way too broad. I'm not overly sure should it be closed for that matter. Nontheless, here's a snippet on how to, at least, get latex working with Tk and matplotlib interactively.

    Enter something in the Entry widget and press enter.

    import matplotlib
    import matplotlib.pyplot as plt
    
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    matplotlib.use('TkAgg')
    
    from Tkinter import *
    from ttk import *
    
    def graph(text):
        tmptext = entry.get()
        tmptext = "$"+tmptext+"$"
    
        ax.clear()
        ax.text(0.2, 0.6, tmptext, fontsize = 50)  
        canvas.draw()
    
    
    root = Tk()
    
    mainframe = Frame(root)
    mainframe.pack()
    
    text = StringVar()
    entry = Entry(mainframe, width=70, textvariable=text)
    entry.pack()
    
    label = Label(mainframe)
    label.pack()
    
    fig = matplotlib.figure.Figure(figsize=(5, 4), dpi=100)
    ax = fig.add_subplot(111)
    
    canvas = FigureCanvasTkAgg(fig, master=label)
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
    canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
    
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    
    root.bind('', graph)
    root.mainloop()
    

    code should produce a window such as one bellow:

    If you want to have a nice interface like theirs this will not be enough. They most likely have something like a plaintext to Latex to Unicode conversion setup. Or even perhaps directly from plaintext to Unicode but I am not aware of any parser for math expressions as nice as latex so all the rules would presumably have to be re-coded and that's a lot of work so they most likely skipped that step and instead let latex do the heavy lifting and then just parse latex to Unicode/Utf8 or whichever encoding can handle all the signs.

    Then they dumped everything through something "extra" (i.e. django and jinja templating) that assigns each element its own nice css class based on the type of element (binary operator, variable, exponent...) in order to have the full math output that looks nice and can still be copied.

    In any case there's so much going on in this question it's not really possible to give a concise all-encompassing easy answer.

提交回复
热议问题