cancel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/TeX/cancel.js
  6. *
  7. * Implements the \cancel, \bcancel, \xcancel, and \cancelto macros.
  8. *
  9. * Usage:
  10. *
  11. * \cancel{math} % strikeout math from lower left to upper right
  12. * \bcancel{math} % strikeout from upper left to lower right
  13. * \xcancel{math} % strikeout with an X
  14. * \cancelto{value}{math} % strikeout with arrow going to value
  15. *
  16. * ---------------------------------------------------------------------
  17. *
  18. * Copyright (c) 2011-2015 The MathJax Consortium
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License");
  21. * you may not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an "AS IS" BASIS,
  28. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. */
  32. MathJax.Extension["TeX/cancel"] = {
  33. version: "2.5.0",
  34. //
  35. // The attributes allowed in \enclose{notation}[attributes]{math}
  36. //
  37. ALLOWED: {
  38. color: 1, mathcolor: 1,
  39. background: 1, mathbackground: 1,
  40. padding: 1,
  41. thickness: 1
  42. }
  43. };
  44. MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  45. var TEX = MathJax.InputJax.TeX,
  46. MML = MathJax.ElementJax.mml,
  47. CANCEL = MathJax.Extension["TeX/cancel"];
  48. CANCEL.setAttributes = function (def,attr) {
  49. if (attr !== "") {
  50. attr = attr.replace(/ /g,"").split(/,/);
  51. for (var i = 0, m = attr.length; i < m; i++) {
  52. var keyvalue = attr[i].split(/[:=]/);
  53. if (CANCEL.ALLOWED[keyvalue[0]]) {
  54. if (keyvalue[1] === "true") {keyvalue[1] = true}
  55. if (keyvalue[1] === "false") {keyvalue[1] = false}
  56. def[keyvalue[0]] = keyvalue[1];
  57. }
  58. }
  59. }
  60. return def;
  61. };
  62. //
  63. // Set up macros
  64. //
  65. TEX.Definitions.Add({
  66. macros: {
  67. cancel: ['Cancel',MML.NOTATION.UPDIAGONALSTRIKE],
  68. bcancel: ['Cancel',MML.NOTATION.DOWNDIAGONALSTRIKE],
  69. xcancel: ['Cancel',MML.NOTATION.UPDIAGONALSTRIKE+" "+MML.NOTATION.DOWNDIAGONALSTRIKE],
  70. cancelto: 'CancelTo'
  71. }
  72. },null,true);
  73. TEX.Parse.Augment({
  74. //
  75. // Implement \cancel[attributes]{math},
  76. // \bcancel[attributes]{math}, and
  77. // \xcancel[attributes]{math}
  78. //
  79. Cancel: function(name,notation) {
  80. var attr = this.GetBrackets(name,""), math = this.ParseArg(name);
  81. var def = CANCEL.setAttributes({notation: notation},attr);
  82. this.Push(MML.menclose(math).With(def));
  83. },
  84. //
  85. // Implement \cancelto{value}[attributes]{math}
  86. //
  87. CancelTo: function(name,notation) {
  88. var value = this.ParseArg(name),
  89. attr = this.GetBrackets(name,""),
  90. math = this.ParseArg(name);
  91. var def = CANCEL.setAttributes({notation: MML.NOTATION.UPDIAGONALSTRIKE+" "+MML.NOTATION.UPDIAGONALARROW},attr);
  92. value = MML.mpadded(value).With({depth:"-.1em",height:"+.1em",voffset:".1em"});
  93. this.Push(MML.msup(MML.menclose(math).With(def),value));
  94. }
  95. });
  96. MathJax.Hub.Startup.signal.Post("TeX cancel Ready");
  97. });
  98. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/cancel.js");