Skip to the Main Content

Note:These pages make extensive use of the latest XHTML and CSS Standards. They ought to look great in any standards-compliant modern browser. Unfortunately, they will probably look horrible in older browsers, like Netscape 4.x and IE 4.x. Moreover, many posts use MathML, which is, currently only supported in Mozilla. My best suggestion (and you will thank me when surfing an ever-increasing number of sites on the web which have been crafted to use the new standards) is to upgrade to the latest version of your browser. If that's not possible, consider moving to the Standards-compliant and open-source Mozilla browser.

October 22, 2007

SVG in MathML in …

As I’ve mentioned before, there is an effort afoot to enable the inclusion of MathML and SVG (and maybe other) markup in HTML5. But that’s a long way off (if it happens at all). What I really want to talk about, today, is an issue that affects us in the here and now: mixing MathML and SVG markup.

The subject came up in a discussion over at Sam’s blog about SVG-in-HTML5. Some people were arguing that, even if it were technically possible, it would be undesirable to sully HTML5 with the inclusion of foreign dialects, like MathML or SVG. The latter, in particular, was in some quarters considered too “presentational.”

This annoyed me sufficiently that I gave the following example:

Say I wish to discuss irreducible representations of SU(3). I think it should be perfectly acceptable to point out that: Rank-2 Symmetric Tensor Representation Fundamental Representation = Adjoint Representation Rank-3 Symmetric Tensor Representation .

Actually, what I wrote at Sam’s blog was a somewhat bastardized version of the above1. At the time, I really had no idea how to properly include SVG fragments inside a MathML equation. It turns out that the “correct” answer is some horrible kludge, involving embedding the SVG in the MathML <semantics> element. Go figure …

It seems to me that the Spec-writers at the W3C haven’t put much thought into how to combine these markup dialects. The best I could find, by way of guidance, was an unfinished note from 2003. Work has started on MathML 3.0, but, apparently, remedying this situation is beyond the scope of their charter. So we’re not going to get any help from that quarter.

Which is unfortunate. The folks over at the n-Category Café have been clamouring for the ability to include fairly sophisticated diagrams. One way or another, I intend to find a MATHML+SVG solution for them. It would be a shame if the way forward involved reverse-engineering what the browser-makers (i.e. Mozilla, since neither Safari nor Opera support MathML) have implemented, and tailoring our markup accordingly.

In the particular case of Young tableaux, it would be easy to extend itex2MML to make entering the above formula as simple as

$\tableau{2} \otimes \tableau{1} = \tableau{2 1} \oplus \tableau{3}$

I once wrote a LaTeX macro to to do precisely that. I may yet implement this in itex, but we really need a more general solution for including SVG fragments in itex equations.

Introducing the itex “svg” environment:

\begin{svg}
   ... 
\end{svg}

into which you can drop an arbitrary SVG fragment. It takes care of supplying the requisite <semantics> and <annotation-xml> tags.

“How does this work with Instiki’s LaTeX export?” you ask. To get something that also works in the LaTeX export, you would write

\begin{svg}
   ...
\end{svg}\includegraphics[width=...]{foo}

where foo.pdf is a file, containing a PDF version of the graphic.

In itex, \includegraphics is a NOOP, and the SVG figure is embedded in the MathML output. Conversely, in Instiki’s LaTeX output, the svg environment is a NOOP and, instead, the graphicx package is invoked to include the external figure. Not ideal, but until someone invents a LaTeX preprocessor that converts inline SVG into PDF \specials, it’s the best we can do.

To accomplish all of this, there are new versions of

  • itex2MML
  • the itexToMML plugin for MovableType (bundled with itex2MML)
  • Instiki

[Update (10/24/2007): Revised, so that the itex2MML stream filter (used by the MT plugin) has the nice error-recovery it used to have. This doesn’t affect Instiki.]

As nifty as the above may be, it still only scratches the surface of what we need.

In the taxonomy of that unfinished note, there are also cases where one wants to embed MathML in SVG. For instance, it would be preferable if the labels on this Feynman diagram

1-loop diagram, coupling electron to an external EM field e e γ
1-loop Contribution to Electron Magnetic Moment

were embedded snippets of MathML, instead of kludgily positioned glyphs in an <svg:text> element. The latter barely suffices2 in this case; in other examples, it would clearly be doomed. The <svg:foreignObject> element is presumably the way to go. I say “presumably,” because there are, currently, no browsers that support both <foreignObject> and MathML.

Finally, there are more complicated diagrams, of the sort beloved by the denizens of the n-Category Café like, say

a complicated diagram from Urs

drawn from this post, where there are several other interesting examples.

That’s where some real thought is required.

Perhaps an xy-pic to SVG+MathML translator (built into itex2MML)?

Update (10/26/2007): Not up to the task

I couldn’t resist redoing the above graphic in SVG, just to see what it would take. As you might have guessed (since you can see them), all the symbols are crudely-positioned text, rather than real MathML equations, embedded in <foreignObject> elements. Even so, one can see the improvement (try, for instance, resizing the text in your browser).

Complicated commutative diagram, realized in SVG 1 1 1 1 Id Id A B ρ H H K K φ1 φ2 NA NB NA NB

