|
本帖最后由 hbghlyj 于 2022-9-5 05:43 编辑 首先在create处,用$\verb|debugger;|$设置一个断点:
- t.prototype.create = function (t) {
- for (var e, r = [
- ], n = 1; n < arguments.length; n++) r[n - 1] = arguments[n];
- debugger;
- return (e = this.configuration.nodeFactory).create.apply(e, i([t], o(r), !1))
- },
- t
- }();
复制代码
用MathJax处理$\frac ab$,第一次断开$\verb|_string|$是"a",第二次断开是"b",第三次是"\\frac ab",看Call stack发现 “分数” 没有调用input/tex/base/BaseMethods.ts第468行的BaseMethods.Frac而是调用了input/tex/ams/AmsMethods.ts第510行的e.AmsMethods.Genfrac,这是因为ams(MathJax Extension)重定义了$\verb|\frac|$:
input/tex/ams/AmsMappings.ts第82行
- frac: ['Genfrac', '', '', '', ''],
复制代码 找到Genfrac的定义,(和15楼的改法类似)改为- e.AmsMethods.Genfrac=function(t,e,r,n,o,i){let old_i=t.i;null==r&&(r=t.GetDelimiterArg(e)),null==n&&(n=t.GetDelimiterArg(e)),null==o&&(o=t.GetArgument(e)),null==i&&(i=a.default.trimSpaces(t.GetArgument(e)));var s=t.ParseArg(e),u=t.ParseArg(e),c=t.create("node","mfrac",[s,u]);if(""!==o&&l.default.setAttribute(c,"linethickness",o),(r||n)&&(l.default.setProperty(c,"withDelims",!0),c=a.default.fixedFence(t.configuration,r,c,n)),""!==i){var f=parseInt(i,10),h=["D","T","S","SS"][f];if(null==h)throw new p.default("BadMathStyleFor","Bad math style for %1",t.currentCS);c=t.create("node","mstyle",[c]),"D"===h?l.default.setProperties(c,{displaystyle:!0,scriptlevel:0}):l.default.setProperties(c,{displaystyle:!1,scriptlevel:f-1})}c.stringlength=t.i-old_i+e.length+(+(t._string.charAt(old_i-1)==' '));t.Push(c)}
复制代码 input/tex/base/BaseMethods.ts第363行
找到- O.NamedFn=function(t,e,r){r||(r=e.substr(1));var n=t.create("token","mi",{texClass:y.TEXCLASS.OP},r);t.Push(t.itemFactory.create("fn",n))}
复制代码
改为- O.NamedFn=function(t,e,r){r||(r=e.substr(1));let n=t.create("token","mi",{texClass:y.TEXCLASS.OP},r),c=t.itemFactory.create("fn",n);n.stringlength=e.length;t.Push(c)}
复制代码
第161行
- BaseMethods.Subscript = function(parser: TexParser, _c: string) {
- if (parser.GetNext().match(/\d/)) {
- // don't treat numbers as a unit
- parser.string =
- parser.string.substr(0, parser.i + 1) + ' ' +
- parser.string.substr(parser.i + 1);
- }
- let primes, base;
- const top = parser.stack.Top();
- if (top.isKind('prime')) {
- // @test Prime on Sub
- [base, primes] = top.Peek(2);
- parser.stack.Pop();
- } else {
- base = parser.stack.Prev();
- if (!base) {
- // @test Empty Base Index
- base = parser.create('token', 'mi', {}, '');
- }
- }
- const movesupsub = NodeUtil.getProperty(base, 'movesupsub');
- let position = NodeUtil.isType(base, 'msubsup') ?
- (base as MmlMsubsup).sub : (base as MmlMunderover).under;
- if ((NodeUtil.isType(base, 'msubsup') && !NodeUtil.isType(base, 'msup') &&
- NodeUtil.getChildAt(base, (base as MmlMsubsup).sub)) ||
- (NodeUtil.isType(base, 'munderover') && !NodeUtil.isType(base, 'mover') &&
- NodeUtil.getChildAt(base, (base as MmlMunderover).under) &&
- !NodeUtil.getProperty(base, 'subsupOK'))) {
- // @test Double-sub-error, Double-under-error
- throw new TexError('DoubleSubscripts', 'Double subscripts: use braces to clarify');
- }
- if (!NodeUtil.isType(base, 'msubsup') || NodeUtil.isType(base, 'msup')) {
- if (movesupsub) {
- // @test Large Operator, Move Superscript
- if (!NodeUtil.isType(base, 'munderover') || NodeUtil.isType(base, 'mover') ||
- NodeUtil.getChildAt(base, (base as MmlMunderover).under)) {
- // @test Move Superscript
- base = parser.create('node', 'munderover', [base], {movesupsub: true});
- }
- position = (base as MmlMunderover).under;
- } else {
- // @test Empty Base Index, Empty Base Index2, Index
- base = parser.create('node', 'msubsup', [base]);
- position = (base as MmlMsubsup).sub;
- }
- }
- parser.Push(
- parser.itemFactory.create('subsup', base).setProperties({
- position: position, primes: primes, movesupsub: movesupsub
- }) );
- };
复制代码
对应于JS- O.Subscript=function(t,e){var r,n,o;t.GetNext().match(/\d/)&&(t.string=t.string.substr(0,t.i+1)+" "+t.string.substr(t.i+1));var i=t.stack.Top();i.isKind("prime")?(o=(r=s(i.Peek(2),2))[0],n=r[1],t.stack.Pop()):(o=t.stack.Prev())||(o=t.create("token","mi",{},""));var a=c.default.getProperty(o,"movesupsub"),l=c.default.isType(o,"msubsup")?o.sub:o.under;if(c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")&&c.default.getChildAt(o,o.sub)||c.default.isType(o,"munderover")&&!c.default.isType(o,"mover")&&c.default.getChildAt(o,o.under)&&!c.default.getProperty(o,"subsupOK"))throw new p.default("DoubleSubscripts","Double subscripts: use braces to clarify");c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")||(a?((!c.default.isType(o,"munderover")||c.default.isType(o,"mover")||c.default.getChildAt(o,o.under))&&(o=t.create("node","munderover",[o],{movesupsub:!0})),l=o.under):l=(o=t.create("node","msubsup",[o])).sub),t.Push(t.itemFactory.create("subsup",o).setProperties({position:l,primes:n,movesupsub:a}))}
复制代码
don't treat numbers as a unit是什么意思? 对于a_25, MathJax的parser正常来说会把25当成整体放到<mn>里面, 但是subscript(同样对于superscript也是)在这一步增加了一个空格, 变成a_2 5就可以“don't treat as a unit”了
但这会导致后面的定位出错, 我们把$\verb't.GetNext().match(/\d/)&&(t.string=t.string.substr(0,t.i+1)+" "+t.string.substr(t.i+1));'$改为直接create那个<mn>就行了(在结尾) :
- O.Subscript=function(t,e){let r,n,o,d=t.GetNext();var i=t.stack.Top();i.isKind("prime")?(o=(r=s(i.Peek(2),2))[0],n=r[1],t.stack.Pop()):(o=t.stack.Prev())||(o=t.create("token","mi",{},""));var a=c.default.getProperty(o,"movesupsub"),l=c.default.isType(o,"msubsup")?o.sub:o.under;if(c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")&&c.default.getChildAt(o,o.sub)||c.default.isType(o,"munderover")&&!c.default.isType(o,"mover")&&c.default.getChildAt(o,o.under)&&!c.default.getProperty(o,"subsupOK"))throw new p.default("DoubleSubscripts","Double subscripts: use braces to clarify");c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")||(a?((!c.default.isType(o,"munderover")||c.default.isType(o,"mover")||c.default.getChildAt(o,o.under))&&(o=t.create("node","munderover",[o],{movesupsub:!0})),l=o.under):l=(o=t.create("node","msubsup",[o])).sub),t.Push(t.itemFactory.create("subsup",o).setProperties({position:l,primes:n,movesupsub:a}));if(d.match(/\d/)){t.i++;t.Push(t.create("token","mn",{},d))}}
复制代码 同样地,对于Superscript,- O.Superscript=function(t,e){var r,n,o;t.GetNext().match(/\d/)&&(t.string=t.string.substr(0,t.i+1)+" "+t.string.substr(t.i+1));var i=t.stack.Top();i.isKind("prime")?(o=(r=s(i.Peek(2),2))[0],n=r[1],t.stack.Pop()):(o=t.stack.Prev())||(o=t.create("token","mi",{},""));var a=c.default.getProperty(o,"movesupsub"),l=c.default.isType(o,"msubsup")?o.sup:o.over;if(c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")&&c.default.getChildAt(o,o.sup)||c.default.isType(o,"munderover")&&!c.default.isType(o,"mover")&&c.default.getChildAt(o,o.over)&&!c.default.getProperty(o,"subsupOK"))throw new p.default("DoubleExponent","Double exponent: use braces to clarify");c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")||(a?((!c.default.isType(o,"munderover")||c.default.isType(o,"mover")||c.default.getChildAt(o,o.over))&&(o=t.create("node","munderover",[o],{movesupsub:!0})),l=o.over):l=(o=t.create("node","msubsup",[o])).sup),t.Push(t.itemFactory.create("subsup",o).setProperties({position:l,primes:n,movesupsub:a}))}
复制代码 把“加空格”那一步改掉,变成- O.Superscript=function(t,e){let r,n,o,d=t.GetNext();var i=t.stack.Top();i.isKind("prime")?(o=(r=s(i.Peek(2),2))[0],n=r[1],t.stack.Pop()):(o=t.stack.Prev())||(o=t.create("token","mi",{},""));var a=c.default.getProperty(o,"movesupsub"),l=c.default.isType(o,"msubsup")?o.sup:o.over;if(c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")&&c.default.getChildAt(o,o.sup)||c.default.isType(o,"munderover")&&!c.default.isType(o,"mover")&&c.default.getChildAt(o,o.over)&&!c.default.getProperty(o,"subsupOK"))throw new p.default("DoubleExponent","Double exponent: use braces to clarify");c.default.isType(o,"msubsup")&&!c.default.isType(o,"msup")||(a?((!c.default.isType(o,"munderover")||c.default.isType(o,"mover")||c.default.getChildAt(o,o.over))&&(o=t.create("node","munderover",[o],{movesupsub:!0})),l=o.over):l=(o=t.create("node","msubsup",[o])).sup),t.Push(t.itemFactory.create("subsup",o).setProperties({position:l,primes:n,movesupsub:a}));if(d.match(/\d/)){t.i++;t.Push(t.create("token","mn",{},d))}}
复制代码 |
|