Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the same manner as you would see when you type your expressions in symbolab.

Is this possible? If so, then what modules do I have to use in order to accomplish this?

like image 780
Jasmeet Brar Avatar asked Apr 15 '16 00:04

Jasmeet Brar


Video Answer


1 Answers

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')

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

def graph(event=None):
    tmptext = entry.get()
    tmptext = "$"+tmptext+"$"

    ax.clear()
    ax.text(0.2, 0.6, tmptext, fontsize=50)  
    canvas.draw()

root = tk.Tk()

mainframe = tk.Frame(root)
mainframe.pack()

entry = tk.Entry(mainframe, width=70)
entry.pack()

label = tk.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=True)
canvas._tkcanvas.pack(side="top", fill="both", expand=True)

ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

entry.insert(0, r"\sum_{i=0}^\infty x_i \frac{y_i}{z_i}")
graph()

root.bind("<Return>", graph)
root.mainloop()

code should produce a window such as one bellow:

enter image description here

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.

like image 58
ljetibo Avatar answered Sep 28 '22 23:09

ljetibo