Skip to content

Commit 2e2d5dc

Browse files
committed
Workaround for NaN in mathjax in node.js
1 parent a0209b3 commit 2e2d5dc

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

modules/base/latex.mjs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,14 @@ function applyAttributesToMathJax(painter, mj_node, svg, arg, font_size, svg_fac
14381438
let mw = parseInt(svg.attr('width')),
14391439
mh = parseInt(svg.attr('height'));
14401440

1441+
if (isNodeJs()) {
1442+
// workaround for NaN in viewBox produced by MathJax
1443+
let vb = svg.attr('viewBox');
1444+
if (isStr(vb) && vb.indexOf('NaN') > 0)
1445+
svg.attr('viewBox', vb.replaceAll('NaN', '600'));
1446+
// console.log('Problematic viewBox', vb, svg.select('text').node()?.innerHTML);
1447+
}
1448+
14411449
if (Number.isInteger(mh) && Number.isInteger(mw)) {
14421450
if (svg_factor > 0) {
14431451
mw = mw / svg_factor;
@@ -1450,9 +1458,11 @@ function applyAttributesToMathJax(painter, mj_node, svg, arg, font_size, svg_fac
14501458
mh = box.height || mh || 10;
14511459
}
14521460

1453-
if ((svg_factor > 0) && arg.valign) arg.valign = arg.valign / svg_factor;
1461+
if ((svg_factor > 0) && arg.valign)
1462+
arg.valign = arg.valign / svg_factor;
14541463

1455-
if (arg.valign === null) arg.valign = (font_size - mh) / 2;
1464+
if (arg.valign === null)
1465+
arg.valign = (font_size - mh) / 2;
14561466

14571467
const sign = { x: 1, y: 1 };
14581468
let nx = 'x', ny = 'y';
@@ -1477,10 +1487,10 @@ function applyAttributesToMathJax(painter, mj_node, svg, arg, font_size, svg_fac
14771487
arg[ny] += sign.y * (arg.height - mh - arg.valign);
14781488

14791489
let trans = makeTranslate(arg.x, arg.y) || '';
1480-
if (arg.rotate) {
1481-
if (trans) trans += ' ';
1482-
trans += `rotate(${arg.rotate})`;
1483-
}
1490+
if (arg.rotate)
1491+
trans += `${trans?' ':''}rotate(${arg.rotate})`;
1492+
1493+
14841494

14851495
mj_node.attr('transform', trans || null).attr('visibility', null);
14861496
}

0 commit comments

Comments
 (0)