| 1 |
/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ |
| 2 |
/* vim: set ts=2 et sw=2 tw=80: */ |
| 3 |
|
| 4 |
/************************************************************* |
| 5 |
* |
| 6 |
* MathJax/extensions/TeX/AMSmath.js |
| 7 |
* |
| 8 |
* Implements AMS math environments and macros. |
| 9 |
* |
| 10 |
* --------------------------------------------------------------------- |
| 11 |
* |
| 12 |
* Copyright (c) 2009-2020 The MathJax Consortium |
| 13 |
* |
| 14 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 |
* you may not use this file except in compliance with the License. |
| 16 |
* You may obtain a copy of the License at |
| 17 |
* |
| 18 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 19 |
* |
| 20 |
* Unless required by applicable law or agreed to in writing, software |
| 21 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 22 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 |
* See the License for the specific language governing permissions and |
| 24 |
* limitations under the License. |
| 25 |
*/ |
| 26 |
|
| 27 |
MathJax.Extension["TeX/AMSmath"] = { |
| 28 |
version: "2.7.9", |
| 29 |
|
| 30 |
number: 0, // current equation number |
| 31 |
startNumber: 0, // current starting equation number (for when equation is restarted) |
| 32 |
IDs: {}, // IDs used in previous equations |
| 33 |
eqIDs: {}, // IDs used in this equation |
| 34 |
labels: {}, // the set of labels |
| 35 |
eqlabels: {}, // labels in the current equation |
| 36 |
refs: [] // array of jax with unresolved references |
| 37 |
}; |
| 38 |
|
| 39 |
MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () { |
| 40 |
|
| 41 |
var MML = MathJax.ElementJax.mml, |
| 42 |
TEX = MathJax.InputJax.TeX, |
| 43 |
AMS = MathJax.Extension["TeX/AMSmath"]; |
| 44 |
|
| 45 |
var TEXDEF = TEX.Definitions, |
| 46 |
STACKITEM = TEX.Stack.Item, |
| 47 |
CONFIG = TEX.config.equationNumbers; |
| 48 |
|
| 49 |
var COLS = function (W) { |
| 50 |
var WW = []; |
| 51 |
for (var i = 0, m = W.length; i < m; i++) |
| 52 |
{WW[i] = TEX.Parse.prototype.Em(W[i])} |
| 53 |
return WW.join(" "); |
| 54 |
}; |
| 55 |
|
| 56 |
// |
| 57 |
// Get the URL of the page (for use with formatURL) when there |
| 58 |
// is a <base> element on the page. |
| 59 |
// |
| 60 |
var baseURL = (document.getElementsByTagName("base").length === 0) ? "" : |
| 61 |
String(document.location).replace(/#.*$/,""); |
| 62 |
|
| 63 |
|
| 64 |
/******************************************************************************/ |
| 65 |
|
| 66 |
TEXDEF.Add({ |
| 67 |
mathchar0mo: { |
| 68 |
iiiint: ['2A0C',{texClass: MML.TEXCLASS.OP}] |
| 69 |
}, |
| 70 |
|
| 71 |
macros: { |
| 72 |
mathring: ['Accent','2DA'], // or 0x30A |
| 73 |
|
| 74 |
nobreakspace: 'Tilde', |
| 75 |
negmedspace: ['Spacer',MML.LENGTH.NEGATIVEMEDIUMMATHSPACE], |
| 76 |
negthickspace: ['Spacer',MML.LENGTH.NEGATIVETHICKMATHSPACE], |
| 77 |
|
| 78 |
// intI: ['Macro','\\mathchoice{\\!}{}{}{}\\!\\!\\int'], |
| 79 |
// iint: ['MultiIntegral','\\int\\intI'], // now in core TeX input jax |
| 80 |
// iiint: ['MultiIntegral','\\int\\intI\\intI'], // now in core TeX input jax |
| 81 |
// iiiint: ['MultiIntegral','\\int\\intI\\intI\\intI'], // now in mathchar0mo above |
| 82 |
idotsint: ['MultiIntegral','\\int\\cdots\\int'], |
| 83 |
|
| 84 |
// dddot: ['Macro','\\mathop{#1}\\limits^{\\textstyle \\mathord{.}\\mathord{.}\\mathord{.}}',1], |
| 85 |
// ddddot: ['Macro','\\mathop{#1}\\limits^{\\textstyle \\mathord{.}\\mathord{.}\\mathord{.}\\mathord{.}}',1], |
| 86 |
dddot: ['Accent','20DB'], |
| 87 |
ddddot: ['Accent','20DC'], |
| 88 |
|
| 89 |
sideset: ['Macro','\\mathop{\\mathop{\\rlap{\\phantom{#3}}}\\nolimits#1\\!\\mathop{#3}\\nolimits#2}',3], |
| 90 |
|
| 91 |
boxed: ['Macro','\\fbox{$\\displaystyle{#1}$}',1], |
| 92 |
|
| 93 |
tag: 'HandleTag', |
| 94 |
notag: 'HandleNoTag', |
| 95 |
label: 'HandleLabel', |
| 96 |
ref: 'HandleRef', |
| 97 |
eqref: ['HandleRef',true], |
| 98 |
|
| 99 |
substack: ['Macro','\\begin{subarray}{c}#1\\end{subarray}',1], |
| 100 |
|
| 101 |
injlim: ['NamedOp','inj lim'], |
| 102 |
projlim: ['NamedOp','proj lim'], |
| 103 |
varliminf: ['Macro','\\mathop{\\underline{\\mmlToken{mi}{lim}}}'], |
| 104 |
varlimsup: ['Macro','\\mathop{\\overline{\\mmlToken{mi}{lim}}}'], |
| 105 |
varinjlim: ['Macro','\\mathop{\\underrightarrow{\\mmlToken{mi}{lim}}}'], |
| 106 |
varprojlim: ['Macro','\\mathop{\\underleftarrow{\\mmlToken{mi}{lim}}}'], |
| 107 |
|
| 108 |
DeclareMathOperator: 'HandleDeclareOp', |
| 109 |
operatorname: 'HandleOperatorName', |
| 110 |
SkipLimits: 'SkipLimits', |
| 111 |
|
| 112 |
genfrac: 'Genfrac', |
| 113 |
frac: ['Genfrac',"","","",""], |
| 114 |
tfrac: ['Genfrac',"","","",1], |
| 115 |
dfrac: ['Genfrac',"","","",0], |
| 116 |
binom: ['Genfrac',"(",")","0",""], |
| 117 |
tbinom: ['Genfrac',"(",")","0",1], |
| 118 |
dbinom: ['Genfrac',"(",")","0",0], |
| 119 |
|
| 120 |
cfrac: 'CFrac', |
| 121 |
|
| 122 |
shoveleft: ['HandleShove',MML.ALIGN.LEFT], |
| 123 |
shoveright: ['HandleShove',MML.ALIGN.RIGHT], |
| 124 |
|
| 125 |
xrightarrow: ['xArrow',0x2192,5,6], |
| 126 |
xleftarrow: ['xArrow',0x2190,7,3] |
| 127 |
}, |
| 128 |
|
| 129 |
environment: { |
| 130 |
align: ['AMSarray',null,true,true, 'rlrlrlrlrlrl',COLS([0,2,0,2,0,2,0,2,0,2,0])], |
| 131 |
'align*': ['AMSarray',null,false,true, 'rlrlrlrlrlrl',COLS([0,2,0,2,0,2,0,2,0,2,0])], |
| 132 |
multline: ['Multline',null,true], |
| 133 |
'multline*': ['Multline',null,false], |
| 134 |
split: ['AMSarray',null,false,false,'rl',COLS([0])], |
| 135 |
gather: ['AMSarray',null,true,true, 'c'], |
| 136 |
'gather*': ['AMSarray',null,false,true, 'c'], |
| 137 |
|
| 138 |
alignat: ['AlignAt',null,true,true], |
| 139 |
'alignat*': ['AlignAt',null,false,true], |
| 140 |
alignedat: ['AlignAt',null,false,false], |
| 141 |
|
| 142 |
aligned: ['AlignedAMSArray',null,null,null,'rlrlrlrlrlrl',COLS([0,2,0,2,0,2,0,2,0,2,0]),".5em",'D'], |
| 143 |
gathered: ['AlignedAMSArray',null,null,null,'c',null,".5em",'D'], |
| 144 |
|
| 145 |
subarray: ['Array',null,null,null,null,COLS([0]),"0.1em",'S',1], |
| 146 |
smallmatrix: ['Array',null,null,null,'c',COLS([1/3]),".2em",'S',1], |
| 147 |
|
| 148 |
'equation': ['EquationBegin','Equation',true], |
| 149 |
'equation*': ['EquationBegin','EquationStar',false], |
| 150 |
|
| 151 |
eqnarray: ['AMSarray',null,true,true, 'rcl',"0 "+MML.LENGTH.THICKMATHSPACE,".5em"], |
| 152 |
'eqnarray*': ['AMSarray',null,false,true,'rcl',"0 "+MML.LENGTH.THICKMATHSPACE,".5em"] |
| 153 |
}, |
| 154 |
|
| 155 |
delimiter: { |
| 156 |
'\\lvert': ['007C',{texClass:MML.TEXCLASS.OPEN}], |
| 157 |
'\\rvert': ['007C',{texClass:MML.TEXCLASS.CLOSE}], |
| 158 |
'\\lVert': ['2016',{texClass:MML.TEXCLASS.OPEN}], |
| 159 |
'\\rVert': ['2016',{texClass:MML.TEXCLASS.CLOSE}] |
| 160 |
} |
| 161 |
},null,true); |
| 162 |
|
| 163 |
|
| 164 |
/******************************************************************************/ |
| 165 |
|
| 166 |
TEX.Parse.Augment({ |
| 167 |
|
| 168 |
/* |
| 169 |
* Add the tag to the environment (to be added to the table row later) |
| 170 |
*/ |
| 171 |
HandleTag: function (name) { |
| 172 |
var star = this.GetStar(); |
| 173 |
var arg = this.trimSpaces(this.GetArgument(name)), tag = arg; |
| 174 |
if (!star) {arg = CONFIG.formatTag(arg)} |
| 175 |
var global = this.stack.global; global.tagID = tag; |
| 176 |
if (global.notags) { |
| 177 |
TEX.Error(["CommandNotAllowedInEnv", |
| 178 |
"%1 not allowed in %2 environment", |
| 179 |
name,global.notags] |
| 180 |
); |
| 181 |
} |
| 182 |
if (global.tag) {TEX.Error(["MultipleCommand","Multiple %1",name])} |
| 183 |
global.tag = MML.mtd.apply(MML,this.InternalMath(arg)).With({id:CONFIG.formatID(tag)}); |
| 184 |
}, |
| 185 |
HandleNoTag: function (name) { |
| 186 |
if (this.stack.global.tag) {delete this.stack.global.tag} |
| 187 |
this.stack.global.notag = true; // prevent auto-tagging |
| 188 |
}, |
| 189 |
|
| 190 |
/* |
| 191 |
* Record a label name for a tag |
| 192 |
*/ |
| 193 |
HandleLabel: function (name) { |
| 194 |
var global = this.stack.global, label = this.GetArgument(name); |
| 195 |
if (label === "") return; |
| 196 |
if (!AMS.refUpdate) { |
| 197 |
if (global.label) {TEX.Error(["MultipleCommand","Multiple %1",name])} |
| 198 |
global.label = label; |
| 199 |
if (AMS.labels[label] || AMS.eqlabels[label]) |
| 200 |
{TEX.Error(["MultipleLabel","Label '%1' multiply defined",label])} |
| 201 |
AMS.eqlabels[label] = {tag:"???", id:""}; // will be replaced by tag value later |
| 202 |
} |
| 203 |
}, |
| 204 |
|
| 205 |
/* |
| 206 |
* Handle a label reference |
| 207 |
*/ |
| 208 |
HandleRef: function (name,eqref) { |
| 209 |
var label = this.GetArgument(name); |
| 210 |
var ref = AMS.labels[label] || AMS.eqlabels[label]; |
| 211 |
if (!ref) {ref = {tag:"???",id:""}; AMS.badref = !AMS.refUpdate} |
| 212 |
var tag = ref.tag; if (eqref) {tag = CONFIG.formatTag(tag)} |
| 213 |
this.Push(MML.mrow.apply(MML,this.InternalMath(tag)).With({ |
| 214 |
href:CONFIG.formatURL(ref.id,baseURL), "class":"MathJax_ref" |
| 215 |
})); |
| 216 |
}, |
| 217 |
|
| 218 |
/* |
| 219 |
* Handle \DeclareMathOperator |
| 220 |
*/ |
| 221 |
HandleDeclareOp: function (name) { |
| 222 |
var limits = (this.GetStar() ? "" : "\\nolimits\\SkipLimits"); |
| 223 |
var cs = this.trimSpaces(this.GetArgument(name)); |
| 224 |
if (cs.charAt(0) == "\\") {cs = cs.substr(1)} |
| 225 |
var op = this.GetArgument(name); |
| 226 |
if (!op.match(/\\text/)) op = op.replace(/\*/g,'\\text{*}').replace(/-/g,'\\text{-}'); |
| 227 |
this.setDef(cs, ['Macro', '\\mathop{\\rm '+op+'}'+limits]); |
| 228 |
}, |
| 229 |
|
| 230 |
HandleOperatorName: function (name) { |
| 231 |
var limits = (this.GetStar() ? "" : "\\nolimits\\SkipLimits"); |
| 232 |
var op = this.trimSpaces(this.GetArgument(name)); |
| 233 |
if (!op.match(/\\text/)) op = op.replace(/\*/g,'\\text{*}').replace(/-/g,'\\text{-}'); |
| 234 |
this.string = '\\mathop{\\rm '+op+'}'+limits+" "+this.string.slice(this.i); |
| 235 |
this.i = 0; |
| 236 |
}, |
| 237 |
|
| 238 |
SkipLimits: function (name) { |
| 239 |
var c = this.GetNext(), i = this.i; |
| 240 |
if (c === "\\" && ++this.i && this.GetCS() !== "limits") this.i = i; |
| 241 |
}, |
| 242 |
|
| 243 |
/* |
| 244 |
* Record presence of \shoveleft and \shoveright |
| 245 |
*/ |
| 246 |
HandleShove: function (name,shove) { |
| 247 |
var top = this.stack.Top(); |
| 248 |
if (top.type !== "multline") { |
| 249 |
TEX.Error(["CommandInMultline", |
| 250 |
"%1 can only appear within the multline environment",name]); |
| 251 |
} |
| 252 |
if (top.data.length) { |
| 253 |
TEX.Error(["CommandAtTheBeginingOfLine", |
| 254 |
"%1 must come at the beginning of the line",name]); |
| 255 |
} |
| 256 |
top.data.shove = shove; |
| 257 |
}, |
| 258 |
|
| 259 |
/* |
| 260 |
* Handle \cfrac |
| 261 |
*/ |
| 262 |
CFrac: function (name) { |
| 263 |
var lr = this.trimSpaces(this.GetBrackets(name,"")), |
| 264 |
num = this.GetArgument(name), |
| 265 |
den = this.GetArgument(name); |
| 266 |
var frac = MML.mfrac(TEX.Parse('\\strut\\textstyle{'+num+'}',this.stack.env).mml(), |
| 267 |
TEX.Parse('\\strut\\textstyle{'+den+'}',this.stack.env).mml()); |
| 268 |
lr = ({l:MML.ALIGN.LEFT, r:MML.ALIGN.RIGHT,"":""})[lr]; |
| 269 |
if (lr == null) |
| 270 |
{TEX.Error(["IllegalAlign","Illegal alignment specified in %1",name])} |
| 271 |
if (lr) {frac.numalign = frac.denomalign = lr} |
| 272 |
this.Push(frac); |
| 273 |
}, |
| 274 |
|
| 275 |
/* |
| 276 |
* Implement AMS generalized fraction |
| 277 |
*/ |
| 278 |
Genfrac: function (name,left,right,thick,style) { |
| 279 |
if (left == null) {left = this.GetDelimiterArg(name)} |
| 280 |
if (right == null) {right = this.GetDelimiterArg(name)} |
| 281 |
if (thick == null) {thick = this.GetArgument(name)} |
| 282 |
if (style == null) {style = this.trimSpaces(this.GetArgument(name))} |
| 283 |
var num = this.ParseArg(name); |
| 284 |
var den = this.ParseArg(name); |
| 285 |
var frac = MML.mfrac(num,den); |
| 286 |
if (thick !== "") {frac.linethickness = thick} |
| 287 |
if (left || right) {frac = TEX.fixedFence(left,frac.With({texWithDelims:true}),right)} |
| 288 |
if (style !== "") { |
| 289 |
var STYLE = (["D","T","S","SS"])[style]; |
| 290 |
if (STYLE == null) |
| 291 |
{TEX.Error(["BadMathStyleFor","Bad math style for %1",name])} |
| 292 |
frac = MML.mstyle(frac); |
| 293 |
if (STYLE === "D") {frac.displaystyle = true; frac.scriptlevel = 0} |
| 294 |
else {frac.displaystyle = false; frac.scriptlevel = style - 1} |
| 295 |
} |
| 296 |
this.Push(frac); |
| 297 |
}, |
| 298 |
|
| 299 |
/* |
| 300 |
* Implements multline environment (mostly handled through STACKITEM below) |
| 301 |
*/ |
| 302 |
Multline: function (begin,numbered) { |
| 303 |
this.Push(begin); this.checkEqnEnv(); |
| 304 |
return STACKITEM.multline(numbered,this.stack).With({ |
| 305 |
arraydef: { |
| 306 |
displaystyle: true, |
| 307 |
rowspacing: ".5em", |
| 308 |
width: TEX.config.MultLineWidth, columnwidth:"100%", |
| 309 |
side: TEX.config.TagSide, |
| 310 |
minlabelspacing: TEX.config.TagIndent |
| 311 |
} |
| 312 |
}); |
| 313 |
}, |
| 314 |
|
| 315 |
/* |
| 316 |
* Handle AMS aligned environments |
| 317 |
*/ |
| 318 |
AMSarray: function (begin,numbered,taggable,align,spacing) { |
| 319 |
this.Push(begin); if (taggable) {this.checkEqnEnv()} |
| 320 |
align = align.replace(/[^clr]/g,'').split('').join(' '); |
| 321 |
align = align.replace(/l/g,'left').replace(/r/g,'right').replace(/c/g,'center'); |
| 322 |
return STACKITEM.AMSarray(begin.name,numbered,taggable,this.stack).With({ |
| 323 |
arraydef: { |
| 324 |
displaystyle: true, |
| 325 |
rowspacing: ".5em", |
| 326 |
columnalign: align, |
| 327 |
columnspacing: (spacing||"1em"), |
| 328 |
rowspacing: "3pt", |
| 329 |
side: TEX.config.TagSide, |
| 330 |
minlabelspacing: TEX.config.TagIndent |
| 331 |
} |
| 332 |
}); |
| 333 |
}, |
| 334 |
|
| 335 |
AlignedAMSArray: function (begin) { |
| 336 |
var align = this.GetBrackets("\\begin{"+begin.name+"}"); |
| 337 |
return this.setArrayAlign(this.AMSarray.apply(this,arguments),align); |
| 338 |
}, |
| 339 |
|
| 340 |
/* |
| 341 |
* Handle alignat environments |
| 342 |
*/ |
| 343 |
AlignAt: function (begin,numbered,taggable) { |
| 344 |
var n, valign, align = "", spacing = []; |
| 345 |
if (!taggable) {valign = this.GetBrackets("\\begin{"+begin.name+"}")} |
| 346 |
n = this.GetArgument("\\begin{"+begin.name+"}"); |
| 347 |
if (n.match(/[^0-9]/)) { |
| 348 |
TEX.Error(["PositiveIntegerArg","Argument to %1 must me a positive integer", |
| 349 |
"\\begin{"+begin.name+"}"]); |
| 350 |
} |
| 351 |
while (n > 0) {align += "rl"; spacing.push("0em 0em"); n--} |
| 352 |
spacing = spacing.join(" "); |
| 353 |
if (taggable) {return this.AMSarray(begin,numbered,taggable,align,spacing)} |
| 354 |
var array = this.AMSarray(begin,numbered,taggable,align,spacing); |
| 355 |
return this.setArrayAlign(array,valign); |
| 356 |
}, |
| 357 |
|
| 358 |
/* |
| 359 |
* Handle equation environment |
| 360 |
*/ |
| 361 |
EquationBegin: function (begin,force) { |
| 362 |
this.checkEqnEnv(); |
| 363 |
this.stack.global.forcetag = (force && CONFIG.autoNumber !== "none"); |
| 364 |
return begin; |
| 365 |
}, |
| 366 |
EquationStar: function (begin,row) { |
| 367 |
this.stack.global.tagged = true; // prevent automatic tagging |
| 368 |
return row; |
| 369 |
}, |
| 370 |
|
| 371 |
/* |
| 372 |
* Check for bad nesting of equation environments |
| 373 |
*/ |
| 374 |
checkEqnEnv: function () { |
| 375 |
if (this.stack.global.eqnenv) |
| 376 |
{TEX.Error(["ErroneousNestingEq","Erroneous nesting of equation structures"])} |
| 377 |
this.stack.global.eqnenv = true; |
| 378 |
}, |
| 379 |
|
| 380 |
/* |
| 381 |
* Handle multiple integrals (make a mathop if followed by limits) |
| 382 |
*/ |
| 383 |
MultiIntegral: function (name,integral) { |
| 384 |
var next = this.GetNext(); |
| 385 |
if (next === "\\") { |
| 386 |
var i = this.i; next = this.GetArgument(name); this.i = i; |
| 387 |
if (next === "\\limits") { |
| 388 |
if (name === "\\idotsint") {integral = "\\!\\!\\mathop{\\,\\,"+integral+"}"} |
| 389 |
else {integral = "\\!\\!\\!\\mathop{\\,\\,\\,"+integral+"}"} |
| 390 |
} |
| 391 |
} |
| 392 |
this.string = integral + " " + this.string.slice(this.i); |
| 393 |
this.i = 0; |
| 394 |
}, |
| 395 |
|
| 396 |
/* |
| 397 |
* Handle stretchable arrows |
| 398 |
*/ |
| 399 |
xArrow: function (name,chr,l,r) { |
| 400 |
var def = {width: "+"+(l+r)+"mu", lspace: l+"mu"}; |
| 401 |
var bot = this.GetBrackets(name), |
| 402 |
top = this.ParseArg(name); |
| 403 |
var arrow = MML.mo(MML.chars(String.fromCharCode(chr))).With({ |
| 404 |
stretchy: true, texClass: MML.TEXCLASS.REL |
| 405 |
}); |
| 406 |
var mml = MML.munderover(arrow); |
| 407 |
mml.SetData(mml.over,MML.mpadded(top).With(def).With({voffset:".15em"})); |
| 408 |
if (bot) { |
| 409 |
bot = TEX.Parse(bot,this.stack.env).mml() |
| 410 |
mml.SetData(mml.under,MML.mpadded(bot).With(def).With({voffset:"-.24em"})); |
| 411 |
} |
| 412 |
this.Push(mml.With({subsupOK:true})); |
| 413 |
}, |
| 414 |
|
| 415 |
/* |
| 416 |
* Get a delimiter or empty argument |
| 417 |
*/ |
| 418 |
GetDelimiterArg: function (name) { |
| 419 |
var c = this.trimSpaces(this.GetArgument(name)); |
| 420 |
if (c == "") return null; |
| 421 |
if (c in TEXDEF.delimiter) return c; |
| 422 |
TEX.Error(["MissingOrUnrecognizedDelim","Missing or unrecognized delimiter for %1",name]); |
| 423 |
}, |
| 424 |
|
| 425 |
/* |
| 426 |
* Get a star following a control sequence name, if any |
| 427 |
*/ |
| 428 |
GetStar: function () { |
| 429 |
var star = (this.GetNext() === "*"); |
| 430 |
if (star) {this.i++} |
| 431 |
return star; |
| 432 |
} |
| 433 |
|
| 434 |
}); |
| 435 |
|
| 436 |
/******************************************************************************/ |
| 437 |
|
| 438 |
STACKITEM.Augment({ |
| 439 |
/* |
| 440 |
* Increment equation number and form tag mtd element |
| 441 |
*/ |
| 442 |
autoTag: function () { |
| 443 |
var global = this.global; |
| 444 |
if (!global.notag) { |
| 445 |
AMS.number++; global.tagID = CONFIG.formatNumber(AMS.number.toString()); |
| 446 |
var mml = TEX.Parse("\\text{"+CONFIG.formatTag(global.tagID)+"}",{}).mml(); |
| 447 |
global.tag = MML.mtd(mml).With({id:CONFIG.formatID(global.tagID)}); |
| 448 |
} |
| 449 |
}, |
| 450 |
|
| 451 |
/* |
| 452 |
* Get the tag and record the label, if any |
| 453 |
*/ |
| 454 |
getTag: function () { |
| 455 |
var global = this.global, tag = global.tag; global.tagged = true; |
| 456 |
if (global.label) { |
| 457 |
if (CONFIG.useLabelIds) {tag.id = CONFIG.formatID(global.label)} |
| 458 |
AMS.eqlabels[global.label] = {tag:global.tagID, id:tag.id}; |
| 459 |
} |
| 460 |
// |
| 461 |
// Check for repeated ID's (either in the document or as |
| 462 |
// a previous tag) and find a unique related one. (#240) |
| 463 |
// |
| 464 |
if (document.getElementById(tag.id) || AMS.IDs[tag.id] || AMS.eqIDs[tag.id]) { |
| 465 |
var i = 0, ID; |
| 466 |
do {i++; ID = tag.id+"_"+i} |
| 467 |
while (document.getElementById(ID) || AMS.IDs[ID] || AMS.eqIDs[ID]); |
| 468 |
tag.id = ID; if (global.label) {AMS.eqlabels[global.label].id = ID} |
| 469 |
} |
| 470 |
AMS.eqIDs[tag.id] = 1; |
| 471 |
this.clearTag(); |
| 472 |
return tag; |
| 473 |
}, |
| 474 |
clearTag: function () { |
| 475 |
var global = this.global; |
| 476 |
delete global.tag; delete global.tagID; delete global.label; |
| 477 |
}, |
| 478 |
|
| 479 |
/* |
| 480 |
* If the initial child, skipping any initial space or |
| 481 |
* empty braces (TeXAtom with child being an empty inferred row), |
| 482 |
* is an <mo>, precede it by an empty <mi> to force the <mo> to |
| 483 |
* be infix. |
| 484 |
*/ |
| 485 |
fixInitialMO: function (data) { |
| 486 |
for (var i = 0, m = data.length; i < m; i++) { |
| 487 |
if (data[i] && (data[i].type !== "mspace" && |
| 488 |
(data[i].type !== "texatom" || (data[i].data[0] && data[i].data[0].data.length)))) { |
| 489 |
if (data[i].isEmbellished() || |
| 490 |
(data[i].type === "texatom" && data[i].texClass === MML.TEXCLASS.REL)) data.unshift(MML.mi()); |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
}); |
| 496 |
|
| 497 |
/* |
| 498 |
* Implement multline environment via a STACKITEM |
| 499 |
*/ |
| 500 |
STACKITEM.multline = STACKITEM.array.Subclass({ |
| 501 |
type: "multline", |
| 502 |
Init: function (numbered,stack) { |
| 503 |
this.SUPER(arguments).Init.apply(this); |
| 504 |
this.numbered = (numbered && CONFIG.autoNumber !== "none"); |
| 505 |
this.save = {notag: stack.global.notag}; |
| 506 |
stack.global.tagged = !numbered && !stack.global.forcetag; // prevent automatic tagging in starred environments |
| 507 |
}, |
| 508 |
EndEntry: function () { |
| 509 |
if (this.table.length) {this.fixInitialMO(this.data)} |
| 510 |
var mtd = MML.mtd.apply(MML,this.data); |
| 511 |
if (this.data.shove) {mtd.columnalign = this.data.shove} |
| 512 |
this.row.push(mtd); |
| 513 |
this.data = []; |
| 514 |
}, |
| 515 |
EndRow: function () { |
| 516 |
if (this.row.length != 1) { |
| 517 |
TEX.Error(["MultlineRowsOneCol", |
| 518 |
"The rows within the %1 environment must have exactly one column", |
| 519 |
"multline"]); |
| 520 |
} |
| 521 |
this.table.push(this.row); this.row = []; |
| 522 |
}, |
| 523 |
EndTable: function () { |
| 524 |
this.SUPER(arguments).EndTable.call(this); |
| 525 |
if (this.table.length) { |
| 526 |
var m = this.table.length-1, i, label = -1; |
| 527 |
if (!this.table[0][0].columnalign) {this.table[0][0].columnalign = MML.ALIGN.LEFT} |
| 528 |
if (!this.table[m][0].columnalign) {this.table[m][0].columnalign = MML.ALIGN.RIGHT} |
| 529 |
if (!this.global.tag && this.numbered) {this.autoTag()} |
| 530 |
if (this.global.tag && !this.global.notags) { |
| 531 |
label = (this.arraydef.side === "left" ? 0 : this.table.length - 1); |
| 532 |
this.table[label] = [this.getTag()].concat(this.table[label]); |
| 533 |
} |
| 534 |
for (i = 0, m = this.table.length; i < m; i++) { |
| 535 |
var mtr = (i === label ? MML.mlabeledtr : MML.mtr); |
| 536 |
this.table[i] = mtr.apply(MML,this.table[i]); |
| 537 |
} |
| 538 |
} |
| 539 |
this.global.notag = this.save.notag; |
| 540 |
} |
| 541 |
}); |
| 542 |
|
| 543 |
/* |
| 544 |
* Save data about numbering and taging equations, and add |
| 545 |
* tags at the ends of rows. |
| 546 |
*/ |
| 547 |
STACKITEM.AMSarray = STACKITEM.array.Subclass({ |
| 548 |
type: "AMSarray", |
| 549 |
Init: function (name,numbered,taggable,stack) { |
| 550 |
this.SUPER(arguments).Init.apply(this); |
| 551 |
this.numbered = (numbered && CONFIG.autoNumber !== "none"); |
| 552 |
this.save = {notags: stack.global.notags, notag: stack.global.notag}; |
| 553 |
stack.global.notags = (taggable ? null : name); |
| 554 |
stack.global.tagged = !numbered && taggable && !stack.global.forcetag; // prevent automatic tagging in starred environments |
| 555 |
}, |
| 556 |
EndEntry: function () { |
| 557 |
if (this.row.length % 2 === 1) {this.fixInitialMO(this.data)} |
| 558 |
this.row.push(MML.mtd.apply(MML,this.data)); |
| 559 |
this.data = []; |
| 560 |
}, |
| 561 |
EndRow: function () { |
| 562 |
var mtr = MML.mtr; |
| 563 |
if (!this.global.tag && this.numbered) {this.autoTag()} |
| 564 |
if (!this.global.notags) { |
| 565 |
if (this.global.tag) { |
| 566 |
this.row = [this.getTag()].concat(this.row); |
| 567 |
mtr = MML.mlabeledtr; |
| 568 |
} else { |
| 569 |
this.clearTag(); |
| 570 |
} |
| 571 |
} |
| 572 |
if (this.numbered) {delete this.global.notag} |
| 573 |
this.table.push(mtr.apply(MML,this.row)); this.row = []; |
| 574 |
}, |
| 575 |
EndTable: function () { |
| 576 |
this.SUPER(arguments).EndTable.call(this); |
| 577 |
this.global.notags = this.save.notags; |
| 578 |
this.global.notag = this.save.notag; |
| 579 |
} |
| 580 |
}); |
| 581 |
|
| 582 |
// |
| 583 |
// Look for \tag on a formula and make an mtable to include it |
| 584 |
// |
| 585 |
STACKITEM.start.Augment({ |
| 586 |
oldCheckItem: STACKITEM.start.prototype.checkItem, |
| 587 |
checkItem: function (item) { |
| 588 |
if (item.type === "stop") { |
| 589 |
var mml = this.mmlData(), global = this.global; |
| 590 |
if (AMS.display && !global.tag && !global.tagged && !global.isInner && |
| 591 |
(CONFIG.autoNumber === "all" || global.forcetag)) {this.autoTag()} |
| 592 |
if (global.tag) { |
| 593 |
var row = [this.getTag(),MML.mtd(mml)]; |
| 594 |
var def = { |
| 595 |
side: TEX.config.TagSide, |
| 596 |
minlabelspacing: TEX.config.TagIndent, |
| 597 |
displaystyle: "inherit" // replaced by TeX input jax Translate() function with actual value |
| 598 |
}; |
| 599 |
mml = MML.mtable(MML.mlabeledtr.apply(MML,row)).With(def); |
| 600 |
} |
| 601 |
return STACKITEM.mml(mml); |
| 602 |
} |
| 603 |
return this.oldCheckItem.call(this,item); |
| 604 |
} |
| 605 |
}); |
| 606 |
|
| 607 |
/******************************************************************************/ |
| 608 |
|
| 609 |
/* |
| 610 |
* Add pre- and post-filters to handle the equation number maintenance. |
| 611 |
*/ |
| 612 |
TEX.prefilterHooks.Add(function (data) { |
| 613 |
AMS.display = data.display; |
| 614 |
AMS.number = AMS.startNumber; // reset equation numbers (in case the equation restarted) |
| 615 |
AMS.eqlabels = {}; |
| 616 |
AMS.eqIDs = {}; |
| 617 |
AMS.badref = false; |
| 618 |
if (AMS.refUpdate) {AMS.number = data.script.MathJax.startNumber} |
| 619 |
}); |
| 620 |
TEX.postfilterHooks.Add(function (data) { |
| 621 |
data.script.MathJax.startNumber = AMS.startNumber; |
| 622 |
AMS.startNumber = AMS.number; // equation numbers for next equation |
| 623 |
MathJax.Hub.Insert(AMS.IDs,AMS.eqIDs); // save IDs from this equation |
| 624 |
MathJax.Hub.Insert(AMS.labels,AMS.eqlabels); // save labels from this equation |
| 625 |
if (AMS.badref && !data.math.texError) {AMS.refs.push(data.script)} // reprocess later |
| 626 |
},100); |
| 627 |
|
| 628 |
MathJax.Hub.Register.MessageHook("Begin Math Input",function () { |
| 629 |
AMS.refs = []; // array of jax with bad references |
| 630 |
AMS.refUpdate = false; |
| 631 |
}); |
| 632 |
MathJax.Hub.Register.MessageHook("End Math Input",function (message) { |
| 633 |
if (AMS.refs.length) { |
| 634 |
AMS.refUpdate = true; |
| 635 |
for (var i = 0, m = AMS.refs.length; i < m; i++) |
| 636 |
{AMS.refs[i].MathJax.state = MathJax.ElementJax.STATE.UPDATE} |
| 637 |
return MathJax.Hub.processInput({ |
| 638 |
scripts:AMS.refs, |
| 639 |
start: new Date().getTime(), |
| 640 |
i:0, j:0, jax:{}, jaxIDs:[] |
| 641 |
}); |
| 642 |
} |
| 643 |
return null; |
| 644 |
}); |
| 645 |
|
| 646 |
// |
| 647 |
// Clear the equation numbers and labels |
| 648 |
// |
| 649 |
TEX.resetEquationNumbers = function (n,keepLabels) { |
| 650 |
AMS.startNumber = (n || 0); |
| 651 |
if (!keepLabels) { |
| 652 |
AMS.labels = {}; |
| 653 |
AMS.IDs = {}; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
/******************************************************************************/ |
| 658 |
|
| 659 |
MathJax.Hub.Startup.signal.Post("TeX AMSmath Ready"); |
| 660 |
|
| 661 |
}); |
| 662 |
|
| 663 |
MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/AMSmath.js"); |