I\'m doing some writing where I use MathJax to render the math. I also occasionally include SVG diagrams which are generated dynamically by javascript. Those SVG diagrams occasi
There is a way to include MathJax SVG elements in an SVG diagram, without requiring foreignObject
.
A full demonstration of the technique is at http://puzlet.com/m/b00b3.
Say you have an SVG diagram like this (and containing any SVG elements) in your web page:
and the following MathJax:
$\Omega$
Use this CoffeeScript (plus jQuery) to integrate the MathJax element into your SVG diagram:
diagram = $("#diagram") # SVG diagram
obj = new MathJaxObject "mathjaxSource" # MathJax object (see class definition below)
obj.appendTo diagram # Append MathJax object to diagram
obj.translate 100, 200 # Position MathJax object within diagram
You can also use the width()
and height()
methods for centering and vertical alignment.
class MathJaxObject
constructor: (@divId, @scale=0.02) ->
@source = $("##{@divId}").find ".MathJax_SVG"
@svg = @source.find "svg"
g = @svg.find "g"
@group = $(g[0]).clone()
@translate 0, 0
viewBox: -> @svg[0].viewBox
width: -> @scale * @viewBox().baseVal.width
height: -> @scale * @viewBox().baseVal.height
translate: (@dx, @dy) ->
dy = @dy + (-@scale * @viewBox().baseVal.y)
@group[0].setAttribute "transform",
"translate(#{@dx} #{dy}) scale(#{@scale}) matrix(1 0 0 -1 0 0)"
appendTo: (diagram) ->
diagram.append @group