电子应用程序中的 Mathjax

Mathjax in an electron application

我想在应用程序启动时加载 Mathjax 一次,之后它的行为应该像网站上的脚本标签和 "translate" 任何 MathML、TeX 或 ASCIImath "text" 转换成人类可读的内容。

我试过 mathjax-electron and mathjax-node 但我根本无法让它工作。有人可以举例说明如何实施吗?

我使用了 mathjax-electron 的 readme.md 示例:

var mathjaxHelper = require('mathjax-electron')

var container = document.createElement('div')
container.innerHTML = '$$\sum\limits_{i=0}^{\infty} \frac{1}{n^2}$$'

mathjaxHelper.loadAndTypeset(document, container)

但它导致 undefined 错误被抛出。我还尝试实现 mathjax-node 提供的示例,但我根本无法让它工作。

I tried mathjax-electron and mathjax-node but I couldn't get it working at all. Can someone give an example on how to implement it?

当然可以。使用 mathjax-electron:

mkdir mathjax-test
cd mathjax-test
npm init -y
npm i -s electron mathjax-electron

然后创建两个文件:index.jsindex.html

index.js(从这里借来的 - Electron hello world

const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow

let mainWindow

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600})
  mainWindow.loadURL(`file://${__dirname}/index.html`)
  mainWindow.webContents.openDevTools()

  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
})

index.html(使用the first example from their homepage

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Mathjax</title>
    <script src="./node_modules/mathjax-electron/resources/MathJax/MathJax.js?config=electron"></script>
  </head>
  <body>
    <h1>MathJax</h1>

    <script>
      var mathjaxHelper = require('mathjax-electron')
      var container = document.createElement('div')
      container.innerHTML = '$$\sum\limits_{i=0}^{\infty} \frac{1}{n^2}$$'
      mathjaxHelper.typesetMath(container)
      document.querySelector('body').append(container)
    </script>
  </body>
</html>

然后从项目的根目录开始:

./node_modules/electron/dist/electron .

结果:

HTH.