Qt and LaTeX via KLFBackend: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
= Render LaTeX code via KLFBackend library =
= Render LaTeX code via KLFBackend library =


''23/5/2014 Update: TinyTex's Qt 5 port of KLFBackend has been "updated to 3.2.8":https://bitbucket.org/bruceoutdoors/tiny-tex/pull-request/1/updated-to-328/diff (fixes a bug with ghostscript) thanks to retux''
''23/5/2014 Update: TinyTex's Qt 5 port of KLFBackend has been "updated to 3.2.8":https://bitbucket.org/bruceoutdoors/tiny-tex/pull-request/1/updated-to-328/diff (fixes a bug with ghostscript) thanks to retux''


== What is KLFBackend? ==
== What is KLFBackend? ==


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/
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 ==
== Getting it ==
Line 13: Line 14:
[[Image:https://lh4.googleusercontent.com/-1sMCCrbbQ7s/UWWOeqssR4I/AAAAAAAABPA/2yzn2O3CMhU/s750/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 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.
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":https://bitbucket.org/bruceoutdoors/tiny-tex/get/master.zip . It comes with an simple example("TinyTex":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 ==
Line 19: Line 20:
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 &quot;Miktex&amp;quot;:http://miktex.org/download - 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 "Miktex":http://miktex.org/download - just the basic Miktex would work.


== Building the library ==
== 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 &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.
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. ==
== Using KLFBackend in your Qt app. ==
Line 29: Line 30:
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 /><code><br />DEFINES ''= KLF_SRC_BUILD<br /></code>
in your pro file be sure you have
<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
<code>
<br />INCLUDEPATH''= $$PWD/klfbackend<br /></code>
DEFINES ''= KLF_SRC_BUILD
<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.
</code>
<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;


<br />MainWindow::MainWindow(QWidget *parent) :
or it will output a bunch of warnings(though the app still runs happily). You will also need the following:
<br /> QMainWindow(parent),<br /> ui(new Ui::MainWindow)<br />{
<code>
<br /> ui</s>&gt;setupUi(this);<br /> input.mathmode = &quot;&quot;;<br /> input.dpi = 200;<br /> ui-&gt;label-&gt;setMinimumHeight(input.dpi/2);
# note that in unix(linux) systems library names are case sensitive
<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 /> }
win32:CONFIG (release, debug|release): LIBS''= -L$$PWD/build-KLFBackend/ -lklfbackend
<br /> // setup variables:<br /> mPreviewBuilderThread = new KLFPreviewBuilderThread(this, input, settings);
else:win32:CONFIG (debug, debug|release): LIBS ''= -L$$PWD/build-KLFBackend/ -lklfbackend
<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()));
else:unix: LIBS ''= -L$$PWD/build-KLFBackend/ -lKLFBackend
<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;);
INCLUDEPATH''= $$PWD/klfbackend
<br />}
</code>
<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 />}
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 />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.
'''mainwindow.h'''
<code>
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QClipboard>
#include "klfbackend.h"
#include "klfpreviewbuilderthread.h"
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
Q_OBJECT
 
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
 
private slots:
void updatePreviewBuilderThreadInput();
void showRealTimePreview(const QImage&amp;amp; preview, bool latexerror);
void copyToClipboard();
 
private:
Ui::MainWindow *ui;
KLFPreviewBuilderThread *mPreviewBuilderThread;
KLFBackend::klfInput input;
KLFBackend::klfSettings settings;
KLFBackend::klfOutput output;
QClipboard *clipboard;
QPixmap pixmap;
 
};
 
