From de9ba5be5e73661549ef326b78f359c11ead6a17 Mon Sep 17 00:00:00 2001 From: Andrew Scholer Date: Fri, 3 Apr 2026 12:22:35 -0700 Subject: [PATCH 1/4] HTML: add MathJax 4 configuration module (PR #2180) --- js/mathjax_startup.js | 156 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 js/mathjax_startup.js diff --git a/js/mathjax_startup.js b/js/mathjax_startup.js new file mode 100644 index 000000000..d253ab0f4 --- /dev/null +++ b/js/mathjax_startup.js @@ -0,0 +1,156 @@ +/*************************************************************** + * Implements startup of MathJax v4 + ***************************************************************/ + +// Base config options. Will be supplemented by optional parts later +let mathJaxOpts = { + "tex": { + "inlineMath": [ + [ + "\\(", + "\\)" + ] + ], + "tags": "none", + "tagSide": "right", + "tagIndent": ".8em", + "packages": { + "[+]": [ + "base", + "ams", + "amscd", + "color", + "newcommand", + "knowl" + ] + } + }, + "options": { + "ignoreHtmlClass": "tex2jax_ignore|ignore-math", + "processHtmlClass": "process-math", + }, + "chtml": { + "scale": 0.98, + "mtextInheritFont": true + }, + "loader": { + "load": [ + //"ui/lazy", // Vastly speeds up time to visible content. But breaks embedded knowl parsing + "input/asciimath", + //"[tex]/extpfeil", //remove backwards compatibility? + "[tex]/amscd", + "[tex]/color", + "[tex]/newcommand", + ], + "paths": { + "pretext": "_static/pretext/js/lib" + } + } +}; + + +export function startMathJax(opts) { + if(opts.hasWebworkReps || opts.hasSage) { + mathJaxOpts['renderActions'] = { + "findScript": [ + 10, + function (doc) { + document.querySelectorAll('script[type^="math/tex"]').forEach(function (node) { + var display = !!node.type.match(/; *mode=display/); + var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display); + var text = document.createTextNode(''); + node.parentNode.replaceChild(text, node); + math.start = { node: text, delim: '', n: 0 }; + math.end = { node: text, delim: '', n: 0 }; + doc.math.push(math); + }); + }, + "" + ] + } + } + + if(opts.isReact) { + mathJaxOpts['startup'] = { + typeset: false, + } + } else { + mathJaxOpts['startup'] = { + ready() { + const { Configuration } = MathJax._.input.tex.Configuration; + const configuration = Configuration.create("knowl", { + handler: { + macro: ["knowl"] + } + }); + + function GetArgumentMML(parser, name) { + const NodeUtil = MathJax._.input.tex.NodeUtil.default; + const arg = parser.ParseArg(name); + if (!NodeUtil.isInferred(arg)) { + return arg; + } + const children = NodeUtil.getChildren(arg); + if (children.length === 1) { + return children[0]; + } + const mrow = parser.create("node", "mrow"); + NodeUtil.copyChildren(arg, mrow); + NodeUtil.copyAttributes(arg, mrow); + return mrow; + }; + + let mathjaxKnowl = {}; + /** + * Implements \knowl{url}{math} + * @param {TexParser} parser The calling parser. + * @param {string} name The TeX string + */ + mathjaxKnowl.Knowl = function (parser, name) { + const url = parser.GetArgument(name); + const arg = GetArgumentMML(parser, name); + const mrow = parser.create("node", "mrow", [arg], { tabindex: '0', "data-knowl": url }); + parser.Push(mrow); + }; + + const CommandMap = MathJax._.input.tex.TokenMap.CommandMap; + new CommandMap( + "knowl", + { + knowl: ["Knowl"] + }, + mathjaxKnowl + ); + + MathJax.startup.defaultReady(); + }, + pageReady() { + return MathJax.startup.defaultPageReady().then(function () { + rsMathReady(); + } + ) + }, + } + } + + if(opts.htmlPresentation) { + mathJaxOpts['options']['menuOptions'] = { + "settings": { + "zoom": "Click", + "zscale": "300%" + } + } + } + + // Apply the options + window.MathJax = mathJaxOpts; + + // Lets Runestone know that MathJax is ready + const runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve); + window.runestoneMathReady = runestoneMathReady; +} + + + + + From fd8e444f62440a888ac51c8fbc9a8171f95ab63f Mon Sep 17 00:00:00 2001 From: Rob Beezer Date: Sat, 4 Apr 2026 09:22:32 -0700 Subject: [PATCH 2/4] HTML: switch to MathJax 4 for browser rendering Co-Authored-By: Claude Opus 4.6 --- js/mathjax_startup.js | 11 +-- xsl/pretext-html.xsl | 154 +++++--------------------------------- xsl/pretext-runestone.xsl | 2 +- 3 files changed, 19 insertions(+), 148 deletions(-) diff --git a/js/mathjax_startup.js b/js/mathjax_startup.js index d253ab0f4..cc686cb43 100644 --- a/js/mathjax_startup.js +++ b/js/mathjax_startup.js @@ -35,16 +35,11 @@ let mathJaxOpts = { }, "loader": { "load": [ - //"ui/lazy", // Vastly speeds up time to visible content. But breaks embedded knowl parsing "input/asciimath", - //"[tex]/extpfeil", //remove backwards compatibility? "[tex]/amscd", "[tex]/color", "[tex]/newcommand", - ], - "paths": { - "pretext": "_static/pretext/js/lib" - } + ] } }; @@ -150,7 +145,3 @@ export function startMathJax(opts) { window.runestoneMathReady = runestoneMathReady; } - - - - diff --git a/xsl/pretext-html.xsl b/xsl/pretext-html.xsl index de8ab29a5..92e53e951 100644 --- a/xsl/pretext-html.xsl +++ b/xsl/pretext-html.xsl @@ -13163,152 +13163,32 @@ TODO: - - - - var runestoneMathReady = new Promise((resolve) => window.rsMathReady = resolve); - window.MathJax = - - - - - - - \( - \) - - - none - right - .8em - - - base - ams - amscd - color - newcommand - - textmacros - knowl - - - - - tex2jax_ignore|ignore-math - process-math - - - - 10 - - function (doc) { - document.querySelectorAll('script[type^="math/tex"]').forEach(function(node) { - var display = !!node.type.match(/; *mode=display/); - var math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display); - var text = document.createTextNode(''); - node.parentNode.replaceChild(text, node); - math.start = {node: text, delim: '', n: 0}; - math.end = {node: text, delim: '', n: 0}; - doc.math.push(math); - }); - } - - - - - - - - 0.98 - true - - - - input/asciimath - [tex]/amscd - [tex]/color - [tex]/newcommand - - [tex]/textmacros - [pretext]/mathjaxknowl3.js - - - - - - - - - - - false - - - - - pageReady() { - return MathJax.startup.defaultPageReady().then(function () { - console.log("in ready function"); - rsMathReady(); - } - )} - - - - - - - - - - Click - 300% - - - - - - - - ; - - - - - - - - - text/javascript - - + + + + diff --git a/xsl/pretext-runestone.xsl b/xsl/pretext-runestone.xsl index 389371356..8b2e68477 100644 --- a/xsl/pretext-runestone.xsl +++ b/xsl/pretext-runestone.xsl @@ -225,7 +225,7 @@ along with PreTeXt. If not, see . *** Runestone Services *** -