To automatically generate such a diagram, one might want to use an input syntax based on xy-pic or DC-pic. For instance, in DC-pic, some semblance3 of the above figure would be generated by (Category Theorists rejoice!)

\begindc[2]
\obj(30,20){$1$}[ll1]
\obj(80,20){$1$}[lr1]
\obj(30,70){$A$}[A]
\obj(80,70){$B$}[B]
\obj(30,120){$1$}[ul1]
\obj(80,120){$1$}[ur1]
\mor{ul1}{A}{$N_A$}[\atright,\solidarrow]
\mor{ur1}{B}{$N_B$}
\mor{A}{ll1}{$N_A^\vee$}[\atright,\solidarrow]
\mor{B}{lr1}{$N_B^\vee$}
\mor{ll1}{lr1}{$H$}[\atright,\solidarrow]
\mor{ul1}{ur1}{$H$}
\cmor((25,115)(10,70)(25,25)) \pdown(5,70){$Id$}
\cmor((85,115)(100,70)(85,25)) \pdown(105,70){$Id$}
\cmor((35,75)(55,80)(75,75)) \pright(55,85){$K$}
\cmor((35,65)(55,60)(75,65)) \pright(55,55){$K'$}
%should be double arrows:
\mor(55,82)(55,58){$\rho$}
\mor(30,70)(7,70){}
\mor(80,70)(103,70){}
\mor(77,117)(35,75){$\phi_1$}[\atright,\solidarrow]
\mor(75,65)(33,23){$\phi_2$}
\enddc

Unfortunately, as that example illustrates, the existing capabilities of SVG are rather far from what one would want, in order to implement such a thing. In order for a command like

\mor{ur1}{B}{$N_B$}

to place a label on an arrow, <foreignObject> would need to be able to compute the size of the box containing its content (in this case, “N B”) and, that knowledge in hand, compute its own position relative to the arrow it’s labelling. In fact, to draw the arrow itself, DC-pic needs to calculate the size of the boxes containing the source (“ur1”) and target (“B”). These calculations are done in the LaTeX packages mentioned above, but they seem to be beyond the current capabilities of SVG.

One would need something like CSVG.


1 Actually, the above example is still somewhat bastardized, as the SVG should be sized in em, rather than px. Because of a Mozilla bug, we can’t do that currently. Which means that the SVG graphics do not rescale along with the text.

2 Provided you’re not on a Mac. Because of this bug, Mac users, using release versions of Mozilla-based browsers can’t see the labels at all. You can get around this by using a nightly build from mid-November, 2006.

3 Not quite, alas. DC-pic doesn’t have native support for double-arrows (), and has limitations on arrowheads. These shortcomings could be rectified by allowing the embedding of raw PicTex commands.

Posted by distler at October 22, 2007 11:40 PM

TrackBack URL for this Entry:   http://golem.ph.utexas.edu/cgi-bin/MT-3.0/dxy-tb.fcgi/1475

10 Comments & 2 Trackbacks

Re: SVG in MathML in …

In fact the W3C’s web browser has supported (presentation) MathML inside SVG since a long time. Also, it has other interesting features to use MathML with other standards.

Nevertheless, I do not think the W3C describes a way to use SVG inside MathML. According to the specification, what you propose with the “semantic” element is an annotation of its first child (the “annotation-xml” element) which is syntactically correct but has not the expected meaning…

Posted by: fred_wang on October 25, 2007 3:03 PM | Permalink | Reply to this
Read the post Sanitizing SVG
Weblog: Musings
Excerpt: Thinking about safety.
Tracked: October 29, 2007 12:03 AM

Re: SVG in MathML in …

I’m integrating RELAX NG schemas for XHTML5, SVG 1.1 and MathML 2.0. I’m not sure what I should do about annotation-xml. Currently I just have an anything-goes hole there.

Do you have advice on what the content model of annotation-xml should be when I already have grammar productions for the svg element and XHTML5 elements at hand?

How did you come up with the value of the encoding attribute? To me, the attribute seems badly under-specified.

Posted by: Henri Sivonen on December 17, 2007 10:17 AM | Permalink | Reply to this

annotation-xml

Do you have advice on what the content model of annotation-xml should be when I already have grammar productions for the svg element and XHTML5 elements at hand?

Obviously, it needs to be namespace-well-formed XML. Presumably, it should be a valid XHTML+MathML+SVG fragment. Beyond that, I think it is, by design, an anything-goes hole.

How did you come up with the value of the encoding attribute?

Like any moron, I followed an example.

Posted by: Jacques Distler on December 17, 2007 1:02 PM | Permalink | PGP Sig | Reply to this

Re: annotation-xml

Do you have advice on what the content model of annotation-xml should be when I already have grammar productions for the svg element and XHTML5 elements at hand?

Obviously, it needs to be namespace-well-formed XML. Presumably, it should be a valid XHTML+MathML+SVG fragment. Beyond that, I think it is, by design, an anything-goes hole.

Well-formed, i.e. single-root, is not obvious from MathML 2.0 but it is from the latest MathML 3.0 draft.

It is generally accepted that a MathML fragment is rooted at math and that an SVG fragment is rooted at svg. However, it is not clear if a single-root XHTML fragment is rooted at html, body, div or any XHTML element. I’m seen examples with a lone img.

Also, besides XHTML and SVG should any other namespace be allowed? Or just OpenMath? (What even happens when OpenMath meets a browser?)

How did you come up with the value of the encoding attribute?

Like any moron, I followed an example.

Interesting. The same document contains another example that uses a bogus MIME type image/svg instead and also violates the rule that annotation-xml must contain a well-formed (i.e. single-root) subdocument.

I wonder if I should enforce some kind of concordance between the encoding value and the actual child element.

Posted by: Henri Sivonen on December 17, 2007 2:36 PM | Permalink | Reply to this

Re: annotation-xml

However, it is not clear if a single-root XHTML fragment is rooted at html, body, div or any XHTML element. I’m seen examples with a lone img.

I think one should allow it to be rooted at any “FLOW” element (which would include div and img, but not html or body).

I wonder if I should enforce some kind of concordance between the encoding value and the actual child element

As far as I can tell, current clients completely ignore the encoding value. But it certainly would be good practice to enforce that the encoding value accord with the actual child element.

But now you’re pretty much asking me to make up a Spec on the spot. The subject of this post was how little guidance has been given for the construction of compound documents of this sort.

I can tell you what sounds right to me, and what I am doing here on Musings and in my Instiki and itex2MML software (see here and here).

But there could conceivably be other stakeholders who have different ideas. I’d be happy to host a discussion if they were interested in chiming in with their opinions.

Posted by: Jacques Distler on December 17, 2007 3:14 PM | Permalink | PGP Sig | Reply to this

Re: annotation-xml

However, it is not clear if a single-root XHTML fragment is rooted at html, body, div or any XHTML element. I’m seen examples with a lone img.

I think one should allow it to be rooted at any “FLOW” element (which would include div and img, but not html or body).

For now, I have allowed block, inline and html. I allowed html due to a MathML spec example and due to the current stance of the HTML 5 draft.

I wonder if I should enforce some kind of concordance between the encoding value and the actual child element As far as I can tell, current clients completely ignore the encoding value. But it certainly would be good practice to enforce that the encoding value accord with the actual child element.

The encoding attribute is now optional but if it is present, concordance with the namespace of the child is required. The encoding values are per MathML 2.0. MathML 3.0 draft style is not supported.

But now you’re pretty much asking me to make up a Spec on the spot. The subject of this post was how little guidance has been given for the construction of compound documents of this sort.

Considering that the speccing is thin, I thought I’d try to find out about actual practice.

I can tell you what sounds right to me, and what I am doing here on Musings and in my Instiki and itex2MML software (see here and here).

Until a few moments ago, Validator.nu had a bug that showed up on Musings because of the way buffer boundaries happened to align. It is now fixed though. The current usage patterns on Musings seem to fit the integration points I have in the schema.

(It appears that your front page side bar has some content pulled from elsewhere without thorough Windows-1252 escape sanitizing.)

But there could conceivably be other stakeholders who have different ideas. I’d be happy to host a discussion if they were interested in chiming in with their opinions.

I’d be particularly interested in learning what foreign markup is actually deployed on the Web in annotation-xml and what the expected and actual consuming programs are.

Posted by: Henri Sivonen on December 21, 2007 8:08 AM | Permalink | Reply to this

Windows-1252

(It appears that your front page side bar has some content pulled from elsewhere without thorough Windows-1252 escape sanitizing.)

Technorati Blog Cosmos.

I’ve done my best to correct the stuff one retrieves via their API. At some point, one hits the point of diminishing returns.

I’d be particularly interested in learning what foreign markup is actually deployed on the Web in annotation-xml and what the expected and actual consuming programs are.

There’s someone else actually deploying this stuff on the Web?

Cool! I’d like to meet him/her.

Posted by: Jacques Distler on December 21, 2007 10:41 AM | Permalink | PGP Sig | Reply to this

Re: SVG in MathML in …

Gecko 1.9 (Firefox 3) will support svg:foreignobject and MathML. You can embed SVG, MathML and HTML in each other freely.

Posted by: Robert O'Callahan on December 17, 2007 4:57 PM | Permalink | Reply to this

Re: SVG in MathML in …

Cool!

Now all we need is for Karl Tomlinson to finish fixing MathML in Mozilla Trunk. I’m hesitant to start filing new MathML-related bugs until Karl has fixed the ones he’s currently working on.

Posted by: Jacques Distler on December 17, 2007 6:21 PM | Permalink | PGP Sig | Reply to this
Read the post foreignObject
Weblog: Musings
Excerpt: Taking the new Firefox out for a spin.
Tracked: February 4, 2008 3:51 PM

Re: SVG in MathML in …

FireFox 3 Beta 3 and Amaya 10 pre-release both support mathml in svg and a physical science oriented page can pass validator.nu validation without math: nor svg: prefixes on elements. That is cause for celebration!

Posted by: Dana Lee Ling on March 8, 2008 7:44 PM | Permalink | Reply to this

Post a New Comment