| 1 |
/************************************************************* |
| 2 |
* |
| 3 |
* MathJax/extensions/TeX/text-macros.js |
| 4 |
* |
| 5 |
* Implements the processing of some text-mode macros inside |
| 6 |
* \text{} and other text boxes. |
| 7 |
* |
| 8 |
* --------------------------------------------------------------------- |
| 9 |
* |
| 10 |
* Copyright (c) 2018-2020 The MathJax Consortium |
| 11 |
* |
| 12 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 |
* you may not use this file except in compliance with the License. |
| 14 |
* You may obtain a copy of the License at |
| 15 |
* |
| 16 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 17 |
* |
| 18 |
* Unless required by applicable law or agreed to in writing, software |
| 19 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 20 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 |
* See the License for the specific language governing permissions and |
| 22 |
* limitations under the License. |
| 23 |
*/ |
| 24 |
|
| 25 |
MathJax.Extension["TeX/text-macros"] = { |
| 26 |
version: "2.7.9" |
| 27 |
}; |
| 28 |
|
| 29 |
MathJax.Hub.Register.StartupHook("TeX Jax Ready", function () { |
| 30 |
var MML = MathJax.ElementJax.mml; |
| 31 |
var TEX = MathJax.InputJax.TeX; |
| 32 |
var TEXDEF = TEX.Definitions; |
| 33 |
|
| 34 |
TEX.Parse.Augment({ |
| 35 |
// |
| 36 |
// Replace InternalMath with parser that handles some text macros |
| 37 |
// |
| 38 |
InternalMath: function(text,level) { |
| 39 |
var mml = TextParser(text, {}).Parse(); |
| 40 |
if (level != null) { |
| 41 |
mml = [MML.mstyle.apply(MML,mml).With({displaystyle:false, scriptlevel:level})]; |
| 42 |
} else if (mml.length > 1) { |
| 43 |
mml = [MML.mrow.apply(MML,mml)]; |
| 44 |
} |
| 45 |
return mml; |
| 46 |
}, |
| 47 |
|
| 48 |
// |
| 49 |
// Correctly skip newline as well as comment |
| 50 |
// |
| 51 |
Comment: function (c) { |
| 52 |
while (this.i < this.string.length && this.string.charAt(this.i) != "\n") {this.i++} |
| 53 |
this.i++; |
| 54 |
}, |
| 55 |
|
| 56 |
// |
| 57 |
// Correctly skip trailing space as well |
| 58 |
// |
| 59 |
GetCS: function () { |
| 60 |
var CS = this.string.slice(this.i).match(/^([a-z]+|.) ?/i); |
| 61 |
if (CS) {this.i += CS[0].length; return CS[1]} else {this.i++; return " "} |
| 62 |
} |
| 63 |
|
| 64 |
}); |
| 65 |
|
| 66 |
// |
| 67 |
// The text parser is a subclass of the math parser, so we can use |
| 68 |
// some of the existing methods (like GetArgument()), and some of the |
| 69 |
// control sequence implementations (like Macro, Spacer, etc.) |
| 70 |
// |
| 71 |
var TextParser = TEX.TextParser = TEX.Parse.Subclass({ |
| 72 |
|
| 73 |
Init: function (text, env) { |
| 74 |
this.env = MathJax.Hub.Insert({},env); |
| 75 |
this.stack = {env: this.env}; |
| 76 |
this.string = text; |
| 77 |
this.i = 0; |
| 78 |
this.mml = []; // the accumulated MathML elements |
| 79 |
this.text = ''; // the accumulated text so far |
| 80 |
}, |
| 81 |
|
| 82 |
// |
| 83 |
// These are the special characters in text mode |
| 84 |
// |
| 85 |
textSpecial: { |
| 86 |
'\\': 'ControlSequence', |
| 87 |
'$': 'Math', |
| 88 |
'%': 'Comment', |
| 89 |
'^': 'MathModeOnly', |
| 90 |
'_': 'MathModeOnly', |
| 91 |
'&': 'Misplaced', |
| 92 |
'#': 'Misplaced', |
| 93 |
'~': 'Tilde', |
| 94 |
' ': 'Space', |
| 95 |
'\t': 'Space', |
| 96 |
'\r': 'Space', |
| 97 |
'\n': 'Space', |
| 98 |
'\u00A0': 'Tilde', |
| 99 |
'{': 'OpenBrace', |
| 100 |
'}': 'CloseBrace', |
| 101 |
'`': 'OpenQuote', |
| 102 |
"'": 'CloseQuote' |
| 103 |
}, |
| 104 |
|
| 105 |
// |
| 106 |
// These are text-mode macros we support |
| 107 |
// |
| 108 |
textMacros: { |
| 109 |
'(': 'Math', |
| 110 |
|
| 111 |
'$': 'SelfQuote', |
| 112 |
'_': 'SelfQuote', |
| 113 |
'%': 'SelfQuote', |
| 114 |
'{': 'SelfQuote', |
| 115 |
'}': 'SelfQuote', |
| 116 |
' ': 'SelfQuote', |
| 117 |
'&': 'SelfQuote', |
| 118 |
'#': 'SelfQuote', |
| 119 |
'\\': 'SelfQuote', |
| 120 |
|
| 121 |
"'": ['Accent', '\u00B4'], |
| 122 |
'`': ['Accent', '\u0060'], |
| 123 |
'^': ['Accent', '^'], |
| 124 |
'"': ['Accent', '\u00A8'], |
| 125 |
'~': ['Accent', '~'], |
| 126 |
'=': ['Accent', '\u00AF'], |
| 127 |
'.': ['Accent', '\u02D9'], |
| 128 |
'u': ['Accent', '\u02D8'], |
| 129 |
'v': ['Accent', '\u02C7'], |
| 130 |
|
| 131 |
emph: 'Emph', |
| 132 |
rm: ['SetFont',MML.VARIANT.NORMAL], |
| 133 |
mit: ['SetFont',MML.VARIANT.ITALIC], |
| 134 |
oldstyle: ['SetFont',MML.VARIANT.OLDSTYLE], |
| 135 |
cal: ['SetFont',MML.VARIANT.CALIGRAPHIC], |
| 136 |
it: ['SetFont','-tex-mathit'], // needs special handling |
| 137 |
bf: ['SetFont',MML.VARIANT.BOLD], |
| 138 |
bbFont: ['SetFont',MML.VARIANT.DOUBLESTRUCK], |
| 139 |
scr: ['SetFont',MML.VARIANT.SCRIPT], |
| 140 |
frak: ['SetFont',MML.VARIANT.FRAKTUR], |
| 141 |
sf: ['SetFont',MML.VARIANT.SANSSERIF], |
| 142 |
tt: ['SetFont',MML.VARIANT.MONOSPACE], |
| 143 |
|
| 144 |
tiny: ['SetSize',0.5], |
| 145 |
Tiny: ['SetSize',0.6], // non-standard |
| 146 |
scriptsize: ['SetSize',0.7], |
| 147 |
small: ['SetSize',0.85], |
| 148 |
normalsize: ['SetSize',1.0], |
| 149 |
large: ['SetSize',1.2], |
| 150 |
Large: ['SetSize',1.44], |
| 151 |
LARGE: ['SetSize',1.73], |
| 152 |
huge: ['SetSize',2.07], |
| 153 |
Huge: ['SetSize',2.49], |
| 154 |
|
| 155 |
mathcal: 'MathModeOnly', |
| 156 |
mathscr: 'MathModeOnly', |
| 157 |
mathrm: 'MathModeOnly', |
| 158 |
mathbf: 'MathModeOnly', |
| 159 |
mathbb: 'MathModeOnly', |
| 160 |
mathit: 'MathModeOnly', |
| 161 |
mathfrak: 'MathModeOnly', |
| 162 |
mathsf: 'MathModeOnly', |
| 163 |
mathtt: 'MathModeOnly', |
| 164 |
Bbb: ['Macro','{\\bbFont #1}',1], |
| 165 |
textrm: ['Macro','{\\rm #1}',1], |
| 166 |
textit: ['Macro','{\\it #1}',1], |
| 167 |
textbf: ['Macro','{\\bf #1}',1], |
| 168 |
textsf: ['Macro','{\\sf #1}',1], |
| 169 |
texttt: ['Macro','{\\tt #1}',1], |
| 170 |
|
| 171 |
dagger: ['Insert', '\u2020'], |
| 172 |
ddagger: ['Insert', '\u2021'], |
| 173 |
S: ['Insert', '\u00A7'] |
| 174 |
}, |
| 175 |
|
| 176 |
// |
| 177 |
// These are the original macros that are allowed in text mode |
| 178 |
// |
| 179 |
useMathMacros: { |
| 180 |
',': true, |
| 181 |
':': true, |
| 182 |
'>': true, |
| 183 |
';': true, |
| 184 |
'!': true, |
| 185 |
enspace: true, |
| 186 |
quad: true, |
| 187 |
qquad: true, |
| 188 |
thinspace: true, |
| 189 |
negthinspace: true, |
| 190 |
|
| 191 |
hskip: true, |
| 192 |
hspace: true, |
| 193 |
kern: true, |
| 194 |
mskip: true, |
| 195 |
mspace: true, |
| 196 |
mkern: true, |
| 197 |
rule: true, |
| 198 |
Rule: true, |
| 199 |
Space: true, |
| 200 |
|
| 201 |
color: true, |
| 202 |
href: true, |
| 203 |
unicode: true, |
| 204 |
|
| 205 |
ref: true, |
| 206 |
eqref: true |
| 207 |
}, |
| 208 |
|
| 209 |
// |
| 210 |
// Look through the text for special characters and process them. |
| 211 |
// Save any accumulated text aat the end and return the MathML |
| 212 |
// elements produced. |
| 213 |
// |
| 214 |
Parse: function () { |
| 215 |
var c; |
| 216 |
while ((c = this.string.charAt(this.i++))) { |
| 217 |
if (this.textSpecial.hasOwnProperty(c)) { |
| 218 |
this[this.textSpecial[c]](c); |
| 219 |
} else { |
| 220 |
this.text += c; |
| 221 |
} |
| 222 |
} |
| 223 |
this.SaveText(); |
| 224 |
return this.mml; |
| 225 |
}, |
| 226 |
|
| 227 |
// |
| 228 |
// Handle a control sequence name |
| 229 |
// If it is a text-mode macro, use it. |
| 230 |
// Otherwise look for it in the math-mode lists |
| 231 |
// Report an error if it is not there |
| 232 |
// Otherwise check if it is a macro or one of the allowed control sequences |
| 233 |
// Run the macro (with arguments if given) |
| 234 |
// |
| 235 |
ControlSequence: function (c) { |
| 236 |
var cs = this.GetCS(), name = c + cs, cmd; |
| 237 |
if (this.textMacros.hasOwnProperty(cs)) { |
| 238 |
cmd = this.textMacros[cs]; |
| 239 |
} else { |
| 240 |
cmd = this.LookupCS(cs); |
| 241 |
if (!cmd) { |
| 242 |
this.Error(["UndefinedControlSequence","Undefined control sequence %1",name]); |
| 243 |
} |
| 244 |
if ((!(cmd instanceof Array) || cmd[0] !== 'Macro') && |
| 245 |
!this.useMathMacros.hasOwnProperty(cs)) { |
| 246 |
this.Error(["MathMacro","'%1' is only supported in math mode",name]); |
| 247 |
} |
| 248 |
} |
| 249 |
if (cmd instanceof Array) { |
| 250 |
if (!this.hasOwnProperty[cmd[0]]) this.SaveText(); |
| 251 |
this[cmd[0]].apply(this,[name].concat(cmd.slice(1))); |
| 252 |
} else { |
| 253 |
if (!this.hasOwnProperty[cmd]) this.SaveText(); |
| 254 |
this[cmd].call(this,name); |
| 255 |
} |
| 256 |
}, |
| 257 |
|
| 258 |
// |
| 259 |
// Lookup the CS as a math-mode macro |
| 260 |
// |
| 261 |
LookupCS: function(cs) { |
| 262 |
if (TEXDEF.macros.hasOwnProperty(cs)) return TEXDEF.macros[cs]; |
| 263 |
if (TEXDEF.mathchar0mi.hasOwnProperty(cs)) return TEXDEF.mathchar0mi[cs]; |
| 264 |
if (TEXDEF.mathchar0mo.hasOwnProperty(cs)) return TEXDEF.mathchar0mo[cs]; |
| 265 |
if (TEXDEF.mathchar7.hasOwnProperty(cs)) return TEXDEF.mathchar7[cs]; |
| 266 |
if (TEXDEF.delimiter.hasOwnProperty('\\'+cs)) return TEXDEF.delimiter['\\'+cs]; |
| 267 |
return null; |
| 268 |
}, |
| 269 |
|
| 270 |
// |
| 271 |
// Handle internal math mode |
| 272 |
// Look for the close delimiter and process the contents |
| 273 |
// |
| 274 |
Math: function (open) { |
| 275 |
this.SaveText(); |
| 276 |
var i = this.i, j; |
| 277 |
var braces = 0, c; |
| 278 |
while ((c = this.GetNext())) { |
| 279 |
j = this.i++; |
| 280 |
switch(c) { |
| 281 |
case '\\': |
| 282 |
var cs = this.GetCS(); |
| 283 |
if (cs === ')') c = '\\('; |
| 284 |
case '$': |
| 285 |
if (braces === 0 && open === c) { |
| 286 |
this.Push(TEX.Parse(this.string.substr(i, j-i),this.env).mml()); |
| 287 |
return; |
| 288 |
} |
| 289 |
break; |
| 290 |
|
| 291 |
case '{': |
| 292 |
braces++; |
| 293 |
break; |
| 294 |
|
| 295 |
case '}': |
| 296 |
if (braces == 0) { |
| 297 |
this.Error(["ExtraCloseMissingOpen","Extra close brace or missing open brace"]); |
| 298 |
} |
| 299 |
braces--; |
| 300 |
break; |
| 301 |
} |
| 302 |
} |
| 303 |
this.Error(["MathNotTerminated","Math not terminated in text box"]); |
| 304 |
}, |
| 305 |
|
| 306 |
// |
| 307 |
// Character can only be used in math mode |
| 308 |
// |
| 309 |
MathModeOnly: function (c) { |
| 310 |
this.Error(["MathModeOnly","'%1' allowed only in math mode",c]); |
| 311 |
}, |
| 312 |
|
| 313 |
// |
| 314 |
// Character is being used out of place |
| 315 |
// |
| 316 |
Misplaced: function (c) { |
| 317 |
this.Error(["Misplaced","'%1' can not be used here",c]); |
| 318 |
}, |
| 319 |
|
| 320 |
// |
| 321 |
// Braces start new environments |
| 322 |
// |
| 323 |
OpenBrace: function (c) { |
| 324 |
var env = this.env; |
| 325 |
this.env = MathJax.Hub.Insert({}, env); |
| 326 |
this.env.oldEnv = env; |
| 327 |
}, |
| 328 |
CloseBrace: function (c) { |
| 329 |
if (this.env.oldEnv) { |
| 330 |
this.SaveText(); |
| 331 |
this.env = this.env.oldEnv; |
| 332 |
} else { |
| 333 |
this.Error(["ExtraCloseMissingOpen","Extra close brace or missing open brace"]); |
| 334 |
} |
| 335 |
}, |
| 336 |
|
| 337 |
// |
| 338 |
// Handle open and close quotes |
| 339 |
// |
| 340 |
OpenQuote: function (c) { |
| 341 |
if (this.string.charAt(this.i) === c) { |
| 342 |
this.text += "\u201C"; |
| 343 |
this.i++; |
| 344 |
} else { |
| 345 |
this.text += "\u2018" |
| 346 |
} |
| 347 |
}, |
| 348 |
CloseQuote: function (c) { |
| 349 |
if (this.string.charAt(this.i) === c) { |
| 350 |
this.text += "\u201D"; |
| 351 |
this.i++; |
| 352 |
} else { |
| 353 |
this.text += "\u2019" |
| 354 |
} |
| 355 |
}, |
| 356 |
|
| 357 |
// |
| 358 |
// Handle non-breaking and regular spaces |
| 359 |
// |
| 360 |
Tilde: function (c) { |
| 361 |
this.text += '\u00A0'; |
| 362 |
}, |
| 363 |
Space: function (c) { |
| 364 |
this.text += ' '; |
| 365 |
while (this.GetNext().match(/\s/)) this.i++; |
| 366 |
}, |
| 367 |
|
| 368 |
// |
| 369 |
// Insert the escaped characer |
| 370 |
// |
| 371 |
SelfQuote: function (name) { |
| 372 |
this.text += name.substr(1); |
| 373 |
}, |
| 374 |
|
| 375 |
// |
| 376 |
// Insert a given character |
| 377 |
// |
| 378 |
Insert: function (name, c) { |
| 379 |
this.text += c; |
| 380 |
}, |
| 381 |
|
| 382 |
// |
| 383 |
// Create an accented character using mover |
| 384 |
// |
| 385 |
Accent: function (name, c) { |
| 386 |
this.SaveText(); |
| 387 |
var base = this.ParseArg(name); |
| 388 |
var accent = MML.mo(MML.chars(c)); |
| 389 |
if (this.env.mathvariant) accent.mathvariant = this.env.mathvariant; |
| 390 |
this.Push(MML.mover(base,accent)); |
| 391 |
}, |
| 392 |
|
| 393 |
// |
| 394 |
// Switch to/from italics |
| 395 |
// |
| 396 |
Emph: function (name) { |
| 397 |
this.UseFont(name, this.env.mathvariant === '-tex-mathit' ? 'normal' : '-tex-mathit'); |
| 398 |
}, |
| 399 |
|
| 400 |
// |
| 401 |
// Use a given font on its argument |
| 402 |
// |
| 403 |
UseFont: function (name, variant) { |
| 404 |
this.SaveText(); |
| 405 |
this.Push(this.ParseTextArg(name,{mathvariant: variant})); |
| 406 |
}, |
| 407 |
|
| 408 |
// |
| 409 |
// Set a font for the rest of the text |
| 410 |
// |
| 411 |
SetFont: function (name, variant) { |
| 412 |
this.SaveText(); |
| 413 |
this.env.mathvariant = variant; |
| 414 |
}, |
| 415 |
|
| 416 |
// |
| 417 |
// Set the size to use |
| 418 |
// |
| 419 |
SetSize: function (name, size) { |
| 420 |
this.SaveText(); |
| 421 |
this.env.mathsize = size; |
| 422 |
}, |
| 423 |
|
| 424 |
// |
| 425 |
// Process the argument as text with the given environment settings |
| 426 |
// |
| 427 |
ParseTextArg: function (name, env) { |
| 428 |
var text = this.GetArgument(name); |
| 429 |
env = MathJax.Hub.Insert(MathJax.Hub.Insert({}, this.env), env); |
| 430 |
delete env.oldEnv; |
| 431 |
return TextParser(text, env).Parse(); |
| 432 |
}, |
| 433 |
|
| 434 |
// |
| 435 |
// Process an argument as text (overrides the math-mode version) |
| 436 |
// |
| 437 |
ParseArg: function (name) { |
| 438 |
var mml = TextParser(this.GetArgument(name), this.env).Parse(); |
| 439 |
if (mml.length === 0) return mml[0]; |
| 440 |
return MML.mrow.apply(MML.mrow, mml); |
| 441 |
}, |
| 442 |
|
| 443 |
// |
| 444 |
// Create an mtext element with the accumulated text, if any |
| 445 |
// and set it variant |
| 446 |
// |
| 447 |
SaveText: function () { |
| 448 |
if (this.text) { |
| 449 |
var text = MML.mtext(MML.chars(this.text)); |
| 450 |
if (this.env.mathvariant) text.mathvariant = this.env.mathvariant; |
| 451 |
this.Push(text); |
| 452 |
} |
| 453 |
this.text = ""; |
| 454 |
}, |
| 455 |
|
| 456 |
// |
| 457 |
// Save a MathML element or array, setting its size and color, if any |
| 458 |
// |
| 459 |
Push: function (mml) { |
| 460 |
if (mml instanceof Array) { |
| 461 |
if (this.env.mathsize || this.env.mathcolor) { |
| 462 |
mml = MML.mstyle.apply(MML,mml); |
| 463 |
if (this.env.mathsize) mml.mathsize = this.env.mathsize; |
| 464 |
if (this.env.mathcolor) mml.mathcolor = this.env.mathcolor; |
| 465 |
} |
| 466 |
this.mml.push.apply(this.mml,mml); |
| 467 |
} else { |
| 468 |
if (this.env.mathsize && !mml.mathsize) mml.mathsize = this.env.mathsize; |
| 469 |
if (this.env.mathcolor && !mml.mathcolor) mml.mathcolor = this.env.mathcolor; |
| 470 |
this.mml.push(mml); |
| 471 |
} |
| 472 |
}, |
| 473 |
|
| 474 |
// |
| 475 |
// Throw an error |
| 476 |
// |
| 477 |
Error: function (message) { |
| 478 |
TEX.Error(message); |
| 479 |
} |
| 480 |
|
| 481 |
}); |
| 482 |
|
| 483 |
MathJax.Hub.Startup.signal.Post('TeX text-macros Ready'); |
| 484 |
|
| 485 |
}); |
| 486 |
|
| 487 |
MathJax.Ajax.loadComplete('[MathJax]/extensions/TeX/text-macros.js'); |