使用MathJax设置/呈现动态内容
Typeset/render dynamic content with MathJax
我使用MathJax来显示数学方程式。它在静态书写的数学中运行良好。但不适用于动态加法。
这是我的代码
<body>
//Static
<div>
<span>'(x = {-b 'pm 'sqrt{b^2-4ac} 'over 2a}')</span>
</div>
//Dynamic
<div id="dynamic-pan">
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#dynamic-pan').empty();
$('#dynamic-pan').append('<span>'(x = {-b 'pm 'sqrt{b^2-4ac} 'over 2a}')</span>');
});
</script>
</body>
我已经用两个跨度元素写了数学。第一个是静态声明的,第二个是在文档准备功能中动态添加的
请帮我解决这个问题。
MathJax v3
http://docs.mathjax.org/en/latest/web/typeset.html
- 同步类型集:
MathJax.typeset()
- 异步类型集:
MathJax.typesetPromise()
setTimeout(function () {
const content = document.createElement('span')
content.textContent = '''(x = {-b ''pm ''sqrt{b^2-4ac} ''over 2a}'')'
const done = document.createElement('span')
done.textContent = ' done!'
const syncTypeset = document.querySelector('#syncTypeset')
syncTypeset.appendChild(content.cloneNode(true))
setTimeout(function () {
MathJax.typeset()
syncTypeset.appendChild(done.cloneNode(true))
}, 3000)
const asyncTypeset = document.querySelector('#asyncTypeset')
asyncTypeset.appendChild(content.cloneNode(true))
setTimeout(async function () {
await MathJax.typesetPromise()
asyncTypeset.appendChild(done.cloneNode(true))
}, 3000)
}, 0)
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['''(', ''')']]
},
svg: {
fontCache: 'global'
}
};
</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js" id="MathJax-script"></script>
//Static
<div>
<span>'(x = {-b 'pm 'sqrt{b^2-4ac} 'over 2a}')</span>
</div>
//Dynamic
<div id="syncTypeset">Sync after 3 second: </div>
<div id="asyncTypeset">Async after 3 seconds: </div>
MathJax v2
您需要告诉MathJax查找使用Typeset()
方法完成的未处理的数学,因为MathJax在调用Typeset()
时可能正在运行,您需要将其添加到其队列中
$(document).ready(function() {
var $el = $('#dynamic-pan')
$el.empty()
$el.append('<span>''(x = {-b ''pm ''sqrt{b^2-4ac} ''over 2a}'')</span>')
MathJax.Hub.Queue(['Typeset', MathJax.Hub, $el[0]]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML"></script>
//Static
<div>
<span>'(x = {-b 'pm 'sqrt{b^2-4ac} 'over 2a}')</span>
</div>
//Dynamic
<div id="dynamic-pan"></div>
有关的更多信息,请参阅本文档
编辑:字符'
在字符串上有特殊含义(它转义了下面的字符)以避免这种行为。请确保使用''
使其出现在最后的字符串
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 动态分配GA增强型电子商务跟踪器
- onkeyup无法动态创建多个文本区域
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何使Javascript动态html表及其上的事件
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何在Bootstrap Modal中为动态点击生成的变量设置jade属性
- 如何使用javascript从主svg对象动态创建svg视图框
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 通过在Dojo mobile ViewController.openExternalView中动态更改打开同一外部视图的
- 如何在Google柱状图中动态添加行/列
- 困在逻辑中试图定位动态的东西
- 在动态创建的元素上获取对特定选择器的引用
- 如何动态插入jquery代码
- 使用Google Visualization动态调用构造函数
- 使用MathJax设置/呈现动态内容
- 无法动态加载MathJax
- 仅当有分隔符时才动态显示MathJax
- 动态渲染mathjax