|
1 | | -// Load this BEFORE the MathJax CDN script in mkdocs.yml |
| 1 | +// MathJax v3 configuration for MkDocs Material |
| 2 | +// Place this file BEFORE the MathJax CDN script in mkdocs.yml |
| 3 | + |
2 | 4 | window.MathJax = { |
3 | 5 | tex: { |
4 | | - // include the delimiters you actually use: |
5 | | - inlineMath: [['\\(', '\\)'], ['$', '$']], |
6 | | - displayMath: [['\\[', '\\]'], ['$$', '$$']], |
| 6 | + inlineMath: [['\\(', '\\)'], ['$', '$']], // inline math |
| 7 | + displayMath: [['\\[', '\\]'], ['$$', '$$']], // display math |
7 | 8 | processEscapes: true, |
8 | 9 | processEnvironments: true |
9 | 10 | }, |
10 | 11 | options: { |
11 | | - // With arithmatex(generic:true), content is wrapped in .arithmatex |
12 | | - processHtmlClass: 'arithmatex', |
13 | | - ignoreHtmlClass: '.*' // process only .arithmatex |
| 12 | + processHtmlClass: 'arithmatex', // only process .arithmatex spans |
| 13 | + ignoreHtmlClass: '.*' |
14 | 14 | }, |
15 | 15 | startup: { |
16 | | - // we'll control typesetting manually to avoid races |
17 | | - typeset: false |
| 16 | + typeset: false // disable auto-typeset; we'll call it ourselves |
18 | 17 | } |
19 | 18 | }; |
20 | 19 |
|
21 | | -// Re-typeset after every MkDocs Material page swap |
| 20 | +// Hook into MkDocs Material instant navigation |
22 | 21 | if (window.document$) { |
23 | 22 | document$.subscribe(() => { |
24 | | - const container = document.querySelector('.md-content'); // main article |
25 | | - // Defer to next animation frame so DOM is fully replaced |
26 | | - requestAnimationFrame(() => { |
27 | | - MathJax.typesetClear(); // clear previous MathItems |
28 | | - MathJax.typesetPromise([container]); // typeset current page |
29 | | - }); |
| 23 | + const container = document.querySelector('.md-content'); |
| 24 | + if (window.MathJax) { |
| 25 | + // Ensure MathJax has finished startup before typesetting |
| 26 | + MathJax.startup.promise |
| 27 | + .then(() => MathJax.typesetPromise([container])) |
| 28 | + .catch(err => |
| 29 | + console.error("MathJax typeset failed: " + err.message) |
| 30 | + ); |
| 31 | + } |
30 | 32 | }); |
31 | 33 | } |
0 commit comments