javascript - 使用 mathjax-node 无法获得与浏览器上的 MathJax 相同的结果
问题描述
我想用 mathjax-node 渲染一些公式,但用户也应该能够用光标单独选择在它们下划线的每个元素。例如,如果出现求和,他可以选择极值、内部公式的元素、迭代变量等。当元素呈现为 <span> 对象时是可能的,例如,可以直接从 URL 使用 MathJax 2.7.7 来完成。
在浏览器上使用 MathJax 时,我有一个具有以下配置的页面:
<!DOCTYPE html>
<html>
<head>
...
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js","[Contrib]/forminput/forminput.js"],
jax: ["input/TeX","output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true,
},
TeX: {extensions: ["AMSmath.js","AMSsymbols.js"]},
displayAlign: 'center',
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}},
linebreaks: { automatic: true }
}
});
</script>
<script
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-MML-AM_CHTML"
/>
在这种情况下,如果我有这个公式 $\sum_{i=0}^ni>2$,结果是:
但现在我正在使用从https://github.com/mathjax/MathJax-node/tarball/master下载的 mathjax-node 。根据文档https://www.npmjs.com/package/mathjax-node我必须输入:
var mjAPI = require("mathjax-node");
mjAPI.config({
MathJax: {
// traditional MathJax configuration
}
});
mjAPI.start();
(在配置部分,它不再识别行:“extensions: ["tex2jax.js","[Contrib]/forminput/forminput.js"]")。
使用 typeset 函数获得带有结果的对象:
var yourMath = '\sum_{i=0}^ni>2';
let result = mjAPI.typeset({
math: yourMath,
format: "TeX", // or "inline-TeX", "MathML"
mml:true, // or svg:true, or html:true
},
然后用“result.mml”、“result.svg”或“result.html”调用应该渲染的部分。
我只设法使用“html:true”选项渲染为 <span> 对象。但它看起来与我在网络上使用 MathJax 时不同。例如,如果输入“格式:'inline-TeX'”,我会得到:
这里问了类似的问题:mathjax-node: different output when formatted on pages than in node project,解决方案是在排版中添加“css:true”选项,但我已经尝试过了,它看起来仍然一样。我想我仍然应该将其称为“result.html”。
解决方案
我正在回答我自己的问题。如果“result”是返回的对象,我不明白应该如何使用“result.css”属性。唯一要做的就是将它包含在显示网页样式的配置中。例如,网页可以是:
<html>
<head>
<style>
result.css
</style>
</head>
<body>
result.html
</body>
</html>
推荐阅读
- vb.net - 通过第二种形式 vb.net 编辑对象
- laravel - 我无法在 Laravel 中通过 SMTP 发送电子邮件,但只能在 sendmail 邮件驱动程序中发送
- flutter - Firestore StreamBuilder ListView - 屏幕闪烁
- c++ - 学习数据结构和算法的简单方法
- laravel - 如何将 Laravel 迁移字段克隆到另一个迁移中?
- c# - 使用 C# 和实体框架生成以月为单位的 DateTime 系列
- sql - 从两个连接表中仅选择一个结果字段
- rest - PUT 请求与 Angular 一起使用,但与 Postman/Insomnia 一起返回 401
- javascript - Plotly 仪表动态
- r - 在点上使用 dplyr::recode