I'm doing a presentation system in QML (with C++ backend) similar to this one.
I want to be able to include some Latex code to display some math (only minimum Latex capabilities needed, such as sums, fractions, super- and substript and math fonts).
I see the following possibilities to do so:
- Using latex2e:
- Compile the Latex code to a DVI document (with
latex
) - Convert the DVI to SVG (with
dvisvgm
) - Display this using an
Image
in QML (which also supports SVG)
- Compile the Latex code to a DVI document (with
- Using MathJax:
- Wrap the Latex code in a small HTML file + include the MathJax JS library
- Display this using
WebKit
for QML
- Custom solution (C++):
- Write or include an existing minimal Latex math parser
- Write a
QDeclarativeItem
with custom painting
Is there another solution I don't see? I prefer the first solution as the second sounds too heavy and the third too hard.
I see the following problem with the baseline of the math formula: I want to be able to include inline formulas (within a paragraph). Currently, text paragraphs are renderd using a QML Text element. Is there a possibility to include an image in a Text element (using HTML img element maybe) with a specified baseline? Also, how can I determine the baseline of a rendered Latex document?
Note: The time to pre-render Latex code isn't problematic. I'm going to cache already rendered formulas, so it should be no problem to invoke latex
+ dvisvgm
on every single formula contained in the presentation document.
Also note that I use a preprocessor (I don't write the QML code by hand but generate it). So I don't need a QML element for the Latex code but I can just generate a QML code snippet with an Image element (for the first solution). This should simplify embedding the rendered image.
I also don't need to support wrapping a formula when using inline Latex. (Latex does so when writing inline formulas, but I only use small formulas I don't want to be wrapped anyways.)
I think those are three good options, and I can't think of any other way to do it.
It sounds like you have a requirement to have the formulas in-line with some textual paragraphs, so for that reason I think option 2 is a good way to do it.
It isn't difficult to use the WebKit component. I'm not sure what you mean by "too heavy".
Cheers!
Another option is to use a rich text QML component which also supports images (e.g. TextEdit) rather than Image - then you can place them inline with normal text.