I'm receiving data (containing LaTeX code) from an http request and I'd like to render it with MathJax. However, it seems like MathJax starts rendering the code before the data is actually received. To reproduce the bug, I've simulated the HTTP request using a setTimeout of 500ms. Here's an example showing what Im trying to do:
HTML
<html>
<head>
<title>MathJax Test</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script>
MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']]
}
}
</script>
<script type="text/javascript" id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
</script>
</head>
<body>
<div id="div1">
<!-- JS fills this div-->
</div>
</body>
</html>
JS
window.addEventListener('DOMContentLoaded', (event) => {
var para = document.createElement("p");
var element = document.getElementById("div1");
// Load the element after a delay of 500 ms
setTimeout(() => {
para.innerText = '$x=x^2+1$'
element.appendChild(para)
}, 500)
})
Here's a JSFiddle with the code above: https://jsfiddle.net/0uo5fhw9/100/
As you can see in the JSFiddle, the LaTeX is not rendering. How do I fix this and tell MathJax to render once all the data has been loaded?