Qt and LaTeX via KLFBackend: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[:Category:Learning::Demos and Examples|Latex rendering via <span class="caps">KLFB</span>ackend]]
[[Category:Learning::Demos_and_Examples|Latex rendering via KLFBackend]]


=Render LaTeX code via <span class="caps">KLFB</span>ackend library=
= Render LaTeX code via KLFBackend library =


''23/5/2014 Update: TinyTex’s Qt 5 port of <span class="caps">KLFB</span>ackend has been [https://bitbucket.org/bruceoutdoors/tiny-tex/pull-request/1/updated-to-328/diff updated to 3.2.8] ''[bitbucket.org]'' (fixes a bug with ghostscript) thanks to retux''
''23/5/2014 Update: TinyTex's Qt 5 port of KLFBackend has been &quot;updated to 3.2.8&amp;quot;:https://bitbucket.org/bruceoutdoors/tiny-tex/pull-request/1/updated-to-328/diff (fixes a bug with ghostscript) thanks to retux''


==What is <span class="caps">KLFB</span>ackend?==
== What is KLFBackend? ==


<span class="caps">KLFB</span>ackend is a C++ library originally developed by Philippe Faist to render LaTeX code in KLatexFormula:<br />http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/
KLFBackend is a C++ library originally developed by Philippe Faist to render LaTeX code in KLatexFormula:<br />http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/


==Getting it==
== Getting it ==


[[Image:Program+Manager_2013-04-11_00-07-07.png|TinyTex]]
[[Image:https://lh4.googleusercontent.com/-1sMCCrbbQ7s/UWWOeqssR4I/AAAAAAAABPA/2yzn2O3CMhU/s750/Program+Manager_2013-04-11_00-07-07.png|TinyTex]]


The source code of <span class="caps">KLFB</span>ackend 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. [https://bitbucket.org/bruceoutdoors/tiny-tex/get/master.zip Download it from here] ''[bitbucket.org]'' . It comes with an simple example([https://bitbucket.org/bruceoutdoors/tiny-tex 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.
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. &quot;Download it from here&amp;quot;:https://bitbucket.org/bruceoutdoors/tiny-tex/get/master.zip . It comes with an simple example(&quot;TinyTex&amp;quot;:https://bitbucket.org/bruceoutdoors/tiny-tex ). 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==
== 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.
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 [http://miktex.org/download Miktex] ''[miktex.org]'' – just the basic Miktex would work.
To get latex working: In linux you'll nees to install texlive and ghostscript from synaptic package manager. In windows install &quot;Miktex&amp;quot;:http://miktex.org/download - just the basic Miktex would work.


==Building the library==
== Building the library ==


Open the <span class="caps">KLFB</span>ackend pro file located in the latextest folder that you’ve downloaded and build the project. Now go to the “build-<span class="caps">KLFB</span>ackend” directory where you have build your library and you should see <span class="caps">KLFB</span>ackend.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.
Open the KLFBackend pro file located in the latextest folder that you've downloaded and build the project. Now go to the &quot;build-KLFBackend&amp;quot; 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 <span class="caps">KLFB</span>ackend in your Qt app.==
== 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).
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 <br />
in your pro file be sure you have<br /><code><br />DEFINES ''= KLF_SRC_BUILD<br /></code>
<br />or it will output a bunch of warnings(though the app still runs happily). You will also need the following:<br /><code><br /># note that in unix(linux) systems library names are case sensitive<br />win32:CONFIG (release, debug|release): LIBS''= -L$$PWD/build-KLFBackend/ -lklfbackend<br />else:win32:CONFIG (debug, debug|release): LIBS ''= -L$$PWD/build-KLFBackend/ <s>lklfbackend<br />else:unix: LIBS ''= -L$$PWD/build-KLFBackend/ -lKLFBackend
<br />INCLUDEPATH''= $$PWD/klfbackend<br /></code>
<br />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.
<br />'''mainwindow.h'''<br /><code><br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
<br />#include &lt;QMainWindow&amp;gt;<br />#include &lt;QClipboard&amp;gt;<br />#include &quot;klfbackend.h&amp;quot;<br />#include &quot;klfpreviewbuilderthread.h&amp;quot;
<br />namespace Ui {<br />class MainWindow;<br />}
<br />class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT
<br />public:<br /> explicit MainWindow(QWidget *parent = 0);<br /> ~MainWindow();
<br />private slots:<br /> void updatePreviewBuilderThreadInput();<br /> void showRealTimePreview(const QImage&amp;amp; preview, bool latexerror);<br /> void copyToClipboard();
<br />private:<br /> Ui::MainWindow *ui;<br /> KLFPreviewBuilderThread *mPreviewBuilderThread;<br /> KLFBackend::klfInput input;<br /> KLFBackend::klfSettings settings;<br /> KLFBackend::klfOutput output;<br /> QClipboard *clipboard;<br /> QPixmap pixmap;
<br />};
<br />#endif // MAINWINDOW_H<br /></code>
<br />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.
<br />Below is the implementation file:
<br />'''mainwindow.cpp'''<br /><code><br />#include &quot;mainwindow.h&amp;quot;<br />#include &quot;ui_mainwindow.h&amp;quot;<br />#include &quot;klfbackend.h&amp;quot;<br />#include &lt;QDebug&amp;gt;


or it will output a bunch of warnings(though the app still runs happily). You will also need the following:<br />
<br />MainWindow::MainWindow(QWidget *parent) :
<br /> QMainWindow(parent),<br /> ui(new Ui::MainWindow)<br />{
<br /> ui</s>&gt;setupUi(this);<br /> input.mathmode = &quot;&quot;;<br /> input.dpi = 200;<br /> ui-&gt;label-&gt;setMinimumHeight(input.dpi/2);
<br />// input.bg_color = qRgba(225, 225, 225, 225);<br /> input.preamble = QString(&quot;usepackage{amssymb,amsmath,mathrsfs}&quot;);<br /> if(!KLFBackend::detectSettings(&amp;settings)) {<br /> qDebug() &lt;&lt; &quot;unable to find LaTeX in default directories.&quot;;<br /> } else {<br /> qDebug() &lt;&lt; &quot;default settings working!&quot;;<br /> }
<br /> // setup variables:<br /> mPreviewBuilderThread = new KLFPreviewBuilderThread(this, input, settings);
<br /> // make connections<br /> connect(ui-&gt;plainTextEdit, SIGNAL (textChanged()), this,<br /> SLOT (updatePreviewBuilderThreadInput()), Qt::QueuedConnection);<br /> connect(mPreviewBuilderThread, SIGNAL (previewAvailable(const QImage&amp;amp;, bool)),<br /> this, SLOT (showRealTimePreview(const QImage&amp;amp;, bool)), Qt::QueuedConnection);<br /> connect(ui-&gt;clipBtn, SIGNAL (clicked()), this, SLOT (copyToClipboard()));
<br /> ui-&gt;plainTextEdit-&gt;setFocus();<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Waiting for input…&quot;);<br />}
<br />void MainWindow::copyToClipboard()<br />{<br /> clipboard-&gt;setPixmap(pixmap);<br /> ui-&gt;statusBar-&gt;showMessage(&quot;your image has been copied to clipboard&amp;quot;);
<br />}
<br />MainWindow::~MainWindow()<br />{<br /> delete mPreviewBuilderThread;<br /> delete ui;<br />}
<br />void MainWindow::updatePreviewBuilderThreadInput()<br />{<br /> // in linux, I need to reinstate the preamble when rendering. No idea why.<br /> input.preamble = QString(&quot;usepackage{amssymb,amsmath}&quot;);<br /> input.latex = ui-&gt;plainTextEdit-&gt;toPlainText();<br /> if(mPreviewBuilderThread-&gt;inputChanged(input)) {<br /> qDebug() &lt;&lt; &quot;input changed. Render…&quot;;<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Input changed. Render…&quot;);<br /> mPreviewBuilderThread-&gt;start();<br /> }<br />}
<br />void MainWindow::showRealTimePreview(const QImage&amp;amp; preview, bool latexerror)<br />{<br /> if (latexerror) {<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Unable to render your equation. Please double check.&quot;);<br /> } else {<br /> ui-&gt;statusBar-&gt;showMessage(&quot;render is succesful[[Image:|Image:]] :D&amp;quot;);<br /> pixmap = QPixmap::fromImage(preview);<br /> ui-&gt;label-&gt;setPixmap(pixmap);<br /> ui-&gt;label-&gt;adjustSize();<br /> }<br />}<br /></code>
<br />Although the LaTeX equation itself is<br /><code><br />{amssymb,amsmath,mathrsfs}<br /></code><br />I have to write it as<br /><code><br />usepackage{amssymb,amsmath,mathrsfs}<br /></code><br />because in C''+ strings, the backslash is used to indicate some commands(e.g is newline). So is read as by the program.


this will add <span class="caps">KLFB</span>ackend library(<span class="caps">LIB</span>) when running app, and import all the headers(<span class="caps">INCLUDEPATH</span>) in <span class="caps">KLFB</span>ackend into your project. Note that $$PWD refers to the project working directory.
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:


'''mainwindow.h'''<br />
<code><br />#if defined(Q_OS_WIN)<br /> settings.tempdir = QDir::tempPath();<br /> settings.dvipsexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/dvips.exe&amp;quot;;<br /> settings.gsexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/mgs.exe&amp;quot;;<br /> settings.latexexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/latex.exe&amp;quot;;<br />#elif defined(Q_OS_LINUX)<br /> settings.tempdir = QDir::tempPath();<br /> settings.dvipsexec = &quot;/usr/bin/dvips&amp;quot;;<br /> settings.gsexec = &quot;/usr/bin/gs&amp;quot;;<br /> settings.latexexec = &quot;/usr/bin/latex&amp;quot;;<br />#endif<br /></code>
 
<span class="caps">KLFB</span>ackend::klfInput object stores input like preamble, mathmode, latex code and size. <span class="caps">KLFB</span>ackend::klfSettings stores settings like the directories of your latex executable. the KLFPreviewBuilderThread object is used to render your equations by feeding it with <span class="caps">KLFB</span>ackend::klfInput and <span class="caps">KLFB</span>ackend::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'''<br />
 
Although the LaTeX equation itself is<br /> I have to write it as<br /> because in C++ strings, the backslash is used to indicate some commands(e.g \n is newline). So \\ is read as \ by the program.
 
<span class="caps">KLFB</span>ackend::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.
note that to use QDir you need to include QDir in your implementation file.


==Where to go from here:==
== Where to go from here: ==
 
[http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/ the <span class="caps">KLFB</span>ackend <span class="caps">API</span> documentation] ''[klatexformula.sourceforge.net]''
 
The development version of KLatexFormula adds extra features to <span class="caps">KLFB</span>ackend. 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 <span class="caps">SVG</span> if the ghostscript driver is installed, and has a better interface for different fonts sizes and vector scaling. It stores also meta-information in <span class="caps">PDF</span> 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.''<br />''<span class="caps">KLFB</span>ackend 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.''
&quot;the KLFBackend API documentation&amp;quot;:http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/


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.
The development version of KLatexFormula adds extra features to KLFBackend. According to the author…


…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/
''Performance wise the older version might be slightly better, if only because it's more lightweight. The new version no longer depends on &quot;epstopdf&amp;quot; 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.''<br />''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.''


===Categories:===
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.


* [[:Category:Learning|Learning]]
<code><br /> &gt; svn checkout https://klatexformula.svn.sourceforge.net/svnroot/klatexformula/trunk/klatexformula<br /> &gt; cd klatexformula/<br /> &gt; mkdir build &amp;&amp; cd build/<br /> &gt; cmake .. -DKLF_BUILD_GUI=off -DKLF_LIBKLFTOOLS_STATIC=on -DKLF_LIBKLFBACKEND_STATIC=on  -DKLF_BUILD_KTEXTEDITORPLUGIN=off -DCMAKE_INSTALL_PREFIX=/usr/local<br /></code>
** [[:Category:Learning::Demos and Examples|Demos_and_Examples]]

Revision as of 08:58, 24 February 2015


Render LaTeX code via KLFBackend library

23/5/2014 Update: TinyTex's Qt 5 port of KLFBackend has been "updated to 3.2.8&quot;:https://bitbucket.org/bruceoutdoors/tiny-tex/pull-request/1/updated-to-328/diff (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

TinyTex

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&quot;:https://bitbucket.org/bruceoutdoors/tiny-tex/get/master.zip . It comes with an simple example("TinyTex&quot;:https://bitbucket.org/bruceoutdoors/tiny-tex ). 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&quot;:http://miktex.org/download - 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&quot; 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

<br />DEFINES ''= KLF_SRC_BUILD<br />


or it will output a bunch of warnings(though the app still runs happily). You will also need the following:

<br /># note that in unix(linux) systems library names are case sensitive<br />win32:CONFIG (release, debug|release): LIBS''= -L$$PWD/build-KLFBackend/ -lklfbackend<br />else:win32:CONFIG (debug, debug|release): LIBS ''= -L$$PWD/build-KLFBackend/ <s>lklfbackend<br />else:unix: LIBS ''= -L$$PWD/build-KLFBackend/ -lKLFBackend
<br />INCLUDEPATH''= $$PWD/klfbackend<br />


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

<br />#ifndef MAINWINDOW_H<br />#define MAINWINDOW_H
<br />#include &lt;QMainWindow&amp;gt;<br />#include &lt;QClipboard&amp;gt;<br />#include &quot;klfbackend.h&amp;quot;<br />#include &quot;klfpreviewbuilderthread.h&amp;quot;
<br />namespace Ui {<br />class MainWindow;<br />}
<br />class MainWindow : public QMainWindow<br />{<br /> Q_OBJECT
<br />public:<br /> explicit MainWindow(QWidget *parent = 0);<br /> ~MainWindow();
<br />private slots:<br /> void updatePreviewBuilderThreadInput();<br /> void showRealTimePreview(const QImage&amp;amp; preview, bool latexerror);<br /> void copyToClipboard();
<br />private:<br /> Ui::MainWindow *ui;<br /> KLFPreviewBuilderThread *mPreviewBuilderThread;<br /> KLFBackend::klfInput input;<br /> KLFBackend::klfSettings settings;<br /> KLFBackend::klfOutput output;<br /> QClipboard *clipboard;<br /> QPixmap pixmap;
<br />};
<br />#endif // MAINWINDOW_H<br />


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

<br />#include &quot;mainwindow.h&amp;quot;<br />#include &quot;ui_mainwindow.h&amp;quot;<br />#include &quot;klfbackend.h&amp;quot;<br />#include &lt;QDebug&amp;gt;

<br />MainWindow::MainWindow(QWidget *parent) :
<br /> QMainWindow(parent),<br /> ui(new Ui::MainWindow)<br />{
<br /> ui</s>&gt;setupUi(this);<br /> input.mathmode = &quot;&quot;;<br /> input.dpi = 200;<br /> ui-&gt;label-&gt;setMinimumHeight(input.dpi/2);
<br />// input.bg_color = qRgba(225, 225, 225, 225);<br /> input.preamble = QString(&quot;usepackage{amssymb,amsmath,mathrsfs}&quot;);<br /> if(!KLFBackend::detectSettings(&amp;settings)) {<br /> qDebug() &lt;&lt; &quot;unable to find LaTeX in default directories.&quot;;<br /> } else {<br /> qDebug() &lt;&lt; &quot;default settings working!&quot;;<br /> }
<br /> // setup variables:<br /> mPreviewBuilderThread = new KLFPreviewBuilderThread(this, input, settings);
<br /> // make connections<br /> connect(ui-&gt;plainTextEdit, SIGNAL (textChanged()), this,<br /> SLOT (updatePreviewBuilderThreadInput()), Qt::QueuedConnection);<br /> connect(mPreviewBuilderThread, SIGNAL (previewAvailable(const QImage&amp;amp;, bool)),<br /> this, SLOT (showRealTimePreview(const QImage&amp;amp;, bool)), Qt::QueuedConnection);<br /> connect(ui-&gt;clipBtn, SIGNAL (clicked()), this, SLOT (copyToClipboard()));
<br /> ui-&gt;plainTextEdit-&gt;setFocus();<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Waiting for input&quot;);<br />}
<br />void MainWindow::copyToClipboard()<br />{<br /> clipboard-&gt;setPixmap(pixmap);<br /> ui-&gt;statusBar-&gt;showMessage(&quot;your image has been copied to clipboard&amp;quot;);
<br />}
<br />MainWindow::~MainWindow()<br />{<br /> delete mPreviewBuilderThread;<br /> delete ui;<br />}
<br />void MainWindow::updatePreviewBuilderThreadInput()<br />{<br /> // in linux, I need to reinstate the preamble when rendering. No idea why.<br /> input.preamble = QString(&quot;usepackage{amssymb,amsmath}&quot;);<br /> input.latex = ui-&gt;plainTextEdit-&gt;toPlainText();<br /> if(mPreviewBuilderThread-&gt;inputChanged(input)) {<br /> qDebug() &lt;&lt; &quot;input changed. Render…&quot;;<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Input changed. Render…&quot;);<br /> mPreviewBuilderThread-&gt;start();<br /> }<br />}
<br />void MainWindow::showRealTimePreview(const QImage&amp;amp; preview, bool latexerror)<br />{<br /> if (latexerror) {<br /> ui-&gt;statusBar-&gt;showMessage(&quot;Unable to render your equation. Please double check.&quot;);<br /> } else {<br /> ui-&gt;statusBar-&gt;showMessage(&quot;render is succesful[[Image:|Image:]] :D&amp;quot;);<br /> pixmap = QPixmap::fromImage(preview);<br /> ui-&gt;label-&gt;setPixmap(pixmap);<br /> ui-&gt;label-&gt;adjustSize();<br /> }<br />}<br />


Although the LaTeX equation itself is

<br />{amssymb,amsmath,mathrsfs}<br />


I have to write it as

<br />usepackage{amssymb,amsmath,mathrsfs}<br />


because in C+ strings, the backslash is used to indicate some commands(e.g 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:

<br />#if defined(Q_OS_WIN)<br /> settings.tempdir = QDir::tempPath();<br /> settings.dvipsexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/dvips.exe&amp;quot;;<br /> settings.gsexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/mgs.exe&amp;quot;;<br /> settings.latexexec = &quot;D:/Program Files/MiKTeX 2.9/miktex/bin/latex.exe&amp;quot;;<br />#elif defined(Q_OS_LINUX)<br /> settings.tempdir = QDir::tempPath();<br /> settings.dvipsexec = &quot;/usr/bin/dvips&amp;quot;;<br /> settings.gsexec = &quot;/usr/bin/gs&amp;quot;;<br /> settings.latexexec = &quot;/usr/bin/latex&amp;quot;;<br />#endif<br />

note that to use QDir you need to include QDir in your implementation file.

Where to go from here:

"the KLFBackend API documentation&quot;:http://klatexformula.sourceforge.net/doc/apidoc/klfbackend/html/

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&quot; 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.

<br /> &gt; svn checkout https://klatexformula.svn.sourceforge.net/svnroot/klatexformula/trunk/klatexformula<br /> &gt; cd klatexformula/<br /> &gt; mkdir build &amp;&amp; cd build/<br /> &gt; cmake .. -DKLF_BUILD_GUI=off -DKLF_LIBKLFTOOLS_STATIC=on -DKLF_LIBKLFBACKEND_STATIC=on  -DKLF_BUILD_KTEXTEDITORPLUGIN=off -DCMAKE_INSTALL_PREFIX=/usr/local<br />