Here is how MathJax is loaded in my page:
-----------------
I have pointers/links to web content that may contain equations. When the link is clicked, the JS code that handles the event does this:
* It checks if the link points to an element in the DOM.
If the object is not in the DOM, it asks the server to fetch the element that is in the database. If so, the HTML code is returned, and the JS code inserts somewhere and typesets it using typesetPromise, and the math is rendered.
If the object is in the DOM, it does the same thing without querying the server. The problem that I cannot presently solve is that the math is not rendered even though the typeset promise is used.
To make things clearer, in both cases, the math has not been typeset previously. In the first case, this is because the code comes from the server. In the second case, this is because the equations were out of view and had not been typeset due to lazy loading. As far as I can tell, the promise code is the same in both cases.
The only difference between the two cases is that, in the second case, the math code already exists somewhere else on the page and it has not been typeset yet. The math code copy is in a different element with a different ID, but until now I have failed to render the math. Can this be due to the fact that MJ has put it aside for later processing due to lazy load?
When I call the typeset engine, the original math code is not in view, but the copy is. Can this confuse MathJax?
Help will be greatly appreciated.
Here is the incriminated code:
----------------------------------------------------------------------
let refContent = document.getElementById(ref);
if (refContent == null) {
axios.post(url, {
id: ref,
label: label,
})
.then(function (response) {
let promise = Promise.resolve();
function typeset(code) {
promise = promise.then(() => MathJax.typesetPromise(code()))
.catch((err) => console.log('Typeset failed: ' + err.message));
return promise;
}
typeset(() => {
let htmlCode = response.data.html;
let randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
let refHTML = '<div class="reference" data-controller="remove" data-action="click->remove#remove" data-ref="' + ref + '" id="'+randomString+'">' + htmlCode + '</div>';
if (tag.slice(0, 1) === 'H') {
div.innerHTML = refHTML + div.innerHTML;
} else {
div.innerHTML += refHTML;
}
let element = document.getElementById(randomString);
if (element) {
LatexDOM.reveal(element);
}
return [div];
});
})
.catch(function (error) {
console.log(error);
})
} else {
let promise = Promise.resolve();
function typeset(code) {
promise = promise.then(() => MathJax.typesetPromise(code()))
.catch((err) => console.log('Typeset failed: ' + err.message));
return promise;
}
typeset(() => {
let randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
let refHTML = '<div class="reference" data-controller="remove" data-action="click->remove#remove" data-ref="' + ref + '" id="'+randomString+'">';
let classes = refContent.classList;
if (classes.contains('section')) {
let tagOfRefContent = refContent.tagName.toLowerCase();
refHTML += '<' + tagOfRefContent + ' class="section" id="' + ref + '" data-controller="section" data-action="click->section#showHide">';
refHTML += refContent.innerHTML;
refHTML += '</' + tagOfRefContent + '>';
let refDiv = ref+'/div';
refHTML += '<div id="'+refDiv+'">';
let section = document.getElementById(refDiv);
if (section !== null) {
let sectionHTML = section.innerHTML;
refHTML += sectionHTML;
}
refHTML += '</div>';
} else {
refHTML += refContent.innerHTML;
}
refHTML += '</div>';
if (tag.slice(0, 1) === 'H') {
div.innerHTML = refHTML + div.innerHTML;
} else {
div.innerHTML += refHTML;
}
let element = document.getElementById(randomString);
if (element) {
LatexDOM.reveal(element);
}
});
}