[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
Subject: Re: Aw: Re: Re: [docbook] LaTeX math inside docbook, on-the-fly MathML conversion?
Hi, On 01/29/2015 10:14 PM, Marco Giebel wrote:
Because typing complicated math with MathML is too time-consuming, I want to include math as LaTeX formulas. This LaTeX needs to be converted to MathML, before I can apply my processing tools.
My xslt knowledge is a bit lacking especially with external tools. Maybe it is possible to call external converter tools on special tags. I have built docbook->website translator in python. http://source.tree.se/treecutter/ the step "Expand xinclude parse text" method could work in this case as a preprocessor, but without using the xlinclude tag, but something like : <equation role="latex"><alt>COMPLEX LATEX EQUATION</alt></equation> You would have to write a small python program that does something like the routine prepare in https://source.tree.se/git/treecutter.git/blob/HEAD:/treecutter/page.py Where I find xinclude :code = self._doc.xpath(u"//xi:include[@parse='text']",namespaces=const.XPATH)
you would have to find u"//equation[@role='latex'] Then take the text inside the alt tag as input for a Subprocess: exe in your case an external tool : xml = subprocess.Popen(exe,stdout=subprocess.PIPE, stderr=subprocess.PIPE)How you move the latex text to the tool of choise you have to think about. Either write a temp file with the content and supply that file in the exe command, or maybe stdin is possible.
In my case I have just limited size of input. Then let the tool do the transform to stdout As for Latex to MathML I saw 4 tools here : http://www.w3.org/Math/wiki/ToolsI can not make any recommendations regarding tools as I have not used math in docbook much.
You can also use an external script as I use. You can see example of xinclude scripts here : https://source.tree.se/git/treecutter.git/tree/HEAD:/xincludeEx. address.py. Given an address it generates an map and builds xml to present it. There you see how much control you have over generating/validating the mathml.
Once the tool is done you take the stdout result, parse it and insert it in the tree again :
xstr = etree.fromstring(stdout) # inserting the generated code and remove the xinclude reference idp = c.getparent() idp.insert(idp.index(c)+1,xstr) idp.remove(c)In your case not removing equation but the alt tag or keeping it inserting the mml: tree under equation.
It can be that you need to add proper namespacing for python lxml.Now you just have to write the tree to disk, and continue to use your toolchain.
vi/emacs docbook.latex.xml python convert.py docbook.latex.xml docbook.mathml.xml ./process docbook.mathml.xml This is the most flexible way to preprocess docbook I have found so far. Python have many libraries that you can use to treat special data. I process images, encrypt data, select data to include using python, but keeping the pure but somewhat expanded docbook at the end. If this method would suit you and if you need more information let me know. Sincerely, Fredrik Unger