简体   繁体   English

MathJax 未定义

[英]MathJax is not defined

I have successfully loaded and configured MathJax by pasting MathJax recommended commands into the <head> element of my webpage.通过将 MathJax 推荐的命令粘贴到我网页的<head>元素中,我已成功加载和配置了 MathJax。 MathJax does a great job rendering LaTex in the <body> of my webpage. MathJax 在我网页的<body>中渲染 LaTex 做得很好。

My problem is with accessing the MathJax object with javascript run at the very end of the <body> element.我的问题是使用<body>元素最后运行的 javascript 访问MathJax对象。 For instance, the code below returns undefined to the console.例如,下面的代码将undefined返回到控制台。 This is confusing to me- shouldn't my commands in the <head> element have created MathJax as a global variable that can be accessed anywhere?这让我感到困惑 - 我在<head>元素中的命令不应该将MathJax创建为可以在任何地方访问的全局变量吗?

Here's a screenshot of my code:这是我的代码的屏幕截图: 代码截图

Here is the relevant section of code from the <head> element:这是来自<head>元素的相关代码部分:

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

Here is the relevant section of code from the <body> element:这是来自<body>元素的相关代码部分:

<h1 class="header">
    If $ax^2+bx+c=0$, then $x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$.
</h1>

<script type="text/javascript" >
    console.log(window.MathJax);
</script>

You are loading MathJax.js with the async attribute .您正在使用async属性加载MathJax.js Therefore, the browser does not block rendering until the script has loaded but instead continues to process the page.因此,浏览器在脚本加载之前不会阻止渲染,而是继续处理页面。 So now you have a race between MathJax.js getting fetched and loaded vs the browser's parser getting to the script tag further down in the page.因此,现在您在获取和加载MathJax.js与浏览器的解析器获取页面中更下方的script标记之间MathJax.js竞争。 Inevitably, the parser wins and executes the log before MathJax.js has finished loading.不可避免地,解析器会在MathJax.js完成加载之前获胜并执行日志。 Accordingly, there is no global MathJax object.因此,没有全局MathJax对象。

If you remove async , then the browser will first execute MathJax.js before parsing the remaining page and thus the console.log will provide something meaningful.如果删除async ,则浏览器将在解析剩余页面之前首先执行MathJax.js ,因此console.log将提供一些有意义的信息。 (Similarly, on older browsers which do not support the async attribute, you will get a result.) (同样,在不支持async属性的旧浏览器上,您会得到一个结果。)

Note, however, that MathJax is highly asynchronous in itself (loading additional components, configurations etc) so you will most likely need to synchronize your code with MathJax's APIs .但是请注意,MathJax 本身是高度异步的(加载附加组件、配置等),因此您很可能需要将您的代码与 MathJax 的 API 同步

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM