QtMathJax is a .pri
file that lets you easily add beautiful
math typesetting into any Qt application.
MathJax is the de facto standard for mathematical typesetting on
the web, using a JavaScript-based typesetting engine.
QtMathJax imports just the necessary parts of that project into
your Qt project, runs them in a hidden QWebView
, and
provides you a one-function API for turning a string of TeX code
into a string of SVG code that can be used to render it anywhere,
in your Qt app or outside it.
Just clone this git repo, or use it as a submodule in your project. In fact, it uses MathJax as a submodule itself.
To use QtMathJax, follow these steps.
- In your
.pro
file, import QtMathJax by adding the following line.include(/path/to/qtmathjax.pri)
- In the source file where you need to use it, include the one
class you need,
TeXEngine
, via#include "texengine.h"
. - Create an instance of the class and call the
TeX2SVG()
method in it.
TeXEngine engine;
// ...
QString svgCode = engine.TeX2SVG( "ax^2+bx+c=0" );
That's it! You can then render that SVG in any of the ways Qt provides, or do something else with it, such as saving it to a file for use outside your app. Example:
myQSvgWidget->load( svgCode.toUtf8() );
See below for an example app.
There is also an API for asynchronous computation, not yet
thoroughly documented here. One calls engine.asyncTeX2SVG()
to enqueue an expression for typesetting, then later checks back
using engine.hasComputed()
to see if the typesetting is
complete. If so, then a call to engine.TeX2SVG()
will not
spin the event loop at all, but return immediately with a value
stored in cache from the asynchronous computation. See the source
for details.
The repository comes with a sample app in the example subfolder. If using Qt 5, you should be able to compile and run that app without any changes needed. It looks like this:
This project is released under the GPLv3 and LGPLv3. It is my understanding that these are both compatible with the Apache license under which MathJax is released.