mhchem.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 0a1
  2. > /* eslint-disable */
  3. 5a7,22
  4. > * KaTeX mhchem.js
  5. > *
  6. > * This file implements a KaTeX version of mhchem version 3.3.0.
  7. > * It is adapted from MathJax/extensions/TeX/mhchem.js
  8. > * It differs from the MathJax version as follows:
  9. > * 1. The interface is changed so that it can be called from KaTeX, not MathJax.
  10. > * 2. \rlap and \llap are replaced with \mathrlap and \mathllap.
  11. > * 3. Four lines of code are edited in order to use \raisebox instead of \raise.
  12. > * 4. The reaction arrow code is simplified. All reaction arrows are rendered
  13. > * using KaTeX extensible arrows instead of building non-extensible arrows.
  14. > * 5. \tripledash vertical alignment is slightly adjusted.
  15. > *
  16. > * This code, as other KaTeX code, is released under the MIT license.
  17. > *
  18. > * /*************************************************************
  19. > *
  20. 33a51
  21. > // version: "3.3.0" for MathJax and KaTeX
  22. 35,37d52
  23. < MathJax.Extension["TeX/mhchem"] = {
  24. < version: "3.3.0"
  25. < };
  26. 39c54
  27. < MathJax.Hub.Register.StartupHook("TeX Jax Ready", function () {
  28. ---
  29. > // Add \ce, \pu, and \tripledash to the KaTeX macros.
  30. 41c56,58
  31. < var TEX = MathJax.InputJax.TeX;
  32. ---
  33. > katex.__defineMacro("\\ce", function(context) {
  34. > return chemParse(context.consumeArgs(1)[0], "ce")
  35. > });
  36. 43,47c60,62
  37. < //
  38. < // This is the main class for handing the \ce and related commands.
  39. < // Its main method is Parse() which takes the argument to \ce and
  40. < // returns the corresponding TeX string.
  41. < //
  42. ---
  43. > katex.__defineMacro("\\pu", function(context) {
  44. > return chemParse(context.consumeArgs(1)[0], "pu");
  45. > });
  46. 49,50c64,68
  47. < var CE = MathJax.Object.Subclass({
  48. < string: "", // the \ce string being parsed
  49. ---
  50. > // Needed for \bond for the ~ forms
  51. > // Raise by 2.56mu, not 2mu. We're raising a hyphen-minus, U+002D, not
  52. > // a mathematical minus, U+2212. So we need that extra 0.56.
  53. > katex.__defineMacro("\\tripledash", "{\\vphantom{-}\\raisebox{2.56mu}{$\\mkern2mu"
  54. > + "\\tiny\\text{-}\\mkern1mu\\text{-}\\mkern1mu\\text{-}\\mkern2mu$}}");
  55. 52,55c70
  56. < //
  57. < // Store the string when a CE object is created
  58. < //
  59. < Init: function (string) { this.string = string; },
  60. ---
  61. > import katex from "katex";
  62. 57,64c72,85
  63. < //
  64. < // This converts the CE string to a TeX string.
  65. < //
  66. < Parse: function (stateMachine) {
  67. < try {
  68. < return texify.go(mhchemParser.go(this.string, stateMachine));
  69. < } catch (ex) {
  70. < TEX.Error(ex);
  71. ---
  72. > //
  73. > // This is the main function for handing the \ce and \pu commands.
  74. > // It takes the argument to \ce or \pu and returns the corresponding TeX string.
  75. > //
  76. >
  77. > var chemParse = function (tokens, stateMachine) {
  78. > // Recreate the argument string from KaTeX's array of tokens.
  79. > var str = "";
  80. > var expectedLoc = tokens[tokens.length - 1].loc.start
  81. > for (var i = tokens.length - 1; i >= 0; i--) {
  82. > if(tokens[i].loc.start > expectedLoc) {
  83. > // context.consumeArgs has eaten a space.
  84. > str += " ";
  85. > expectedLoc = tokens[i].loc.start;
  86. 65a87,88
  87. > str += tokens[i].text;
  88. > expectedLoc += tokens[i].text.length;
  89. 67c90,92
  90. < });
  91. ---
  92. > var tex = texify.go(mhchemParser.go(str, stateMachine));
  93. > return tex;
  94. > };
  95. 1405,1406c1430,1431
  96. < res += "^{\\smash[t]{\\vphantom{2}}\\llap{"+(b5.b||"")+"}}";
  97. < res += "_{\\vphantom{2}\\llap{\\smash[t]{"+(b5.p||"")+"}}}";
  98. ---
  99. > res += "^{\\smash[t]{\\vphantom{2}}\\mathllap{"+(b5.b||"")+"}}";
  100. > res += "_{\\vphantom{2}\\mathllap{\\smash[t]{"+(b5.p||"")+"}}}";
  101. 1508,1520c1533,1536
  102. < var arrow = texify._getArrow(buf.r);
  103. < if (b6.rd || b6.rq) {
  104. < if (buf.r === "<=>" || buf.r === "<=>>" || buf.r === "<<=>" || buf.r === "<-->") {
  105. < // arrows that cannot stretch correctly yet, https://github.com/mathjax/MathJax/issues/1491
  106. < arrow = "\\long"+arrow;
  107. < if (b6.rd) { arrow = "\\overset{"+b6.rd+"}{"+arrow+"}"; }
  108. < if (b6.rq) { arrow = "\\underset{\\lower7mu{"+b6.rq+"}}{"+arrow+"}"; }
  109. < arrow = " {}\\mathrel{"+arrow+"}{} ";
  110. < } else {
  111. < if (b6.rq) { arrow += "[{"+b6.rq+"}]"; }
  112. < arrow += "{"+b6.rd+"}";
  113. < arrow = " {}\\mathrel{\\x"+arrow+"}{} ";
  114. < }
  115. ---
  116. > var arrow = "\\x" + texify._getArrow(buf.r);
  117. > if (b6.rq) { arrow += "[{" + b6.rq + "}]"; }
  118. > if (b6.rd) {
  119. > arrow += "{" + b6.rd + "}";
  120. 1522c1538
  121. < arrow = " {}\\mathrel{\\long"+arrow+"}{} ";
  122. ---
  123. > arrow += "{}";
  124. 1615c1631
  125. < case "<-->": return "leftrightarrows";
  126. ---
  127. > case "<-->": return "rightleftarrows";
  128. 1618,1619c1634,1635
  129. < case "<=>>": return "Rightleftharpoons";
  130. < case "<<=>": return "Leftrightharpoons";
  131. ---
  132. > case "<=>>": return "rightequilibrium";
  133. > case "<<=>": return "leftequilibrium";
  134. 1634,1637c1650,1653
  135. < case "~-": return "{\\rlap{\\lower.1em{-}}\\raise.1em{\\tripledash}}";
  136. < case "~=": return "{\\rlap{\\lower.2em{-}}\\rlap{\\raise.2em{\\tripledash}}-}";
  137. < case "~--": return "{\\rlap{\\lower.2em{-}}\\rlap{\\raise.2em{\\tripledash}}-}";
  138. < case "-~-": return "{\\rlap{\\lower.2em{-}}\\rlap{\\raise.2em{-}}\\tripledash}";
  139. ---
  140. > case "~-": return "{\\mathrlap{\\raisebox{-.1em}{$-$}}\\raisebox{.1em}{$\\tripledash$}}";
  141. > case "~=": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  142. > case "~--": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$\\tripledash$}}-}";
  143. > case "-~-": return "{\\mathrlap{\\raisebox{-.2em}{$-$}}\\mathrlap{\\raisebox{.2em}{$-$}}\\tripledash}";
  144. 1680,1770d1695
  145. <
  146. < //
  147. < // MathJax definitions
  148. < //
  149. < MathJax.Extension["TeX/mhchem"].CE = CE;
  150. <
  151. < /***************************************************************************/
  152. <
  153. < TEX.Definitions.Add({
  154. < macros: {
  155. < //
  156. < // Set up the macros for chemistry
  157. < //
  158. < ce: "CE",
  159. < pu: "PU",
  160. <
  161. < //
  162. < // Make these load AMSmath package (redefined below when loaded)
  163. < //
  164. < xleftrightarrow: ["Extension", "AMSmath"],
  165. < xrightleftharpoons: ["Extension", "AMSmath"],
  166. < xRightleftharpoons: ["Extension", "AMSmath"],
  167. < xLeftrightharpoons: ["Extension", "AMSmath"],
  168. <
  169. < // FIXME: These don't work well in FF NativeMML mode
  170. < longrightleftharpoons: ["Macro", "\\stackrel{\\textstyle{-}\\!\\!{\\rightharpoonup}}{\\smash{{\\leftharpoondown}\\!\\!{-}}}"],
  171. < longRightleftharpoons: ["Macro", "\\stackrel{\\textstyle{-}\\!\\!{\\rightharpoonup}}{\\smash{\\leftharpoondown}}"],
  172. < longLeftrightharpoons: ["Macro", "\\stackrel{\\textstyle\\vphantom{{-}}{\\rightharpoonup}}{\\smash{{\\leftharpoondown}\\!\\!{-}}}"],
  173. < longleftrightarrows: ["Macro", "\\stackrel{\\longrightarrow}{\\smash{\\longleftarrow}\\Rule{0px}{.25em}{0px}}"],
  174. <
  175. < //
  176. < // Needed for \bond for the ~ forms
  177. < // Not perfectly aligned when zoomed in, but on 100%
  178. < //
  179. < tripledash: ["Macro", "\\vphantom{-}\\raise2mu{\\kern2mu\\tiny\\text{-}\\kern1mu\\text{-}\\kern1mu\\text{-}\\kern2mu}"]
  180. < },
  181. < }, null, true);
  182. <
  183. < if (!MathJax.Extension["TeX/AMSmath"]) {
  184. < TEX.Definitions.Add({
  185. < macros: {
  186. < xrightarrow: ["Extension", "AMSmath"],
  187. < xleftarrow: ["Extension", "AMSmath"]
  188. < }
  189. < }, null, true);
  190. < }
  191. <
  192. < //
  193. < // These arrows need to wait until AMSmath is loaded
  194. < //
  195. < MathJax.Hub.Register.StartupHook("TeX AMSmath Ready", function () {
  196. < TEX.Definitions.Add({
  197. < macros: {
  198. < //
  199. < // Some of these are hacks for now
  200. < //
  201. < xleftrightarrow: ["xArrow", 0x2194, 6, 6],
  202. < xrightleftharpoons: ["xArrow", 0x21CC, 5, 7], // FIXME: doesn't stretch in HTML-CSS output
  203. < xRightleftharpoons: ["xArrow", 0x21CC, 5, 7], // FIXME: how should this be handled?
  204. < xLeftrightharpoons: ["xArrow", 0x21CC, 5, 7]
  205. < }
  206. < }, null, true);
  207. < });
  208. <
  209. < TEX.Parse.Augment({
  210. <
  211. < //
  212. < // Implements \ce and friends
  213. < //
  214. < CE: function (name) {
  215. < var arg = this.GetArgument(name);
  216. < var tex = CE(arg).Parse();
  217. < this.string = tex + this.string.substr(this.i); this.i = 0;
  218. < },
  219. <
  220. < PU: function (name) {
  221. < var arg = this.GetArgument(name);
  222. < var tex = CE(arg).Parse('pu');
  223. < this.string = tex + this.string.substr(this.i); this.i = 0;
  224. < }
  225. <
  226. < });
  227. <
  228. < //
  229. < // Indicate that the extension is ready
  230. < //
  231. < MathJax.Hub.Startup.signal.Post("TeX mhchem Ready");
  232. <
  233. < });
  234. <
  235. < MathJax.Ajax.loadComplete("[mhchem]/unpacked/mhchem.js");