Render MathJax in an SVG file

谁都会走 提交于 2021-01-19 04:39:14

问题


I have spent a few days on the following with no joy.

I wish to render Mathjax in an SVG file. I have no problem including it in a html file in an svg element using a foreignObject from the examples at https://groups.google.com/forum/#!topic/mathjax-users/_UMt3C7TIpQ/discussion, but I cannot get it to work in an svg file.

The code I am trying is as follows :-

<svg width="1960" height="1080" xmlns="http://www.w3.org/2000/svg">
<script type="text/javascript" src="MathJax-master/MathJax.js?config=TeX-AMS_HTML-SVG"></script>
<g>
<title>Layer 1</title>
<text xml:space="preserve" text-anchor="middle" font-family="serif" font-size="50" id="svg_1" y="223" x="636" stroke-opacity="0.8" stroke-width="0" stroke="#007FFF" fill="#000000">Hello World</text>
<foreignObject x="100" y="100" width="100" height="100">
   <body xmlns="http://www.w3.org/2000/svg">
     <div>
       \(\displaystyle{x+1\over y-1}\)
     </div>
   </body>
 </foreignObject>
</g>
</svg>

Any help would be much appreciated. I do suspect the problem is with the line declaring the body element.


回答1:


A <div> tag is html so the <body> tag should be in the html namespace xmlns="http://www.w3.org/1999/xhtml" rather than the svg namespace.

Your other mistake is that you're using html syntax for the script tag. SVG script tags use xlink:href instead of a src attribute. Fixing that gets us here:

<svg width="1960" height="1080" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<script xlink:href="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<g>
<title>Layer 1</title>
<text xml:space="preserve" text-anchor="middle" font-family="serif" font-size="50" id="svg_1" y="223" x="636" stroke-opacity="0.8" stroke-width="0" 

stroke="#007FFF" fill="#000000">Hello World</text>
<foreignObject x="100" y="100" width="100" height="100">
   <body xmlns="http://www.w3.org/1999/xhtml">
     <div>
       \(\displaystyle{x+1\over y-1}\)
     </div>
   </body>
 </foreignObject>
</g>
</svg>

But when we do that we run into a bug in the mathjax library. It seems it expects to find html nodes in the document (check the Firefox Error Console). You'd have to contact the mathjax developers and get them to fix their bug to progress further.



来源:https://stackoverflow.com/questions/14862410/render-mathjax-in-an-svg-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!