简体   繁体   English

将作为mathjax命令的javascript变量插入到输出格式化的数学内容的html页面中

[英]Inserting a javascript variable that is a mathjax command into a html page that outputs formatted maths

I have a variable in javascript that depends on an input from 我在javascript中有一个变量,该变量取决于来自

<input type="text" id="inputfunction" value="">
<button onclick="example">Example</button>
<p id="input"></p>

In the js script I have used document.getElementById("input").innerHTML = inpt; 在js脚本中,我使用了document.getElementById("input").innerHTML = inpt; to place this variable on the page. 将此变量放置在页面上。

The variable comes out similar to say '3x+8' however I want this to be formatted with mathjax. 变量出来类似于“ 3x + 8”,但是我希望使用mathjax对其进行格式化。 To facilitate this I currently have 为此,我目前有

  <head>
        <link rel='stylesheet' type='text/css' href='Stylesheet1.css'/>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width">
        <script type="text/javascript" async
                src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML">
        </script>
    </head>

in my html document, however this doesn't output the variable formatted. 在我的HTML文档中,但是这不会输出格式化的变量。 I have tried using document.getElementById("input").innerHTML = \\\\(inpt\\\\); 我尝试使用document.getElementById("input").innerHTML = \\\\(inpt\\\\); or placing $$ on either side of it however this just puts it on the page as \\(inpt\\) or $$inpt$$ . 或在其任一侧放置$$ ,但这只是将其放在\\(inpt\\)$$inpt$$ Is there any way to have it so that mathjax formats the variable? 有什么方法可以让mathjax格式化变量吗?

Okay so I eventually found it and it was a really easy fix. 好的,所以我最终找到了它,这真的很容易解决。 I just added 我刚刚添加

MathJax.Hub.Typeset();

at the end of the function and that makes MathJax look back over the page to format anything else. 该函数的末尾,这使得MathJax可以回顾页面以格式化其他内容。 Apparently you shouldn't call this directly though because MathJax may be performing other actions so to counteract any issues this may cause you can write 显然,您不应该直接调用此方法,因为MathJax可能正在执行其他操作,以便抵消可能导致您写的任何问题

MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

which makes sure MathJax has finished all other processes before typesetting the page stop any synchronisation errors. 这可以确保MathJax在排版页面之前已完成所有其他过程,以停止任何同步错误。

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

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