First, let me say that I know very little about javascript so please bear with me.
What I’m trying to do is take an existing script (inline to an html page) and convert it to an external js script that can then be called from any html page.
I started with an html from Davide P. Cervone that contains an inline script. (I’m not permitted to post a link here but I can post his entire code here if needed.) His script allows mathjax equations to be renumbered. This works fine in my html editor (Pinegrow) and in a browser.
I then saved Cervone’s script as number_equations_11b.js. (Note that Cervone’s script is from 2016 if this makes any difference.)
MathJax.Hub.Register.StartupHook("TeX AMSmath Ready",function () {
var sectionDefault = {name: "", num: 0};
var sections = {}, section = sectionDefault;
//
// The MathJax objects we need to modify
//
var TEX = MathJax.InputJax.TeX, PARSE = TEX.Parse;
var AMS = MathJax.Extension["TeX/AMSmath"];
//
// Add \section{n} and \seteqnumber{n} macros
//
TEX.Definitions.Add({
macros: {
section: "mySection",
seteqnumber: "mySetEqNumber"
}
});
PARSE.Augment({
//
// \section{n}
// Sets the current section. The n is used in tags and also in
// the anchor names. The equation numbers are set to 0 when the
// section is changed. If a section is reused, the numbers restart
// where we left off.
//
mySection: function (name) {
section.num = AMS.number;
var n = this.GetArgument(name);
if (n === "") {
section = sectionDefault;
} else {
if (!sections["_"+n]) sections["_"+n] = {name:n, num:0};
section = sections["_"+n];
}
AMS.number = section.num;
},
//
// \seteqnumber{n}
// sets the next equation number to be n (a positive integer).
//
mySetEqNumber: function (name) {
var n = this.GetArgument(name);
if (!n || !n.match(/^ *[0-9]+ *$/)) n = ""; else n = parseInt(n)-1;
if (n === "" || n < 1) TEX.Error("Argument to "+name+" should be a positive integer");
AMS.number = n;
}
});
//
// Configure the formatting of the tags and IDs. The IDs need to
// include the secetion number as well so that the equations in
// different sections get different anchors.
//
MathJax.Hub.Config({
TeX: {
equationNumbers: {
formatTag: function (n) {return "("+(section.name+"."+n).replace(/^\./,"")+")";},
// 5/Apr/2023 - the following is the original but gave syntax error without the ; before the final }
// Check this section alone in https://extendsclass.com/javascript-fiddle.html
// formatTag: function (n) {return "("+(section.name+"."+n).replace(/^\./,"")+")"},
formatID: function (n) {
n = (section.name+'.'+n).replace(/[:"'<>&]/g,"").replace(/^\./,"");
return 'mjx-eqn-' + n;
}
}
}
});
MathJax.Hub.Config({
TeX: {
equationNumbers: {
autoNumber: "AMS"
}
}
});
});
My calling html is –
<!DOCTYPE html>
<html>
<head>
<title>Equation numbers by section</title>
<!-- 2023-04-06 - original from below; must come before next script or no equation numbers ever -->
<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full"></script>
<!-- 2023-04-04 - includes full path -->
<script type="text/javascript" src="/_scripts/mathjax/number_equations_11b.js"></script>
</head>
<body>
<p>Demo --</p>
<p></p>
<p>Default numbering --</p>
\begin{align}
a+b &= c
\end{align}
<p>Start section E --</p>
<div style="display:none; position:absolute">
\(\section{E}\)
</div>
\begin{equation}
E=mc^2
\end{equation}
<p class="_eqn_latex notranslate">
\begin{align} \label{eq:10301a}
\widehat{U} = \frac{\overline{U}_{min}}{\overline{U}_{max}}
\end{align}
</p>
<p>Start section 1 --</p>
<div style="display:none; position:absolute">
\(\section{1}\)
</div>
\begin{align}
p+q &= r
\end{align}
\begin{align}
x+y &= z
\end{align}
<p>Back to default numbering --</p>
<div style="display:none; position:absolute">
<!-- back to the default numbering sequence -->
\(\section{}\)
</div>
\begin{align}
t+u &= v
\end{align}
</body>
</html>
The output should look like this –
When I repeatedly refresh Pinegrow’s wysiwyg window, sometimes the equation numbers appear and sometimes they don’t. When I preview the html in Microsoft Edge (using Pinegrow’s browser preview feature) the equation numbers never appear.
My newbie guess – the html that displays the equations executes before number_equations_11b.js can load. If so, I don’t know how to correct this.
I’ve been trying to resolve this for several days so I’d appreciate any suggestions. (Let me know if you need clarification or more info.)
Thanks,
Don C.