Figures
My last paper was written almost entirely on an Instiki wiki, transferred to a TeX file (in an SVN repository) only a few days before submission. The 136 figures were prepared using Instiki’s nifty built-in WYSIWYG SVG-editor.
Unfortunately, when it came time to “export” the figures to accompany the TeX file, I realized – to my horror – that there did not exist a tool for converting SVG, with embedded MathML, into PDF. We ended up producing PNG bitmaps (essentially, taking screen shots in the browser). That was time-consuming (thank heavens for a graduate student co-author) and the results were less than completely satisfactory.
So, with the paper squared-away, and the semester well underway, I sat down to put together a better solution.
Inkscape (for instance) has a quite nice ability to export SVG to PDF. But it is completely oblivious to the embedded MathML. Luckily, I found an (apparently inactive) project which specialized in “rendering” MathML to SVG. My attempts to contact the author were in vain, so I launched in, to fix what needed fixing:
- It didn’t support OpenType fonts, which meant that — without some modification, it wouldn’t work with the STIX fonts.
- It didn’t really work right when converting MathML inside a
<foreignObject>
element in an SVG document. Presumably, this was not a use-case considered by the author, but fixing it was trickier than I thought it would be. - There was an amusing bug in the Python SAX parser, which needed fixing.
Anyway, there’s now a working version of SVGMath available, with all of the above fixes. (To be honest, it could still use a decent setup.py
to be a proper Python package.)
With that in hand, a simple script1
#!/usr/bin/env ruby require 'tempfile'
IO.readlines(ARGV[0]).join.scan(/(<svg.*?<\/svg>)\s*\\end\{svg\}\\includegraphics\[.*?\]\{(.*?)\}/m) do |svg, outfile| puts "Converting #{outfile}.pdf" f = Tempfile.new("convert_svg") << svg f.close `math2svg.py #{f.path} > #{f.path}_fixed.svg; \ /Applications/Inkscape.app/Contents/Resources/bin/inkscape -z -f #{f.path}_fixed.svg -A #{outfile}.pdf` File.delete(f.path, "#{f.path}_fixed.svg") end
sufficed to convert the wiki source into 136 PDF files, containing the figures.
Now, I can be as proud of the figures as I am of the physics (which is, I must say, quite neat).
1 As a Python newbie, I’m curious to know how someone, better-acquainted with the language, would write that in Python.
Re: Figures
Untested conversion: