summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9d30628)
raw | patch | inline | side by side (parent: 9d30628)
author | sgimenez <sgimenez@users.sourceforge.net> | |
Fri, 24 Nov 2006 15:02:55 +0000 (15:02 +0000) | ||
committer | sgimenez <sgimenez@users.sourceforge.net> | |
Fri, 24 Nov 2006 15:02:55 +0000 (15:02 +0000) |
with the eqtexsvg plugin (ie LaTeX Formula):
- uses better svg parsing technique
- does better tmp file creation
- uses plot-svg pstoedit driver to produce svgs
- does not use -output-dir with latex
- other minor fixes and improvements
thanks to Roman S Dubtsov
- uses better svg parsing technique
- does better tmp file creation
- uses plot-svg pstoedit driver to produce svgs
- does not use -output-dir with latex
- other minor fixes and improvements
thanks to Roman S Dubtsov
share/extensions/eqtexsvg.inx | patch | blob | history | |
share/extensions/eqtexsvg.py | patch | blob | history |
index 819f30aa03cced6b8c99e6dfa5c2c5a2144d484a..657594a9c8b6da09681482d5d0f883ab69a2a05b 100644 (file)
<dependency type="executable" location="path">latex</dependency>
<dependency type="executable" location="path">dvips</dependency>
<dependency type="executable" location="path">pstoedit</dependency>
- <param name="formule" type="string" _gui-text="LaTeX formula: ">\lim_{n \to \infty}\sum_{k=1}^n \frac{1}{k^2}= \frac{\pi^2}{6}</param>
+ <param name="formule" type="string" _gui-text="LaTeX formula: ">\[\frac{\pi^2}{6}=\lim_{n \to \infty}\sum_{k=1}^n \frac{1}{k^2}\]</param>
<effect>
<object-type>all</object-type>
<effects-menu>
index a16cdc62552bb5460b905303af6e31c3201fe20e..5b88febece6da33a89a25cc34e152bd150108f71 100644 (file)
#!/usr/bin/env python
# -*- coding: cp1252 -*-
"""
-EQTEXSVG.py
-functions for converting LATEX equation string into SVG path
-This extension need, to work properly :
- - a TEX/LATEX distribution (MiKTEX ...)
+eqtexsvg.py
+functions for converting LaTeX equation string into SVG path
+This extension need, to work properly:
+ - a TeX/LaTeX distribution (MiKTeX ...)
- pstoedit software: <http://www.pstoedit.net/pstoedit>
-Copyright (C) 2006 Julien Vitard, julienvitard@gmail.com
-
-- I will try to code XML parsing, not the hard way ;-)
+Copyright (C) 2006 Julien Vitard <julienvitard@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
-import inkex, os, tempfile, xml.dom.ext, xml.dom.ext.reader.Sax
-import sys
-from xml.dom import Node
+import inkex, os, tempfile, xml.dom.minidom
def create_equation_tex(filename, equation):
tex = open(filename, 'w')
- tex.write("""%% processed with EqTeXSVG.py
-\documentclass{article}
-\usepackage{amsmath}
+ tex.write("""%% processed with eqtexsvg.py
+\\documentclass{article}
+\\usepackage{amsmath}
+\\usepackage{amssymb}
+\\usepackage{amsfonts}
+
\\thispagestyle{empty}
\\begin{document}
""")
- tex.write("$$\n")
tex.write(equation)
- tex.write("\n$$\n")
- tex.write("\end{document}\n")
+ tex.write("\\end{document}\n")
tex.close()
-def add_paths(self,group,node):
- next = node.firstChild
- while next is not None:
- child = next
- next = child.nextSibling
-
- if child.nodeType is not Node.ELEMENT_NODE: continue
- if child.nodeName == "title": continue
- if child.nodeName == "desc": continue
- if child.nodeName == "rect" and child.getAttribute("id") == "background": continue
-
- if child.nodeName == "g":
- svg_group = self.document.createElement('svg:g')
- if child.hasAttribute("transform"):
- svg_group.setAttribute("transform",child.getAttribute("transform"))
- group.appendChild(svg_group)
- add_paths(self,svg_group,child)
- else:
- group.appendChild(self.document.importNode(child,1))
-
def svg_open(self,filename):
- # parsing of SVG file with the equation
- reader = xml.dom.ext.reader.Sax.Reader()
- stream = open(filename,'r')
- svgdoc = reader.fromStream(stream)
-
- group = self.document.createElement('svg:g')
+ doc_width = float(self.document.documentElement.getAttribute('width'))
+ doc_height = float(self.document.documentElement.getAttribute('height'))
+ doc_sizeH = min(doc_width,doc_height)
+ doc_sizeW = max(doc_width,doc_height)
+
+ def clone_and_rewrite(self, node_in):
+ if node_in.localName != 'svg':
+ node_out = self.document.createElement('svg:' + node_in.localName)
+ for i in range(0, node_in.attributes.length):
+ name = node_in.attributes.item(i).name
+ value = node_in.attributes.item(i).value
+ node_out.setAttribute(name, value)
+ else:
+ node_out = self.document.createElement('svg:g')
+ for c in node_in.childNodes:
+ if c.localName in ('g', 'path', 'polyline', 'polygon'):
+ child = clone_and_rewrite(self, c)
+ if c.localName == 'g':
+ child.setAttribute('transform','matrix('+str(doc_sizeH/700.)+',0,0,'+str(-doc_sizeH/700.)+','+str(-doc_sizeH*0.25)+','+str(doc_sizeW*0.75)+')')
+ node_out.appendChild(child)
+
+ return node_out
+
+ doc = xml.dom.minidom.parse(filename)
+ svg = doc.getElementsByTagName('svg')[0]
+ group = clone_and_rewrite(self, svg)
self.current_layer.appendChild(group)
- child = svgdoc.firstChild
- while child is not None:
- if child.nodeName == "svg" and child.nodeType == Node.ELEMENT_NODE:
- add_paths(self,group,child)
- child = child.nextSibling
-
class EQTEXSVG(inkex.Effect):
def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("-f", "--formule",
- action="store", type="string",
- dest="formule", default=10.0,
- help="Formule LaTeX")
+ action="store", type="string",
+ dest="formula", default=10.0,
+ help="LaTeX formula")
def effect(self):
- base_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp")
- latex_file = base_file + ".tex"
- create_equation_tex(latex_file, self.options.formule)
- out_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp.output")
- os.system('latex -output-directory=' + tempfile.gettempdir() + ' ' + latex_file + '> ' + out_file)
+ base_file = os.path.join(tempfile.gettempdir(), "inkscape-latex.tmp." + str(os.getpid()))
+ latex_file = base_file + ".tex"
+ create_equation_tex(latex_file, self.options.formula)
+ out_file = base_file + ".output"
+ os.system('cd ' + tempfile.gettempdir() + '; latex -halt-on-error ' + latex_file + ' > ' + out_file);
ps_file = base_file + ".ps"
dvi_file = base_file + ".dvi"
svg_file = base_file + ".svg"
- os.system('dvips -q -f -E -D 600 -y 50000 -o ' + ps_file + ' ' + dvi_file)
- os.system('pstoedit -f plot-svg -centered -dt -ssp ' + ps_file + ' ' + svg_file + '>> ' + out_file + ' 2>&1')
+ os.system('dvips -q -f -E -D 600 -y 5000 -o ' + ps_file + ' ' + dvi_file)
+ os.system('cd ' + tempfile.gettempdir() + '; pstoedit -f plot-svg -dt -ssp ' + ps_file + ' ' + svg_file + '&> ' + out_file)
- # ouvrir le svg et remplacer #7F7F7F par #000000
svg_open(self, svg_file)
- # clean up
+ # Clean up
aux_file = base_file + ".aux"
log_file = base_file + ".log"
os.remove(latex_file)
os.remove(svg_file)
os.remove(out_file)
-#e = EQTEXSVG()
-#e.affect()
-try:
- e = EQTEXSVG()
- e.affect()
-except Exception, inst:
- sys.stderr.write(str(inst) + "\n")
+e = EQTEXSVG()
+e.affect()