Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Qt and LaTeX via KLFBackend
Latex rendering via KLFBackend
Render LaTeX code via KLFBackend library
23/5/2014 Update: TinyTex’s Qt 5 port of KLFBackend has been updated to 3.2.8 [bitbucket.org] (fixes a bug with ghostscript) thanks to retux
What is KLFBackend?
KLFBackend is a C++ library originally developed by Philippe Faist to render LaTeX code in KLatexFormula:
http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/
Getting it
The source code of KLFBackend is inside KLatexFormula from here http://klatexformula.sourceforge.net/download/ (as of this writing the current stable source version is 3.2.6). However, the library only works with Qt 4. I have a modified version of the library that allows it to support both Qt 4 and Qt 5 plus certain features which I’ll get to. For this tutorial to work you’ll need this modified version. Download it from here [bitbucket.org] . It comes with an simple example(TinyTex [bitbucket.org] ). Note that TinyTex only works with Qt 5. To make it work with Qt 4 you would need to rebuild it and change some includes.
Requirements
Library is tested to work on Windows 7 and Linux Mint 14 32bit, in Qt 4 and Qt 5. However, for windows users you can only use MinGW version of Qt. MSCV2010 compiler will produce an error should you try to build the library or run programs with it.
To get latex working: In linux you’ll nees to install texlive and ghostscript from synaptic package manager. In windows install Miktex [miktex.org] – just the basic Miktex would work.
Building the library
Open the KLFBackend pro file located in the latextest folder that you’ve downloaded and build the project. Now go to the “build-KLFBackend” directory where you have build your library and you should see KLFBackend.dll(windows) or libKLFBackend.so, libKLFBackend.so.1, libKLFBackend.so.1.0, libKLFBackend.so.1.0.0(linux). You can freely choose to discard the rest of the files cause they’re taking up space.
Using KLFBackend in your Qt app.
Open TinyTex.pro to access the example(alternatively you can refer to KLatexFormula’s source code as most of the code in this mini example originated from there).
in your pro file be sure you have
or it will output a bunch of warnings(though the app still runs happily). You will also need the following:
this will add KLFBackend library(LIB) when running app, and import all the headers(INCLUDEPATH) in KLFBackend into your project. Note that $$PWD refers to the project working directory.
mainwindow.h
KLFBackend::klfInput object stores input like preamble, mathmode, latex code and size. KLFBackend::klfSettings stores settings like the directories of your latex executable. the KLFPreviewBuilderThread object is used to render your equations by feeding it with KLFBackend::klfInput and KLFBackend::klfSettings. We will save the image as a QPixmap to the clipboard although it’s output a QImage, because for some reason copying the QImage of the equation gives really choppy results.
Below is the implementation file:
mainwindow.cpp
Although the LaTeX equation itself is
I have to write it as
because in C++ strings, the backslash is used to indicate some commands(e.g \n is newline). So \\ is read as \ by the program.
KLFBackend::detectSettings is a useful function to automatically search your the required executables. However should you need to set it manually you’ll do is as so:
note that to use QDir you need to include QDir in your implementation file.
Where to go from here:
the KLFBackend API documentation [klatexformula.sourceforge.net]
The development version of KLatexFormula adds extra features to KLFBackend. According to the author…
Performance wise the older version might be slightly better, if only because it’s more lightweight. The new version no longer depends on “epstopdf” for generating pdf, and uses directly ghostscript directly. It has much better detection of bounding boxes, can also generate SVG if the ghostscript driver is installed, and has a better interface for different fonts sizes and vector scaling. It stores also meta-information in PDF files too. You can also invoke user scripts that change the way the formula is rendered (for example for feynman diagrams with feynmf). Maybe more and I’m forgetting. So basically, it has more features, but if you were fine with 3.2, you can stick to that.
KLFBackend now needs some tools that are defined in klftools (e.g. data serializing, and utilities for the user scripts), and so depends on that library. And it wasn’t logical to have klftools depend on klfbackend.
Note that you will need to install cmake to configure it, and that it currently only works with Qt 4, and that I can’t get to build in windows.
…and there you have it! Qt and Latex! If you encounter any problems or have any questions/feedback, please add a comment at my forum post: http://forum.qt.io/viewthread/26657/