Geddes
Geddes

Reputation: 1173

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

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

Upvotes: 6

Views: 2458

Answers (3)

Daniel Donnelly
Daniel Donnelly

Reputation: 610

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.

Upvotes: 1

Boyi
Boyi

Reputation: 94

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.

Upvotes: 5

Mad Scientist
Mad Scientist

Reputation: 18551

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

Upvotes: 3

Related Questions