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..."));
55 m_label->setToolTip(tex);
58 if (tex.startsWith(QLatin1String(
"$$"))) {
59 tex.replace(0,2,
"\\begin{eqnarray*}").remove(-2,2).append(
"\\end{eqnarray*}");
61 tex.remove(0,2).remove(-2,2);
63 kDebug()<<
"rendering as latex";
65 QString dir=KGlobal::dirs()->saveLocation(
"tmp",
"parley/");
68 KTemporaryFile *texFile=
new KTemporaryFile();
69 texFile->setPrefix(
"parley/" );
70 texFile->setSuffix(
".tex" );
73 QColor color = m_label->palette().color(QPalette::WindowText);
74 QString colorString = QString::number(color.redF()) +
','
75 + QString::number(color.greenF()) +
','
76 + QString::number(color.blueF());
77 QString expressionTex = QString(
texTemplate).arg(colorString, tex.trimmed());
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() ) );
98 return tex.length() > 4 && tex.mid(2, tex.length()-4).simplified().length() > 0 &&
99 ((tex.startsWith(QLatin1String(
"$$")) && tex.endsWith(QLatin1String(
"$$"))) ||
100 (tex.startsWith(QString::fromUtf8(
"§§")) && tex.endsWith(QString::fromUtf8(
"§§"))));
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");
137 if(QFileInfo(pngFile).exists())
139 QPixmap pixmap(pngFile);
140 m_label->setPixmap(pixmap);
141 m_label->setMinimumSize(pixmap.size().boundedTo(QSize(600, 300)));
143 m_label->setText(i18n(
"LaTeX error."));
147 QString dir=KGlobal::dirs()->saveLocation(
"tmp",
"parley/");
148 QStringList extensions;
149 extensions<<
".log"<<
".aux"<<
".tex"<<
".dvi"<<
".eps"<<
".png";
150 foreach(
const QString& ext, extensions)
152 QString s=m_latexFilename;
153 s.replace(
".eps", ext);
159 #include "latexrenderer.moc"
LatexRenderer(QObject *parent=0)
void renderLatex(QString tex)
static bool isLatex(const QString &tex)