#!/usr/bin/env python # # mathmltest.py -- test the MathML support in eqn # # This script generates an HTML test page from a list of eqn equations. # The test page will have putatively equivalent MathML in it. # # To use, capture the output in a file with extension .xhtml and view it with # Firefox or some other MathML-capable browser. You must ensure that # the browser ships .xhtml pages with Mime-Type: # application/xhtml+xml, or the MathML will not render correctly. # Apache does this. # # As of Firefox 1.5, some of these tests will have visual glitches including # braces that don't stretch and botched radical signs. These reflect bugs in # the Firefox display engine, not problems with the MathML translation. import os, commands, tempfile tests = ( ("x + y", "Demonstrate binary operators and identifiers"), ("a sub 1", "Demonstrate subscripting."), ("b prime", "Demonstrate prime boxes."), ("x={-b +- sqrt{b sup 2 -4ac}} over 2a", "Quadratic formula."), ("lim from {x -> pi /2} ( tan~x) sup{sin~2x}~=~1", "Inline first example from the abstract of the Guide."), (''' G(z)~mark =~ e sup { ln ~ G(z) } ~=~ exp left ( sum from k>=1 {S sub k z sup k} over k right ) ~=~ prod from k>=1 e sup {S sub k z sup k /k} ''', "Part 1 of the second (display) example from the abstract of the Guide."), (''' lineup = left ( 1 + S sub 1 z + { S sub 1 sup 2 z sup 2 } over 2! + ... right ) left ( 1+ { S sub 2 z sup 2 } over 2 + { S sub 2 sup 2 z sup 4 } over { 2 sup 2 cdot 2! } + ... right ) ...''', "Part 2 of the second (display) example from the abstract of the Guide."), (''' lineup = sum from m>=0 left ( sum from pile { k sub 1 ,k sub 2 ,..., k sub m >=0 above k sub 1 +2k sub 2 + ... +mk sub m =m} { S sub 1 sup {k sub 1} } over {1 sup k sub 1 k sub 1 ! } ~ { S sub 2 sup {k sub 2} } over {2 sup k sub 2 k sub 2 ! } ~ ... { S sub m sup {k sub m} } over {m sup k sub m k sub m ! } right ) z sup m ''', "Part 3 of the second (display) example from the abstract of the Guide."), ("x = 2 pi int sin ( omega t ) dt", "Integral example from section 6 of the Guide."), ("A~=~left [ pile { a above b above c } ~~ pile { x above y above z } right ]", "First pile example from section 17 of the Guide."), ("matrix { ccol { x sub i above y sub i } ccol { x sup 2 above y sup 2 }}", "First matrix example from section 18 of the Guide."), ) def filter_string(str, command): fp = tempfile.NamedTemporaryFile(prefix="mathmltest") fp.write(str + "\n") fp.flush() output = os.popen(command + " <" + fp.name, "r").read() fp.close() return output print ''' Test page demonstrating eqn to MathML translation This is a test page demonstrating eqn to MathML translation with the -TMathML option. References to "the Guide" are to the 1976 description of eqn, Typesetting Mathematics - User\'s Guide (Second Edition) by Brian Kernighan and Lorinda Cherry. ''' for (test, explanation) in tests: print "

%s

" % explanation mathml = filter_string(".EQ\n" + test + "\n.EN\n", "eqn -TMathML") mathml = mathml.replace(".EQ\n", "").replace(".EN\n", "") mathml = mathml.replace("", "") #mathml = filter_string(mathml, "xmllint --valid --format -") print "EQN markup text:" print "
%s
" % test print "MathML translation:" print mathml print ''' '''