I would like to dynamically generate a typeset equation as an SVG object and then insert it into an existing SVG image in a web page. Right now, I have the following:
function insertEquation() {
let integral = MathJax.tex2svg(
String.raw`\int_{-1}^1 \sqrt{1-x^2} \, dx = \frac{1}{2}\pi`
);
//$("#span").append(integral);
let int = integral.children[0];
int.setAttribute("x", "2");
int.setAttribute("y", "2");
d3.select("#svg").append(int);
}
However, when I try to append the SVG element, I get the following error:
Uncaught DOMException: Failed to execute 'createElementNS' on 'Document': The qualified name provided ('[object SVGSVGElement]') contains the invalid name-start character '['.
I'm sure the SVG is fine, because if I uncomment the line which appends the equation to a span, it appears just fine. Could someone please help me figure out what I'm doing wrong?
Many thanks,
Jason