External MathML files with <object> does not work with MathJax

111 views
Skip to first unread message

Dirk

unread,
Jun 9, 2012, 12:33:24 PM6/9/12
to MathJax Users
Is there a way that I can embed external MathML files in a HTML
document? Usually I would do it with <object>. Sadly this seems not to
work with MathJax. Is there another way?

Davide P. Cervone

unread,
Jun 9, 2012, 7:37:47 PM6/9/12
to mathja...@googlegroups.com
Can you post an example of what you would like to do? I have not
heard of using <object> to load MathML, so it would help to see what
you have in mind in more detail.

Davide

Dirk

unread,
Jun 10, 2012, 2:23:04 AM6/10/12
to MathJax Users
<object data="identity.xml" type="text/xml"></object>

identity.xml is a MathML file. This works in all browsers today. But
MathJax does not recognize the MathML format of the file I guess. I
have a HTML file with a lot of formulas and would like to keep the
formulas separate from the page for maintenance reasons.

Davide P. Cervone

unread,
Jun 10, 2012, 8:10:24 AM6/10/12
to mathja...@googlegroups.com
Can you send an example of identity.xml? Does it contain anything
other than the <math> element and its contents (e.g., is there any
other xml needed to make the file complete?)

Since the MathML is not actually part of the HTML file, MathJax won't
see the MathML. I have some ideas about how to get around that,
though, and will let you know whether I can suggest anything that will
work for you.

Davide

Davide P. Cervone

unread,
Jun 10, 2012, 10:19:15 AM6/10/12
to mathja...@googlegroups.com
Well, I did some experiments and it looks like the files don't need to
contain anything other than a <math> tag. I don't know how you are
setting the height and width, however (my tests indicate that the
browsers simply use a default width and height that has nothing to do
with the actual contents of the file). Also the baseline would need
to be adjusted if you were using in-line math, and it is not clear how
to determine that either.

In any case, assuming that your xml files contain a single <math> tag,
I have the following suggestion. Add

<script type="text/x-mathjax-config">
MathJax.Extension.object2jax = {
version: "1.0",
PreProcess: function (element) {
if (typeof(element) === "string") {element =
document.getElementById(element)}
if (!element) {element = document.body}
var objects = element.getElementsByTagName("object");
for (var i = objects.length-1; i >= 0; i--) {
var object = objects[i];
var math = ((object.contentDocument||{}).documentElement||{});
if (String(math.nodeName).toLowerCase() === "math") {
math = document.importNode(math,true);
object.parentNode.insertBefore(math,object);
object.parentNode.removeChild(object);
}
}
}
};

MathJax
.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.object2jax],
5); // priority 5 is before mml2jax
</script>

somewhere before the script that loads MathJax.js (or put it in your
local configuration file if you are using one). This will register a
preprocessor that will look for <math> elements in <object> tags and
will incorporate them into the body of the document, where the mml2jax
preprocesor will be able to recognize and handle them. Note that this
avoids having to specify a height and width for the <object>, since
the <object> is actually removed and the MathML is inserted into the
document directly. This also gets the baseline to work properly, too,
when y9ou are using in-line math.

Anyway, I think that will do what you need.

Davide


On Jun 10, 2012, at 2:23 AM, Dirk wrote:

Reply all
Reply to author
Forward
0 new messages