// Minimal LaTeX -> HTML converter for the pandoc-generated paper source. // Math is passed through untouched (HTML-escaped) for MathJax to typeset. (function (root) { 'use strict'; var ESC = { '&': '&', '<': '<', '>': '>', '"': '"' }; function esc(s) { return s.replace(/[&<>"]/g, function (c) { return ESC[c]; }); } var THM = /^(Theorem|Lemma|Proposition|Corollary|Definition|Remark|Problem|Conjecture|Example|Algorithm|Assumption|Claim|Notation)\b/; // Index of the matching close brace for the '{' at position i. function matchBrace(s, i) { var depth = 0; for (var j = i; j < s.length; j++) { var c = s[j]; if (c === '\\') { j++; continue; } if (c === '{') depth++; else if (c === '}') { depth--; if (depth === 0) return j; } } return -1; } var ACCENT = { "'": '́', '`': '̀', '^': '̂', '"': '̈', '~': '̃', 'c': '̧' }; function inline(s) { s = s.replace(/\{\[\}/g, '[').replace(/\{\]\}/g, ']'); // accents like \'e or \'{e} s = s.replace(/\\(['`^"~])\{?([A-Za-z])\}?/g, function (_, a, ch) { return (ch + ACCENT[a]).normalize('NFC'); }); var out = '', i = 0; while (i < s.length) { var c = s[i]; if (c === '\\') { var m = /^\\([A-Za-z]+)\*?\s*/.exec(s.slice(i)); if (!m) { var nx = s[i + 1]; if (nx === '&') { out += '&'; i += 6; continue; } // "\&" after escaping if (nx === ' ') { out += ' '; i += 2; continue; } if (nx === '\\') { out += '
'; i += 2; continue; } out += nx || ''; i += 2; continue; } var name = m[1]; i += m[0].length; var arg = null; if (s[i] === '{') { var e = matchBrace(s, i); if (e > 0) { arg = s.slice(i + 1, e); i = e + 1; } } switch (name) { case 'emph': case 'textit': out += '' + inline(arg || '') + ''; break; case 'textbf': out += '' + inline(arg || '') + ''; break; case 'nolinkurl': case 'texttt': case 'url': out += '' + (arg || '') + ''; break; case 'textup': case 'textrm': case 'mbox': out += inline(arg || ''); break; case 'S': out += '§'; if (arg !== null) out += inline(arg); break; case 'ldots': case 'dots': out += '…'; break; case 'label': case 'raggedright': case 'noindent': case 'tightlist': break; default: if (arg !== null) out += inline(arg); } continue; } if (c === '{' || c === '}') { i++; continue; } out += c; i++; } return out .replace(/---/g, '—').replace(/--/g, '–') .replace(/``/g, '“').replace(/''/g, '”') .replace(/~/g, ' '); } function convert(tex) { var b = tex.indexOf('\\begin{document}'), e = tex.indexOf('\\end{document}'); var body = tex.slice(b >= 0 ? b + 16 : 0, e >= 0 ? e : tex.length); // TeX comments: unescaped % eats the rest of the line and the newline. body = body.replace(/(^|[^\\])%[^\n]*\n?/g, '$1'); var math = []; function stash(open, close) { return function (_, m) { math.push(open + esc(m) + close); return '' + (math.length - 1) + ''; }; } body = body.replace(/\\\[([\s\S]*?)\\\]/g, stash('\\[', '\\]')); body = body.replace(/\\\(([\s\S]*?)\\\)/g, stash('\\(', '\\)')); body = esc(body); var blocks = []; function block(html) { blocks.push(html); return '\n\n' + (blocks.length - 1) + '\n\n'; } var toc = []; body = body.replace(/\\hypertarget\{[^}]*\}\{\s*\\(sub)?section\{([\s\S]*?)\}\\label\{([^}]*)\}\}/g, function (_, sub, title, id) { title = inline(title.replace(/\s+/g, ' ').trim()); toc.push({ id: id, title: title, sub: !!sub }); return block('' + title + ''); }); body = body.replace(/\\(sub)?section\{([\s\S]*?)\}\\label\{([^}]*)\}\}?/g, function (_, sub, title, id) { title = inline(title.replace(/\s+/g, ' ').trim()); toc.push({ id: id, title: title, sub: !!sub }); return block('' + title + ''); }); body = body.replace(/\\begin\{longtable\}[\s\S]*?\\end\{longtable\}/g, function (t) { var head = [], rows = []; var hm = /\\toprule\\noalign\{\}([\s\S]*?)\\\\\s*\\midrule/.exec(t); if (hm) head = hm[1].split('&').map(function (c) { return inline(c.replace(/\\begin\{minipage\}(\[[^\]]*\])?\{[^}]*\}|\\end\{minipage\}/g, '').trim()); }); var rb = t.indexOf('\\endlastfoot'), re = t.lastIndexOf('\\end{longtable}'); t.slice(rb + 12, re).split(/\\\\\s*/).forEach(function (r) { if (r.trim()) rows.push(r.split('&').map(function (c) { return inline(c.trim()); })); }); var h = '
' + head.map(function (c) { return ''; }).join('') + '' + rows.map(function (r) { return '' + r.map(function (c) { return ''; }).join('') + ''; }).join('') + '
' + c + '
' + c + '
'; return block(h); }); body = body.replace(/\\maketitle/g, '') .replace(/\\begin\{abstract\}/g, block('

Abstract

')) .replace(/\\end\{abstract\}/g, block('
')); var html = body.split(/\n\s*\n/).map(function (p) { p = p.trim(); if (!p) return ''; var bm = /^(\d+)$/.exec(p); if (bm) return blocks[+bm[1]]; var h = inline(p.replace(/\s*\n\s*/g, ' ')); var cls = THM.test(h) ? ' class="thm"' : /^Proof/.test(h) ? ' class="proof"' : ''; return '' + h + '

'; }).join('\n'); html = html.replace(/(\d+)/g, function (_, n) { return math[+n]; }); toc = toc.map(function (t) { t.title = t.title.replace(/(\d+)/g, function (_, n) { return math[+n]; }); return t; }); return { html: html, toc: toc }; } root.tex2html = convert; if (typeof module !== 'undefined') module.exports = convert; })(typeof window !== 'undefined' ? window : globalThis);