19 #include <ktemporaryfile.h>
20 #include <kstandarddirs.h>
29 using namespace Practice;
31 const char*
texTemplate =
"\\documentclass[12pt,fleqn]{article} \n "\
32 "\\usepackage{latexsym,amsfonts,amssymb,ulem} \n "\
33 "\\usepackage[dvips]{graphicx} \n "\
34 "\\setlength\\textwidth{5in} \n "\
35 "\\setlength{\\parindent}{0pt} \n "\
36 "\\usepackage{amsmath} \n "\
37 "\\usepackage{color} \n "\
38 "\\pagestyle{empty} \n "\
39 "\\begin{document} \n "\
40 "{\\definecolor{mycolor}{rgb}{%1} \n "\
41 "{\\color{mycolor} \n "\
54 m_label->
setText(i18n(
"Rendering..."));
63 kDebug() <<
"rendering as latex";
65 QString dir = KGlobal::dirs()->saveLocation(
"tmp",
"parley/");
69 texFile->setPrefix(
"parley/");
70 texFile->setSuffix(
".tex");
73 QColor color = m_label->
palette().color(QPalette::WindowText);
79 texFile->write(expressionTex.toUtf8());
82 QString fileName = texFile->fileName();
83 kDebug() <<
"fileName: " << fileName;
84 m_latexFilename = fileName;
85 m_latexFilename.
replace(
".tex",
".eps");
86 KProcess *p =
new KProcess(
this);
87 p->setWorkingDirectory(dir);
89 (*p) <<
"latex" <<
"-interaction=batchmode" <<
"-halt-on-error" << fileName;
91 connect(p, SIGNAL(finished(
int, QProcess::ExitStatus)),
this, SLOT(convertToPs()));
92 connect(p, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(latexRendered()));
103 void LatexRenderer::convertToPs()
105 kDebug() <<
"converting to ps";
106 QString dviFile = m_latexFilename;
107 dviFile.
replace(
".eps",
".dvi");
108 KProcess *p =
new KProcess(
this);
109 kDebug() <<
"running: " <<
"dvips" <<
"-E" <<
"-o" << m_latexFilename << dviFile;
110 (*p) <<
"dvips" <<
"-E" <<
"-o" << m_latexFilename << dviFile;
112 connect(p, SIGNAL(finished(
int, QProcess::ExitStatus)),
this, SLOT(convertToImage()));
113 connect(p, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(latexRendered()));
117 void LatexRenderer::convertToImage()
119 kDebug() <<
"converting to ps";
120 QString pngFile = m_latexFilename;
121 pngFile.
replace(
".eps",
".png");
122 KProcess *p =
new KProcess(
this);
123 kDebug() <<
"running:" <<
"convert" << m_latexFilename << pngFile;
124 (*p) <<
"convert" <<
"-density" <<
"85" << m_latexFilename << pngFile;
126 connect(p, SIGNAL(finished(
int, QProcess::ExitStatus)),
this, SLOT(latexRendered()));
127 connect(p, SIGNAL(error(QProcess::ProcessError)),
this, SLOT(latexRendered()));
131 void LatexRenderer::latexRendered()
133 kDebug() <<
"rendered file " << m_latexFilename;
135 QString pngFile = m_latexFilename;
136 pngFile.
replace(
".eps",
".png");
142 m_label->
setText(i18n(
"LaTeX error."));
146 QString dir = KGlobal::dirs()->saveLocation(
"tmp",
"parley/");
148 extensions <<
".log" <<
".aux" <<
".tex" <<
".dvi" <<
".eps" <<
".png";
149 foreach(
const QString & ext, extensions) {
157 #include "latexrenderer.moc"
QString & append(QChar ch)
void setPixmap(const QPixmap &)
QString & remove(int position, int n)
QString number(int n, int base)
QString fromUtf8(const char *str, int size)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setText(const QString &)
QString & replace(int position, int n, QChar after)
QString mid(int position, int n) const
LatexRenderer(QObject *parent=0)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
void renderLatex(QString tex)
static bool isLatex(const QString &tex)