#endif // MAINWINDOW_H
</code>
 
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'''
<code>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "klfbackend.h"
#include <QDebug>
 
 
MainWindow::MainWindow(QWidget *parent) :
 
QMainWindow(parent),
ui(new Ui::MainWindow)
{
 
ui->setupUi(this);
input.mathmode = "";
input.dpi = 200;
ui->label->setMinimumHeight(input.dpi/2);
 
// input.bg_color = qRgba(225, 225, 225, 225);
input.preamble = QString("usepackage{amssymb,amsmath,mathrsfs}");
if(!KLFBackend::detectSettings(&amp;settings)) {
qDebug() << "unable to find LaTeX in default directories.";
} else {
qDebug() << "default settings working!";
}
 
// setup variables:
mPreviewBuilderThread = new KLFPreviewBuilderThread(this, input, settings);
 
// make connections
connect(ui->plainTextEdit, SIGNAL (textChanged()), this,
SLOT (updatePreviewBuilderThreadInput()), Qt::QueuedConnection);
connect(mPreviewBuilderThread, SIGNAL (previewAvailable(const QImage&amp;amp;, bool)),
this, SLOT (showRealTimePreview(const QImage&amp;amp;, bool)), Qt::QueuedConnection);
connect(ui->clipBtn, SIGNAL (clicked()), this, SLOT (copyToClipboard()));
 
ui->plainTextEdit->setFocus();
ui->statusBar->showMessage("Waiting for input…");
}
 
void MainWindow::copyToClipboard()
{
clipboard->setPixmap(pixmap);
ui->statusBar->showMessage("your image has been copied to clipboard");
 
}
 
MainWindow::~MainWindow()
{
delete mPreviewBuilderThread;
delete ui;
}
 
void MainWindow::updatePreviewBuilderThreadInput()
{
// in linux, I need to reinstate the preamble when rendering. No idea why.
input.preamble = QString("usepackage{amssymb,amsmath}");
input.latex = ui->plainTextEdit->toPlainText();
if(mPreviewBuilderThread->inputChanged(input)) {
qDebug() << "input changed. Render…";
ui->statusBar->showMessage("Input changed. Render…");
mPreviewBuilderThread->start();
}
}
 
void MainWindow::showRealTimePreview(const QImage&amp;amp; preview, bool latexerror)
{
if (latexerror) {
ui->statusBar->showMessage("Unable to render your equation. Please double check.");
} else {
ui->statusBar->showMessage("render is succesful[[Image:|Image:]] :D");
pixmap = QPixmap::fromImage(preview);
ui->label->setPixmap(pixmap);
ui->label->adjustSize();
}
}
</code>
 
Although the LaTeX equation itself is
<code>
{amssymb,amsmath,mathrsfs}
</code>
I have to write it as
<code>
usepackage{amssymb,amsmath,mathrsfs}
</code>
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:
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:


<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>
<code>
#if defined(Q_OS_WIN)
settings.tempdir = QDir::tempPath();
settings.dvipsexec = "D:/Program Files/MiKTeX 2.9/miktex/bin/dvips.exe";
settings.gsexec = "D:/Program Files/MiKTeX 2.9/miktex/bin/mgs.exe";
settings.latexexec = "D:/Program Files/MiKTeX 2.9/miktex/bin/latex.exe";
#elif defined(Q_OS_LINUX)
settings.tempdir = QDir::tempPath();
settings.dvipsexec = "/usr/bin/dvips";
settings.gsexec = "/usr/bin/gs";
settings.latexexec = "/usr/bin/latex";
#endif
</code>


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.
Line 68: Line 201:
== Where to go from here: ==
== Where to go from here: ==


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


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


<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>
<code>
> svn checkout https://klatexformula.svn.sourceforge.net/svnroot/klatexformula/trunk/klatexformula
> cd klatexformula/
> mkdir build &amp;&amp; cd build/
> cmake .. -DKLF_BUILD_GUI=off -DKLF_LIBKLFTOOLS_STATIC=on -DKLF_LIBKLFBACKEND_STATIC=on  -DKLF_BUILD_KTEXTEDITORPLUGIN=off -DCMAKE_INSTALL_PREFIX=/usr/local
</code>

Revision as of 09:28, 25 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":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":https://bitbucket.org/bruceoutdoors/tiny-tex/get/master.zip . It comes with an simple example("TinyTex":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":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" 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

DEFINES ''= KLF_SRC_BUILD

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

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

INCLUDEPATH''= $$PWD/klfbackend

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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QClipboard>
#include "klfbackend.h"
#include "klfpreviewbuilderthread.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
 Q_OBJECT

public:
 explicit MainWindow(QWidget *parent = 0);
 ~MainWindow();

private slots:
 void updatePreviewBuilderThreadInput();
 void showRealTimePreview(const QImage&amp;amp; preview, bool latexerror);
 void copyToClipboard();

private:
 Ui::MainWindow *ui;
 KLFPreviewBuilderThread *mPreviewBuilderThread;
 KLFBackend::klfInput input;
 KLFBackend::klfSettings settings;
 KLFBackend::klfOutput output;
 QClipboard *clipboard;
 QPixmap pixmap;

};

#endif // 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

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "klfbackend.h"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent) :

 QMainWindow(parent),
 ui(new Ui::MainWindow)
{

 ui->setupUi(this);
 input.mathmode = "";
 input.dpi = 200;
 ui->label->setMinimumHeight(input.dpi/2);

// input.bg_color = qRgba(225, 225, 225, 225);
 input.preamble = QString("usepackage{amssymb,amsmath,mathrsfs}");
 if(!KLFBackend::detectSettings(&amp;settings)) {
 qDebug() << "unable to find LaTeX in default directories.";
 } else {
 qDebug() << "default settings working!";
 }

 // setup variables:
 mPreviewBuilderThread = new KLFPreviewBuilderThread(this, input, settings);

 // make connections
 connect(ui->plainTextEdit, SIGNAL (textChanged()), this,
 SLOT (updatePreviewBuilderThreadInput()), Qt::QueuedConnection);
 connect(mPreviewBuilderThread, SIGNAL (previewAvailable(const QImage&amp;amp;, bool)),
 this, SLOT (showRealTimePreview(const QImage&amp;amp;, bool)), Qt::QueuedConnection);
 connect(ui->clipBtn, SIGNAL (clicked()), this, SLOT (copyToClipboard()));

 ui->plainTextEdit->setFocus();
 ui->statusBar->showMessage("Waiting for input…");
}

void MainWindow::copyToClipboard()
{
 clipboard->setPixmap(pixmap);
 ui->statusBar->showMessage("your image has been copied to clipboard");

}

MainWindow::~MainWindow()
{
 delete mPreviewBuilderThread;
 delete ui;
}

void MainWindow::updatePreviewBuilderThreadInput()
{
 // in linux, I need to reinstate the preamble when rendering. No idea why.
 input.preamble = QString("usepackage{amssymb,amsmath}");
 input.latex = ui->plainTextEdit->toPlainText();
 if(mPreviewBuilderThread->inputChanged(input)) {
 qDebug() << "input changed. Render…";
 ui->statusBar->showMessage("Input changed. Render…");
 mPreviewBuilderThread->start();
 }
}

void MainWindow::showRealTimePreview(const QImage&amp;amp; preview, bool latexerror)
{
 if (latexerror) {
 ui->statusBar->showMessage("Unable to render your equation. Please double check.");
 } else {
 ui->statusBar->showMessage("render is succesful[[Image:|Image:]] :D");
 pixmap = QPixmap::fromImage(preview);
 ui->label->setPixmap(pixmap);
 ui->label->adjustSize();
 }
}

Although the LaTeX equation itself is

{amssymb,amsmath,mathrsfs}

I have to write it as

usepackage{amssymb,amsmath,mathrsfs}

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:

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

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

Where to go from here:

"the KLFBackend API documentation":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" 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.

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