AllenDowney / plastex-oreilly

Branch of plastex that generates DocBook 4.5 that meets O'Reilly style guidelines.
6 stars 7 forks source link

easier way to render MathML? #14

Closed AllenDowney closed 12 years ago

AllenDowney commented 12 years ago

In TreeCleaner I am using Tralics to translate LaTeX math into MathML. The result is a string of XML, like

<formula type='display'><math mode='display' xmlns='http://www.w3.org/1998/Math/MathML'><mrow><msup><mi>&#x3C3;</mi> <mn>2</mn> </msup><mo>=</mo><mfrac><mn>1</mn> <mi>n</mi></mfrac><munder><mo>&#x02211;</mo> <mi>i</mi> </munder><msup><mrow><mo>(</mo><msub><mi>x</mi> <mi>i</mi> </msub><mo>-</mo><mi>&#x3BC;</mi><mo>)</mo></mrow> <mn>2</mn> </msup></mrow></math></formula>

I use lxml to parse it; the result is a tree of etree.Element

Then I convert this tree into a tree of DOM.Node

Now, in order for that to get rendered, I am currently adding a new command in Base/LaTeX/Math.py for each MathML tag, for example:

class mmlmi(Command):
    args = 'self'

Then I am adding a new template in Renderers/Docbook/book.zpts (maybe it should be in Math.zpts), for example:

name: mmlmi
<mml:mi tal:content="self"></mml:mi>

This is working, but it seems crazy to add a Command and a template for every MathML tag when they are all doing the same thing. Is there a smarter way to do this?

One option would be to render the entire expression as a string during TreeCleaning, then render the whole thing as one string. In that case I need to do something in the Renderer to treat the string as raw XML and not convert < and > to XML entities.

AllenDowney commented 12 years ago

I went ahead and generated class definitions and ztp templates for all of the mathml presentation tags listed here:

http://www.w3.org/TR/MathML2/chapter3.html#presm.summary

Still not sure this is the most elegant solution, but it works.