// Render the paper TeX in the browser. Math is left for MathJax.
(function (root) {
'use strict';
var ESC = { '&': '&', '<': '<', '>': '>', '"': '"' };
function esc(s) {
return String(s).replace(/[&<>"]/g, function (c) { return ESC[c]; });
}
// Filled from the document preamble. Do not keep a stock macro set:
// a shorter name must not rewrite a longer command from another paper.
var MACROS = {};
var MACRO_RE = null;
var ENVS = {};
function rebuildMacroRe() {
var names = Object.keys(MACROS).sort(function (a, b) { return b.length - a.length; });
MACRO_RE = names.length
? new RegExp('\\\\(' + names.join('|') + ')(?![A-Za-z])', 'g')
: null;
}
function expandMacros(s) {
if (!MACRO_RE) return s;
return s.replace(MACRO_RE, function (m, name) {
var body = MACROS[name];
// A letter body glued to the previous control word (\in\Bo -> \inB).
if (body.charAt(0) !== '\\') return '{' + body + '}';
return body;
});
}
function expandInputs(src, files) {
files = files || {};
var guard = 0;
while (guard++ < 20) {
var m = /\\input\s*\{([^}]+)\}/.exec(src);
if (!m) return src;
var key = m[1].trim();
var body = files[key];
if (body == null && key.slice(-4) !== '.tex') body = files[key + '.tex'];
if (body == null) body = files[key.replace(/\.tex$/, '')];
if (body == null) throw new Error('missing input: ' + key);
src = src.slice(0, m.index) + '\n' + body + '\n' + src.slice(m.index + m[0].length);
}
throw new Error('too many \\\\input expansions');
}
function parseNewcommands(src) {
MACROS = {};
var re = /\\newcommand\s*\{\\([A-Za-z]+)\}/g;
var m;
while ((m = re.exec(src))) {
var p = m.index + m[0].length;
if (src[p] === '[') continue;
var body = readBrace(src, p);
MACROS[m[1]] = body.text;
re.lastIndex = body.end;
}
rebuildMacroRe();
}
function parseNewtheorems(preamble) {
ENVS = {};
var style = 'plain';
var i = 0;
while (i < preamble.length) {
var rest = preamble.slice(i);
var st = /\\theoremstyle\s*\{([^}]*)\}/.exec(rest);
var nt = /\\newtheorem\s*\{([A-Za-z]+)\}/.exec(rest);
var nextStyle = st ? st.index : -1;
var nextThm = nt ? nt.index : -1;
if (nextStyle < 0 && nextThm < 0) break;
if (nextThm < 0 || (nextStyle >= 0 && nextStyle < nextThm)) {
style = st[1].trim();
i += nextStyle + st[0].length;
continue;
}
var p = i + nextThm + nt[0].length;
var shared = null;
if (preamble[p] === '[') {
var sh = readBracket(preamble, p);
shared = sh.text.trim();
p = sh.end;
}
var title = readBrace(preamble, p);
p = title.end;
var within = null;
if (preamble[p] === '[') {
var w = readBracket(preamble, p);
within = w.text.trim();
p = w.end;
}
ENVS[nt[1]] = { title: title.text, shared: shared, within: shared ? null : within, style: style };
i = p;
}
}
function stripComments(s) {
var out = '';
for (var i = 0; i < s.length; i++) {
if (s[i] === '\\') {
out += s[i];
if (i + 1 < s.length) out += s[++i];
continue;
}
if (s[i] === '%') {
while (i < s.length && s[i] !== '\n') i++;
out += '\n';
continue;
}
out += s[i];
}
return out;
}
function readBrace(s, i) {
if (s[i] !== '{') throw new Error('expected { at ' + i + ': ' + JSON.stringify(s.slice(i, i + 60)));
var depth = 0;
for (var j = i; j < s.length; j++) {
if (s[j] === '\\') { j++; continue; }
if (s[j] === '{') depth++;
else if (s[j] === '}') {
depth--;
if (depth === 0) return { text: s.slice(i + 1, j), end: j + 1 };
}
}
throw new Error('unclosed { at ' + i);
}
function readBracket(s, i) {
if (s[i] !== '[') return null;
var depth = 0, brace = 0;
for (var j = i; j < s.length; j++) {
if (s[j] === '\\') { j++; continue; }
if (s[j] === '{') brace++;
else if (s[j] === '}') brace--;
else if (brace === 0 && s[j] === '[') depth++;
else if (brace === 0 && s[j] === ']') {
depth--;
if (depth === 0) return { text: s.slice(i + 1, j), end: j + 1 };
}
}
throw new Error('unclosed [ at ' + i);
}
function grab(s, name) {
var token = '\\' + name + '{';
var i = s.indexOf(token);
if (i < 0) return '';
return readBrace(s, i + token.length - 1).text;
}
function extractAll(src) {
var math = [];
var out = '';
var i = 0;
function push(info) {
var n = math.length;
math.push(info);
return '\uE000' + n + '\uE001';
}
while (i < src.length) {
if (src.startsWith('\\begin{equation', i)) {
var star = src.startsWith('\\begin{equation*}', i);
var open = star ? '\\begin{equation*}' : '\\begin{equation}';
var close = star ? '\\end{equation*}' : '\\end{equation}';
var start = i + open.length;
var end = src.indexOf(close, start);
if (end < 0) throw new Error('unclosed equation');
var body = src.slice(start, end);
var id = null;
body = body.replace(/\\label\{([^}]*)\}/, function (_, lab) { id = lab; return ''; });
out += push({ display: true, tex: expandMacros(body.trim()), id: id, numbered: !star });
i = end + close.length;
continue;
}
if (src.startsWith('\\[', i)) {
var endD = src.indexOf('\\]', i + 2);
if (endD < 0) throw new Error('unclosed \\[');
out += push({ display: true, tex: expandMacros(src.slice(i + 2, endD).trim()), id: null, numbered: false });
i = endD + 2;
continue;
}
if (src[i] === '\\') {
out += src[i];
if (i + 1 < src.length) out += src[++i];
i++;
continue;
}
if (src[i] === '$') {
var j = i + 1;
while (j < src.length) {
if (src[j] === '\\') { j += 2; continue; }
if (src[j] === '$') break;
j++;
}
if (j >= src.length) throw new Error('unclosed $');
out += push({ display: false, tex: expandMacros(src.slice(i + 1, j)), id: null, numbered: false });
i = j + 1;
continue;
}
out += src[i];
i++;
}
return { text: out, math: math };
}
function findEnd(s, i, name) {
var depth = 1;
var open = '\\begin{' + name + '}';
var close = '\\end{' + name + '}';
var j = i;
while (j < s.length) {
if (s.startsWith(open, j)) { depth++; j += open.length; continue; }
if (s.startsWith(close, j)) {
depth--;
if (depth === 0) return { body: s.slice(i, j), end: j + close.length };
j += close.length;
continue;
}
if (s[j] === '\\') { j += 2; continue; }
j++;
}
throw new Error('missing \\end{' + name + '}');
}
function readBegin(s, i) {
var m = /^\\begin\{([^}]+)\}/.exec(s.slice(i));
if (!m) throw new Error('bad begin at ' + i);
var j = i + m[0].length;
var opt = null;
if (s[j] === '[') {
var b = readBracket(s, j);
opt = b.text;
j = b.end;
}
return { name: m[1], opt: opt, next: j };
}
function takeLabel(s) {
var m = /^\s*\\label\{([^}]*)\}/.exec(s);
if (!m) return { id: null, rest: s };
return { id: m[1], rest: s.slice(m[0].length) };
}
function splitItems(s) {
var items = [];
var buf = '';
var env = 0;
var brace = 0;
var started = false;
function push() {
if (!started) return;
var raw = buf;
var label = null;
var t = raw.replace(/^\s+/, '');
if (t[0] === '[') {
var b = readBracket(t, 0);
label = b.text;
t = t.slice(b.end);
}
items.push({ label: label, text: t });
buf = '';
}
for (var i = 0; i < s.length; i++) {
if (s[i] === '\\' && env === 0 && brace === 0 && s.startsWith('\\item', i) && !/[a-zA-Z]/.test(s[i + 5] || '')) {
push();
started = true;
i += 4;
continue;
}
if (s.startsWith('\\begin{', i)) env++;
else if (s.startsWith('\\end{', i)) env--;
if (s[i] === '\\') {
buf += s[i];
if (i + 1 < s.length) buf += s[++i];
continue;
}
if (s[i] === '{') brace++;
else if (s[i] === '}') brace--;
buf += s[i];
}
push();
return items;
}
function splitRows(s) {
var rows = [];
var cur = '';
var brace = 0;
for (var i = 0; i < s.length; i++) {
if (s[i] === '\\' && s[i + 1] === '\\' && brace === 0) {
rows.push(cur);
cur = '';
i++;
continue;
}
if (s[i] === '\\') {
cur += s[i];
if (i + 1 < s.length) cur += s[++i];
continue;
}
if (s[i] === '{') brace++;
else if (s[i] === '}') brace--;
cur += s[i];
}
if (cur.trim()) rows.push(cur);
return rows;
}
function splitCells(s) {
var cells = [];
var cur = '';
var brace = 0;
for (var i = 0; i < s.length; i++) {
if (s[i] === '&' && brace === 0) {
cells.push(cur);
cur = '';
continue;
}
if (s[i] === '\\') {
cur += s[i];
if (i + 1 < s.length) cur += s[++i];
continue;
}
if (s[i] === '{') brace++;
else if (s[i] === '}') brace--;
cur += s[i];
}
cells.push(cur);
return cells;
}
function peelRules(t) {
var before = false;
var guard = 0;
while (guard++ < 8) {
var m = /^\s*\\(?:toprule|midrule|bottomrule|hline)\s*/.exec(t);
if (!m) break;
before = true;
t = t.slice(m[0].length);
}
return { before: before, text: t };
}
function parseTabular(body) {
var rows = [];
splitRows(body).forEach(function (raw) {
var t = raw.trim();
if (!t) return;
var peeled = peelRules(t);
if (!peeled.text) {
if (rows.length && peeled.before) rows[rows.length - 1].ruleAfter = true;
else if (peeled.before) rows.push({ ruleBefore: true, cells: [], ruleAfter: false });
return;
}
var rule = peeled.before;
t = peeled.text;
var cells = splitCells(t).map(function (c) {
var m = /^\\multicolumn\{(\d+)\}\{[^}]*\}\{([\s\S]*)\}$/.exec(c.trim());
if (m) return { span: +m[1], text: m[2] };
return { span: 1, text: c.trim() };
});
rows.push({ ruleBefore: rule, ruleAfter: false, cells: cells });
});
return rows;
}
function listKind(name, opt) {
if (name === 'itemize') return 'bullet';
if (name === 'description') return 'desc';
var spec = '';
if (opt) {
var m = /label\s*=\s*([^,\]]+)/.exec(opt);
if (m) spec = m[1].trim();
}
return spec || 'decimal';
}
function roman(n) {
var map = [[100, 'c'], [90, 'xc'], [50, 'l'], [40, 'xl'], [10, 'x'], [9, 'ix'], [5, 'v'], [4, 'iv'], [1, 'i']];
var s = '';
map.forEach(function (pair) {
while (n >= pair[0]) { s += pair[1]; n -= pair[0]; }
});
return s;
}
function formatMarker(spec, n) {
if (spec === 'decimal') return n + '.';
if (spec === 'bullet' || spec === 'desc') return '';
return spec
.replace(/\\alph\*/g, String.fromCharCode(96 + n))
.replace(/\\Alph\*/g, String.fromCharCode(64 + n))
.replace(/\\roman\*/g, roman(n))
.replace(/\\Roman\*/g, roman(n).toUpperCase())
.replace(/\\arabic\*/g, String(n));
}
function parseContent(s, depth) {
var blocks = [];
var buf = '';
var i = 0;
var pendingSize = '';
function flush() {
if (buf.trim()) blocks.push({ type: 'p', text: buf.trim() });
buf = '';
}
while (i < s.length) {
var start = i;
if (s[i] === '\n') {
var j = i + 1;
while (s[j] === ' ' || s[j] === '\t') j++;
if (s[j] === '\n') {
flush();
i = j + 1;
continue;
}
}
if (s.startsWith('\\begin{', i)) {
flush();
var b = readBegin(s, i);
var found = findEnd(s, b.next, b.name);
blocks.push(makeEnv(b.name, b.opt, found.body, depth, pendingSize));
pendingSize = '';
i = found.end;
if (i === start) throw new Error('env did not advance');
continue;
}
var sec = /^\\(subsubsection|subsection|section|paragraph|maketitle|tableofcontents|clearpage)\b\*?/.exec(s.slice(i));
if (sec) {
flush();
var name = sec[1];
var k = i + sec[0].length;
if (name === 'maketitle') blocks.push({ type: 'maketitle' });
else if (name === 'tableofcontents') blocks.push({ type: 'toc' });
else if (name === 'clearpage') { /* skip */ }
else if (name === 'paragraph') {
var title = readBrace(s, k);
buf = '\uE010' + title.text + '\uE011 ';
k = title.end;
} else {
var titleS = readBrace(s, k);
k = titleS.end;
var id = null;
var lab = /^\s*\\label\{([^}]*)\}/.exec(s.slice(k));
if (lab) { id = lab[1]; k = k + lab[0].length; }
var level = name === 'section' ? 1 : name === 'subsection' ? 2 : 3;
blocks.push({ type: 'section', level: level, title: titleS.text, id: id });
}
i = k;
if (i === start) throw new Error('command did not advance: ' + name);
continue;
}
if (s.startsWith('\\appendix', i) && !/[a-zA-Z]/.test(s[i + 9] || '')) {
flush();
blocks.push({ type: 'appendix' });
i += 9;
continue;
}
if (s.startsWith('\\bibliographystyle', i)) {
flush();
var bs = i + '\\bibliographystyle'.length;
if (s[bs] === '{') bs = readBrace(s, bs).end;
i = bs;
continue;
}
if (s.startsWith('\\small', i) && !/[a-zA-Z]/.test(s[i + 6] || '')) { pendingSize = 'small'; i += 6; continue; }
if (s.startsWith('\\footnotesize', i)) { pendingSize = 'footnotesize'; i += 13; continue; }
buf += s[i];
i++;
}
flush();
return blocks;
}
var ALGO_OPEN = { If: 'if ', For: 'for ', While: 'while ' };
var ALGO_CLOSE = { EndIf: 1, EndFor: 1, EndWhile: 1, EndProcedure: 1, EndFunction: 1 };
function readAlgoCmd(s, i) {
var m = /^\\([A-Za-z]+)\s*/.exec(s.slice(i));
if (!m) return null;
return { name: m[1], end: i + m[0].length };
}
function parseAlgo(body) {
var lines = [];
var indent = 0;
var i = 0;
function emit(kind, text, ind) {
lines.push({ kind: kind, text: text.trim(), indent: ind, comment: '' });
}
function restUntilCmd() {
var j = i;
while (j < body.length && body[j] !== '\\') j++;
var text = body.slice(i, j);
i = j;
return text;
}
while (i < body.length) {
while (i < body.length && /\s/.test(body[i])) i++;
if (i >= body.length) break;
if (body[i] !== '\\') {
var extra = restUntilCmd().trim();
if (extra && lines.length) lines[lines.length - 1].text += ' ' + extra;
continue;
}
var cmd = readAlgoCmd(body, i);
if (!cmd) { i++; continue; }
i = cmd.end;
var arg = '';
if (body[i] === '{') {
var b = readBrace(body, i);
arg = b.text;
i = b.end;
}
if (cmd.name === 'Comment') {
if (lines.length) lines[lines.length - 1].comment = arg;
continue;
}
if (ALGO_CLOSE[cmd.name]) {
indent = Math.max(0, indent - 1);
continue;
}
if (cmd.name === 'Else' || cmd.name === 'ElsIf') {
emit(cmd.name, cmd.name === 'Else' ? 'else' : 'else if ' + arg, Math.max(0, indent - 1));
continue;
}
if (ALGO_OPEN[cmd.name]) {
emit(cmd.name, ALGO_OPEN[cmd.name] + arg, indent);
indent++;
continue;
}
if (cmd.name === 'Return') {
var ret = ('return ' + restUntilCmd()).trim();
if (lines.length && lines[lines.length - 1].kind === 'State') {
lines[lines.length - 1].text = lines[lines.length - 1].text.replace(/\s+$/, '') + ' ' + ret;
} else emit('Return', ret, indent);
continue;
}
if (cmd.name === 'State') {
emit('State', restUntilCmd(), indent);
continue;
}
emit(cmd.name, (arg ? arg + ' ' : '') + restUntilCmd(), indent);
}
return lines;
}
function parseAlgorithm(body) {
var cap = '';
var id = null;
var c = /\\caption\{/.exec(body);
if (c) cap = readBrace(body, c.index + '\\caption'.length).text;
var lab = /\\label\{([^}]*)\}/.exec(body);
if (lab) id = lab[1];
var inner = '';
var alg = /\\begin\{algorithmic\}(?:\[[^\]]*\])?/.exec(body);
if (alg) {
var start = alg.index + alg[0].length;
var end = body.indexOf('\\end{algorithmic}', start);
inner = end < 0 ? body.slice(start) : body.slice(start, end);
}
return { type: 'algorithm', caption: cap, id: id, lines: parseAlgo(inner) };
}
function makeEnv(name, opt, body, depth, size) {
if (ENVS[name]) {
var labelled = takeLabel(body);
return { type: 'thm', kind: name, opt: opt, id: labelled.id, body: parseContent(labelled.rest, depth) };
}
if (name === 'proof') return { type: 'proof', opt: opt, body: parseContent(body, depth) };
if (name === 'abstract') return { type: 'abstract', body: parseContent(body, depth) };
if (name === 'center') return { type: 'center', body: parseContent(body, depth), size: size || '' };
if (name === 'enumerate' || name === 'itemize' || name === 'description') {
return {
type: 'list',
kind: listKind(name, opt),
depth: depth + 1,
items: splitItems(body).map(function (it) {
return { label: it.label, body: parseContent(it.text, depth + 1) };
})
};
}
if (name === 'tabular') {
var spec = '';
var rest = body;
if (rest.trimStart().startsWith('{')) {
var lead = rest.length - rest.trimStart().length;
var specArg = readBrace(rest.trimStart(), 0);
spec = specArg.text;
rest = rest.trimStart().slice(specArg.end);
if (lead) { /* spec already sliced from trimmed string */ }
}
return { type: 'table', rows: parseTabular(rest), size: size || '', spec: spec };
}
if (name === 'figure') return parseFigure(body);
if (name === 'algorithm') return parseAlgorithm(body);
if (name === 'thebibliography') return { type: 'bib', items: parseBib(body) };
throw new Error('unknown environment: ' + name);
}
function parseFigure(body) {
var imgs = [];
var re = /\\includegraphics(?:\[[^\]]*\])?\{([^}]*)\}/g;
var m;
while ((m = re.exec(body))) imgs.push(m[1]);
var cap = '';
var c = /\\caption\{/.exec(body);
if (c) cap = readBrace(body, c.index + '\\caption'.length).text;
var id = null;
var lab = /\\label\{([^}]*)\}/.exec(body);
if (lab) id = lab[1];
return { type: 'figure', imgs: imgs, caption: cap, id: id };
}
function parseBib(body) {
var items = [];
var re = /\\bibitem\b/g;
var starts = [];
var m;
while ((m = re.exec(body))) starts.push(m.index);
starts.forEach(function (pos, idx) {
var i = pos + '\\bibitem'.length;
if (body[i] === '[') i = readBracket(body, i).end;
if (body[i] !== '{') return;
var key = readBrace(body, i);
var end = idx + 1 < starts.length ? starts[idx + 1] : body.length;
items.push({ key: key.text, text: body.slice(key.end, end).trim() });
});
return items;
}
function assignMath(s, state) {
if (!s) return;
var re = /\uE000(\d+)\uE001/g;
var m;
while ((m = re.exec(s))) {
var info = state.math[+m[1]];
if (info && info.display && info.numbered && info.num == null) {
info.num = ++state.eq;
if (info.id) state.labels[info.id] = String(info.num);
}
}
}
function walk(nodes, state) {
nodes.forEach(function (n) {
if (n.type === 'appendix') {
state.appendix = true;
state.sec = 0;
state.sub = 0;
return;
}
if (n.type === 'section') {
if (n.level === 1) {
state.sec++;
state.sub = 0;
Object.keys(ENVS).forEach(function (kind) {
var def = ENVS[kind];
if (!def.shared && def.within === 'section') state.counters[kind] = 0;
});
n.num = state.appendix ? String.fromCharCode(64 + state.sec) : String(state.sec);
} else if (n.level === 2) {
state.sub++;
var secNo = state.appendix ? String.fromCharCode(64 + state.sec) : String(state.sec);
n.num = secNo + '.' + state.sub;
}
else n.num = '';
n.anchor = n.id || ('sec-' + (n.num || n.title));
if (n.id) state.labels[n.id] = n.num || n.title;
assignMath(n.title, state);
} else if (n.type === 'p') assignMath(n.text, state);
else if (n.type === 'thm') {
var def = ENVS[n.kind] || { title: n.kind, shared: null, within: 'section', style: 'plain' };
var key = def.shared || n.kind;
var parent = def.shared ? ENVS[def.shared] : def;
var within = parent && parent.within;
state.counters[key] = (state.counters[key] || 0) + 1;
n.num = within === 'section' ? state.sec + '.' + state.counters[key] : String(state.counters[key]);
n.title = def.title;
n.style = def.style || 'plain';
if (n.id) state.labels[n.id] = n.num;
assignMath(n.opt || '', state);
walk(n.body, state);
} else if (n.type === 'proof' || n.type === 'abstract' || n.type === 'center') walk(n.body, state);
else if (n.type === 'list') n.items.forEach(function (it) {
assignMath(it.label || '', state);
walk(it.body, state);
});
else if (n.type === 'table') n.rows.forEach(function (row) {
(row.cells || []).forEach(function (cell) { assignMath(cell.text, state); });
});
else if (n.type === 'figure') {
state.fig++;
n.num = String(state.fig);
if (n.id) state.labels[n.id] = n.num;
assignMath(n.caption, state);
} else if (n.type === 'algorithm') {
state.alg++;
n.num = String(state.alg);
if (n.id) state.labels[n.id] = n.num;
assignMath(n.caption, state);
n.lines.forEach(function (ln) {
assignMath(ln.text, state);
if (ln.comment) assignMath(ln.comment, state);
});
} else if (n.type === 'bib') n.items.forEach(function (it, idx) {
it.num = idx + 1;
state.cites[it.key] = it.num;
assignMath(it.text, state);
});
});
}
function mathSpan(info) {
if (!info) return '';
var open = info.display ? '\\[' : '\\(';
var close = info.display ? '\\]' : '\\)';
return open + esc(info.tex) + close;
}
function inline(s, state) {
var out = '';
var i = 0;
while (i < s.length) {
if (s.charCodeAt(i) === 0xE000) {
var j = s.indexOf('\uE001', i);
out += mathSpan(state.math[+s.slice(i + 1, j)]);
i = j + 1;
continue;
}
if (s.charCodeAt(i) === 0xE010) {
var j2 = s.indexOf('\uE011', i);
out += '' + inline(s.slice(i + 1, j2), state) + ' ';
i = j2 + 1;
continue;
}
if (s.startsWith('``', i)) { out += '\u201c'; i += 2; continue; }
if (s.startsWith("''", i)) { out += '\u201d'; i += 2; continue; }
if (s.startsWith('---', i)) { out += '\u2014'; i += 3; continue; }
if (s.startsWith('--', i)) { out += '\u2013'; i += 2; continue; }
if (s[i] === '~') { out += '\u00a0'; i++; continue; }
if (s[i] === '\\') {
var acc = s[i + 1];
if (acc && "'`^\"~".indexOf(acc) >= 0) {
var k = i + 2;
var ch = '';
if (s[k] === '{') {
var g = readBrace(s, k);
ch = g.text;
k = g.end;
} else {
ch = s[k] || '';
k++;
}
var mark = { "'": '\u0301', '`': '\u0300', '^': '\u0302', '"': '\u0308', '~': '\u0303' }[acc];
out += esc(ch).replace(/^(.)/, function (_, c) { return c + mark; });
i = k;
continue;
}
if (s.startsWith('\\c{', i)) {
var ced = readBrace(s, i + 2);
out += esc(ced.text) + '\u0327';
i = ced.end;
continue;
}
var word = /^\\([a-zA-Z]+)\s*/.exec(s.slice(i));
if (!word) {
var n = s[i + 1];
if (n === '\\') out += '
';
else if (n === ' ' || n === ',' || n === '/') out += n === ',' ? '\u2009' : (n === ' ' ? ' ' : '');
else if (n === '&') out += '&';
else if (n === '%') out += '%';
else if (n === '#') out += '#';
else if (n === '_') out += '_';
else if (n === '$') out += '$';
else if (n === '{' || n === '}') out += n;
else out += esc(n || '');
i += 2;
continue;
}
var cmd = word[1];
var p = i + word[0].length;
if (cmd === 'emph' || cmd === 'textit' || cmd === 'textsc') {
var a1 = readBrace(s, p);
out += '' + inline(a1.text, state) + '';
i = a1.end;
continue;
}
if (cmd === 'textbf' || cmd === 'textup') {
var a2 = readBrace(s, p);
out += '' + inline(a2.text, state) + '';
i = a2.end;
continue;
}
if (cmd === 'texttt') {
var a3 = readBrace(s, p);
out += '' + inline(a3.text, state) + '';
i = a3.end;
continue;
}
if (cmd === 'ref') {
var r = readBrace(s, p);
var num = state.labels[r.text];
out += '' + esc(num || r.text) + '';
i = r.end;
continue;
}
if (cmd === 'eqref') {
var e = readBrace(s, p);
var en = state.labels[e.text];
out += '(' + esc(en || e.text) + ')';
i = e.end;
continue;
}
if (cmd === 'newblock') { out += ' '; i = p; continue; }
if (cmd === 'penalty') {
while (p < s.length && /[0-9]/.test(s[p])) p++;
i = p;
continue;
}
if (cmd === 'url') {
var u = readBrace(s, p);
out += '' + esc(u.text) + '';
i = u.end;
continue;
}
if (cmd === 'cite' || cmd === 'citep' || cmd === 'citet') {
var note = '';
if (s[p] === '[') {
var br = readBracket(s, p);
note = br.text;
p = br.end;
}
var c = readBrace(s, p);
var keys = c.text.split(/\s*,\s*/);
var bits = keys.map(function (key) {
var cn = state.cites[key];
return '' + esc(cn || key) + '';
});
out += '[' + bits.join(', ') + (note ? ', ' + inline(note, state) : '') + ']';
i = c.end;
continue;
}
if (cmd === 'S') { out += '\u00a7'; i = p; continue; }
if (cmd === 'label') {
var lb = readBrace(s, p);
out += '';
i = lb.end;
continue;
}
if (cmd === 'qedhere') { out += '\u220e'; i = p; continue; }
if (cmd === 'noindent' || cmd === 'leavevmode' || cmd === 'centering' || cmd === 'small' ||
cmd === 'footnotesize' || cmd === 'normalsize' || cmd === 'clearpage' || cmd === 'hfill' ||
cmd === 'qquad' || cmd === 'quad' || cmd === 'hspace' || cmd === 'vspace') {
if (cmd === 'hspace' || cmd === 'vspace') {
if (s[p] === '{') p = readBrace(s, p).end;
}
if (cmd === 'quad') out += '\u2003';
if (cmd === 'qquad') out += '\u2003\u2003';
i = p;
continue;
}
if (MACROS[cmd]) {
out += '\\(' + esc(MACROS[cmd]) + '\\)';
i = p;
continue;
}
if (s[p] === '{') {
var unk = readBrace(s, p);
state.unknown[cmd] = (state.unknown[cmd] || 0) + 1;
out += inline(unk.text, state);
i = unk.end;
continue;
}
state.unknown[cmd] = (state.unknown[cmd] || 0) + 1;
i = p;
continue;
}
if (s[i] === '{') {
var grp = readBrace(s, i);
var inner = grp.text;
var font = /^(?:\\(em|it|bf|rm|sc|tt|bfseries|itshape|rmfamily|sffamily))\s*/.exec(inner);
if (font) {
var rest = inner.slice(font[0].length);
var tag = (font[1] === 'bf' || font[1] === 'bfseries') ? 'strong' : 'em';
if (font[1] === 'rm' || font[1] === 'rmfamily' || font[1] === 'upshape') out += inline(rest, state);
else out += '<' + tag + '>' + inline(rest, state) + '' + tag + '>';
} else out += inline(inner, state);
i = grp.end;
continue;
}
out += esc(s[i]);
i++;
}
return out;
}
function renderBlocks(nodes, state, mergeFirst) {
var html = '';
nodes.forEach(function (n, idx) {
html += renderNode(n, state, mergeFirst && idx === 0);
});
return html;
}
function renderParas(text, state) {
var re = /\uE000(\d+)\uE001/g;
var html = '';
var last = 0;
var buf = '';
var m;
function flush() {
var t = buf.replace(/\s*\n\s*/g, ' ').trim();
if (t) html += '
' + inline(t, state) + '
'; buf = ''; } while ((m = re.exec(text))) { var info = state.math[+m[1]]; if (info && info.display) { buf += text.slice(last, m.index); flush(); html += renderEq(info, state); } else { buf += text.slice(last, m.index) + m[0]; } last = m.index + m[0].length; } buf += text.slice(last); flush(); return html; } function renderEq(info) { var id = info.id ? ' id="' + esc(info.id) + '"' : ''; var no = info.numbered ? '(' + info.num + ')' : ''; return '' + inline(state.author, state) + '
' + '' + inline(state.date, state) + '
\u220e
'; return '' + head + '
'; if (blocks[0].type !== 'p') return '' + head + '
' + renderBlocks(blocks, state); var first = blocks[0].text; var re = /\uE000(\d+)\uE001/; var m = re.exec(first); var html; if (m && m.index === 0 && state.math[+m[1]] && state.math[+m[1]].display) { html = '' + head + '
' + renderParas(first, state); } else { var collapsed = first.replace(/\s*\n\s*/g, ' ').trim(); var displayAt = collapsed.search(/\uE000(\d+)\uE001/); html = renderParas(collapsed, state).replace('', '
' + head); } return html + renderBlocks(blocks.slice(1), state); } function renderList(n, state) { if (n.kind === 'desc') { return '