|
45 | 45 | if (ch === '_' && (i===0 || inner[i-1] !== '\\')){ out += '\\\\_'; } |
46 | 46 | else { out += ch; } |
47 | 47 | } |
48 | | - return '\\\\text{' + out + '}'; |
| 48 | + return '\\text{' + out + '}'; |
49 | 49 | }); |
50 | 50 | } |
51 | 51 | function rebuildWithKatex(el){ |
52 | 52 | if (!window.katex) return; |
53 | 53 | var txt = el.textContent; |
54 | 54 | if (!(/[\$]/.test(txt) || /\\\(|\\\)|\\\[|\\\]/.test(txt))) return; |
55 | 55 | var tokens = tokenizeMath(txt); |
56 | | - if (!tokens.some(function(x){return x.t==='m';})) return; |
| 56 | + var hasMath = tokens.some(function(x){return x.t==='m';}); |
| 57 | + if (!hasMath) { |
| 58 | + var trimmed = txt.trim(); |
| 59 | + // If content uses double backslashes (e.g., \\text), normalise to single backslash for KaTeX |
| 60 | + var normalised = trimmed.replace(/\\\\/g, "\\"); |
| 61 | + if (/\\\\?[a-zA-Z]+/.test(trimmed) || /\\[a-zA-Z]+/.test(normalised)) { |
| 62 | + // Try to render whole node as inline math when it looks like TeX but lacks delimiters |
| 63 | + try { |
| 64 | + var span = document.createElement('span'); |
| 65 | + window.katex.render(escapeUnderscoresInTextCommand(normalised), span, {displayMode:false, strict:'ignore'}); |
| 66 | + while (el.firstChild) el.removeChild(el.firstChild); |
| 67 | + el.appendChild(span); |
| 68 | + el.setAttribute('data-katex-rebuilt','1'); |
| 69 | + } catch (_) {} |
| 70 | + } |
| 71 | + return; |
| 72 | + } |
57 | 73 | while (el.firstChild) el.removeChild(el.firstChild); |
58 | 74 | tokens.forEach(function(tok){ |
59 | 75 | if (tok.t==='t'){ |
60 | 76 | el.appendChild(document.createTextNode(tok.s)); |
61 | 77 | } else if (tok.t==='m'){ |
62 | 78 | var content = escapeUnderscoresInTextCommand(tok.s); |
63 | 79 | var span = document.createElement('span'); |
64 | | - try { window.katex.render(content, span, {displayMode: tok.d}); } catch(_) { span.textContent = tok.s; } |
| 80 | + try { window.katex.render(content, span, {displayMode: tok.d, strict: "ignore"}); } catch(_) { span.textContent = tok.s; } |
65 | 81 | el.appendChild(span); |
66 | 82 | } |
67 | 83 | }); |
|
77 | 93 | {left: "\\[", right: "\\]", display: true} |
78 | 94 | ], |
79 | 95 | ignoredTags: ["script","noscript","style","textarea","pre","code","option"], |
| 96 | + preProcess: function (s) { return escapeUnderscoresInTextCommand(s); }, |
| 97 | + errorCallback: function () { /* suppress autorender errors; fallback will handle */ }, |
| 98 | + strict: "ignore", |
80 | 99 | }); |
81 | 100 | } catch (_) {} |
82 | 101 | } |
|
0 commit comments