gpt4 book ai didi

node.js - 使用 mathjax 和 node.js 在网络浏览器中显示 latex 数学方程

转载 作者:太空宇宙 更新时间:2023-11-04 01:31:31 26 4
gpt4 key购买 nike

我正在尝试下载 node.js Mathjax 库并运行 GitHub 页面上提供的示例:https://github.com/mathjax/MathJax-node

我遵循的步骤:

第 1 步: mkdir mydemo

第 2 步: cd mydemo

第 3 步: npm install mathjax-node

第 4 步:从 GitHub 网站废弃示例 javascript 代码

// I place this in file ".\lib\main.js
// a simple TeX-input example
var mjAPI = require("mathjax-node");
mjAPI.config({
MathJax: {
// traditional MathJax configuration
}
});
mjAPI.start();

var yourMath = 'E = mc^2';

mjAPI.typeset({
math: yourMath,
format: "TeX", // or "inline-TeX", "MathML"
mml:true, // or svg:true, or html:true
}, function (data) {
if (!data.errors) {console.log(data.mml)}
});

第 5 步:我设置“.\index.html”来加载 mathjax 的 Javascript 演示代码。

<html>
<head>
<script src="./lib/main.js></script>
</head>
<body></body>
</html>

第 6 步:我将 index.html 加载到 Chrome Web 浏览器中。什么都没发生。 :-(

诚然,我对 javascript 和 Node.js 的了解有点糟糕。但是,我做错了什么?为什么我在网络浏览器窗口中看不到“e=mc^2”的 mathjax 排版?

更新

好的,我纠正了。它在命令行中工作如下:

C:\mydemo> node .\lib\main.js

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block" alttext="E = mc^2">
<mi>E</mi>
<mo>=</mo>
<mi>m</mi>
<msup>
<mi>c</mi>
<mn>2</mn>
</msup>
</math>

我的问题是如何让这个 javascript 设置在 Web 浏览器中显示为可以放在我的网站上的 Web 应用程序?而不是使用“node”命令从 Windows 命令行运行它?

然后还有另一个问题,如果我将 Node 脚本生成的 mathml 代码废弃到 html 文档中,它仍然没有使用正确的类型设置来显示“e=mc^2”。示例:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block" alttext="E = mc^2">
<mi>E</mi>
<mo>=</mo>
<mi>m</mi>
<msup>
<mi>c</mi>
<mn>2</mn>
</msup>
</math>

</body>
</html>

enter image description here

看到格式很丑......它甚至没有将“2”变成上标或其他 latex 好的排版......

最佳答案

更简单...将其保存在 mathjax 中:

第 1 步:

mkdir demo1
cd demo1

第 2 步:将 mathjax javascript 库的本地副本下载到 demo1 目录

npm i mathjax

第 3 步:

在demo1目录下创建index.html文件:

<!DOCTYPE html>
<html>
<head>

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]}
});
</script>
<script type="text/javascript" src="./node_modules/mathjax/MathJax.js?config=TeX-AMS_HTML-full"></script>

</head>
<body>

<p>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</p>

</body>
</html>

关于node.js - 使用 mathjax 和 node.js 在网络浏览器中显示 latex 数学方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55973971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com