window.MathJax 未定义。

huangapple go评论67阅读模式
英文:

window.MathJax is undefined?

问题

你好,我有一个关于MathJax库的问题。我想在屏幕上显示MathJax公式,但是当我使用window.MathJax时,我收到一个未定义的错误。以下是我在html文件中安装MathJax的方式:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Test</title>
    <link rel="icon" href="/icons/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="/icons/favicon.ico" type="image/x-icon">
    <!-- inject:css -->
    <!-- endinject -->
    <script type="text/x-mathjax-config">
        MathJax = {
          options: {
            renderActions: {
              addMenu: []
            }
          },
        };
        MathJax.Hub.Config({
          tex2jax: {
            inlineMath: [ ['$', '$'], ["\\(", "\\)"] ],
            processEscapes: true
          }
        });
    </script>
    <script async type="text/javascript" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-svg.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
</head>
<body>
    <div id="application-content">
    </div>
</body>
<!-- inject:js -->
<!-- endinject -->
<script type="application/javascript">
    EMBED.default.init();
</script>
</html>

以下是我使用该库的组件:

import React, { Component } from 'react';

export default class MathBlock extends Component {
    constructor(props) {
        super(props);

        this.state = {
            open: false,
        };
    }

    render() {
        const text = this.props.block.getText();
        const latexRegex = /${2}(.*?)${2}/;
        const hasLatex = latexRegex.test(this.props.block.getText());

        return (
            <div>
                <div
                    dangerouslySetInnerHTML={{
                        __html: hasLatex
              ? window.MathJax.tex2svg(text.replaceAll('$', '')).innerHTML
              : window.MathJax.mathml2svg(text).innerHTML,
                    }}
                />
            </div>
        );
    }
}

MathBlock.propTypes = {
    block: React.PropTypes.object.isRequired,
};

有人知道这里的问题是什么吗?
英文:

Hi i Have a problem with MathJax library. I want to display the mathjax formula on the screen, but when I use window.MathJax I get the error that it is undefined. Here is how I installed the MathJax in my html file:

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head lang=&quot;en&quot;&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;link rel=&quot;icon&quot; href=&quot;/icons/favicon.ico&quot; type=&quot;image/x-icon&quot;&gt;
    &lt;link rel=&quot;shortcut icon&quot; href=&quot;/icons/favicon.ico&quot; type=&quot;image/x-icon&quot;&gt;
    &lt;!-- inject:css --&gt;
    &lt;!-- endinject --&gt;
    &lt;script type=&quot;text/x-mathjax-config&quot;&gt;
        MathJax = {
          options: {
            renderActions: {
              addMenu: []
            }
          },
        };
        MathJax.Hub.Config({
          tex2jax: {
            inlineMath: [ [&#39;$&#39;,&#39;$&#39;], [&quot;\\(&quot;,&quot;\\)&quot;] ],
            processEscapes: true
          }
        });
    &lt;/script&gt;
    &lt;script async type=&quot;text/javascript&quot; src=&quot;https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-svg.js&quot;&gt;&lt;/script&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot;/&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;application-content&quot;&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;!-- inject:js --&gt;
&lt;!-- endinject --&gt;
&lt;script type=&quot;application/javascript&quot;&gt;
    EMBED.default.init();
&lt;/script&gt;
&lt;/html&gt;

And here is the component where I use the library:

import React, { Component } from &#39;react&#39;;

export default class MathBlock extends Component {
    constructor(props) {
        super(props);

        this.state = {
            open: false,
        };
    }

    render() {
        const text = this.props.block.getText();
        const latexRegex = /${2}(.*?)${2}/;
        const hasLatex = latexRegex.test(this.props.block.getText());

        return (
            &lt;div&gt;
                &lt;div
                    dangerouslySetInnerHTML={{
                        __html: hasLatex
              ? window.MathJax.tex2svg(text.replaceAll(&#39;$&#39;, &#39;&#39;)).innerHTML
              : window.MathJax.mathml2svg(text).innerHTML,
                    }}
                /&gt;
            &lt;/div&gt;
        );
    }
}

MathBlock.propTypes = {
    block: React.PropTypes.object.isRequired,
};

Does anyone know what is the problem here?

答案1

得分: 1

由于加载 MathJax 的脚本标签具有 async 属性,当您的 EMBED.default.init(); 命令运行时,可能会在 MathJax 加载之前执行,因此在 window.MathJax 被定义之前执行。

您可以选择删除 async 属性(这将意味着您的页面必须等待 MathJax 加载和编译,然后才能继续处理页面的其余部分,从而减慢页面的初始加载速度),或者将 EMBED.default.init(); 放在 MathJax 的启动 ready() 函数中,以便它在 MathJax 加载后再执行。

您正在加载 MathJax 版本 3,但您当前的配置似乎是 v2 和 v3 配置的混合,目前 MathJax 完全忽略了它。

您可以尝试使用以下代码片段替代当前的配置脚本,以查看是否可以避免这个问题:

<script>
MathJax = {
  options: {
    renderActions: {
      addMenu: []
    }
  },
  tex: {
    inlineMath: [ ['$','$'], ["\\(","\\)"] ],
    processEscapes: true
  },
  startup: {
    ready() {
      MathJax.startup.defaultReady();
      EMBED.default.init();
    }
  }
};
</script>

(这是正确的 v3 配置)。

英文:

Because the script tag that loads MathJax has the async attribute, it may not be loaded when your EMBED.default.init(); command likely will run before MathJax is loaded, and so before window.MathJax has been defined.

You could either remove the async attribute (which will mean your page will have to wait for MathJax to load and compile before the rest of the page is processed, slowing down your initial view of the page), or you could put the EMBED.default.init(); in MathJax's startup ready() function so that it is not performed until MathJax is loaded.

You are loading MathJax version 3, but your current configuration seems to be a mix of v2 and v3 configurations, and it is currently being ignored entirely by MathJax.

You could use

&lt;script&gt;
MathJax = {
  options: {
    renderActions: {
      addMenu: []
    }
  },
  tex: {
    inlineMath: [ [&#39;$&#39;,&#39;$&#39;], [&quot;\\(&quot;,&quot;\\)&quot;] ],
    processEscapes: true
  },
  startup: {
    ready() {
      MathJax.startup.defaultReady();
      EMBED.default.init();
    }
  }
};
&lt;/script&gt;

(a correct v3 configuration) in place of your current configuration script, and see if that avoids the problem.

huangapple
  • 本文由 发表于 2023年2月14日 21:38:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定