cancel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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-2013 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.2",
  34. //
  35. // The attributes allowed in \enclose{notation}[attributes]{math}
  36. //
  37. ALLOWED: {
  38. arrow: 1,
  39. color: 1, mathcolor: 1,
  40. background: 1, mathbackground: 1,
  41. padding: 1,
  42. thickness: 1
  43. }
  44. };
  45. MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  46. var TEX = MathJax.InputJax.TeX,
  47. MML = MathJax.ElementJax.mml,
  48. CANCEL = MathJax.Extension["TeX/cancel"];
  49. CANCEL.setAttributes = function (def,attr) {
  50. if (attr !== "") {
  51. attr = attr.replace(/ /g,"").split(/,/);
  52. for (var i = 0, m = attr.length; i < m; i++) {
  53. var keyvalue = attr[i].split(/[:=]/);
  54. if (CANCEL.ALLOWED[keyvalue[0]]) {
  55. if (keyvalue[1] === "true") {keyvalue[1] = true}
  56. if (keyvalue[1] === "false") {keyvalue[1] = false}
  57. def[keyvalue[0]] = keyvalue[1];
  58. }
  59. }
  60. }
  61. return def;
  62. };
  63. //
  64. // Set up macros
  65. //
  66. TEX.Definitions.Add({
  67. macros: {
  68. cancel: ['Cancel',MML.NOTATION.UPDIAGONALSTRIKE],
  69. bcancel: ['Cancel',MML.NOTATION.DOWNDIAGONALSTRIKE],
  70. xcancel: ['Cancel',MML.NOTATION.UPDIAGONALSTRIKE+" "+MML.NOTATION.DOWNDIAGONALSTRIKE],
  71. cancelto: 'CancelTo'
  72. }
  73. },null,true);
  74. TEX.Parse.Augment({
  75. //
  76. // Implement \cancel[attributes]{math},
  77. // \bcancel[attributes]{math}, and
  78. // \xcancel[attributes]{math}
  79. //
  80. Cancel: function(name,notation) {
  81. var attr = this.GetBrackets(name,""), math = this.ParseArg(name);
  82. var def = CANCEL.setAttributes({notation: notation},attr);
  83. this.Push(MML.menclose(math).With(def));
  84. },
  85. //
  86. // Implement \cancelto{value}[attributes]{math}
  87. //
  88. CancelTo: function(name,notation) {
  89. var value = this.ParseArg(name),
  90. attr = this.GetBrackets(name,""),
  91. math = this.ParseArg(name);
  92. var def = CANCEL.setAttributes({notation: MML.NOTATION.UPDIAGONALSTRIKE, arrow:true},attr);
  93. value = MML.mpadded(value).With({depth:"-.1em",height:"+.1em",voffset:".1em"});
  94. this.Push(MML.msup(MML.menclose(math).With(def),value));
  95. }
  96. });
  97. MathJax.Hub.Startup.signal.Post("TeX cancel Ready");
  98. });
  99. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/cancel.js");