Is it possible for SymPy to render LaTeX for use in a GUI?

Question:

I am hoping to use PyQt to produce an application that will display an equation entered by the user. I had considered matplotlib, but this seems like overkill as I would only be using it to render the latex.

I need to use SymPy anyway, so I was hoping there would be a way to use it to do the rendering also, preferably producing an SVG file for PyQt to use.

I hope this makes sense – many thanks in advance for any help/advice.

Best wishes,
Geddes

Asked By: Geddes

||

Answers:

You could output MathML using SymPy and then use SVGMath to convert it to SVG.

Answered By: Mad Scientist

If you just need simple rendering using python, how about trying ipython 0.11 with qtconsole using sympy profile(ipython qtconsole –profile=sympy). It did implement matplotlib, but I consider it quite clean considering mathematical expressions can be rendered directly on the qtconsole. Need to install pyzmq module.

Answered By: Boyi

You can take the markdown editor example in the Qt library and link in KaTeX to your HTML template. It will render math, really fast! You can then take a screencap of your QWebView widget, converting it to SVG or PNG.

I still haven’t figured out how to get KaTeX to render to an SVG file. Perhaps you can just grab the rendered HTML and it’s already formatted. IDK. Comment here or in the below GitHub link’s issues forum for additional help.

https://github.com/enjoysmath/Marked-KaTeX-example-cpp

It even let’s you edit the Markdown / KaTeX content! What they did was use something called QWebChannel. IDK how it works or why it’s needed, but it’s the way to do things if you’re wanting to communicate between a web browser and your Qt app.

You need to have QWebEngineWidgets / Qt Positioning / Any web-related libraries installed using your Qt Maintenance tool.

This example is definitely convertible to PyQt5, but I haven’t tried it yet. It wouldn’t be too hard to convert, because the JavaScript file qwebchannel.js stays exactly the same.

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.