MhchemConfiguration.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Configuration file for the mhchem package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import {CommandMap} from '../SymbolMap.js';
  24. import {ParseMethod} from '../Types.js';
  25. import TexError from '../TexError.js';
  26. import TexParser from '../TexParser.js';
  27. import BaseMethods from '../base/BaseMethods.js';
  28. import {AmsMethods} from '../ams/AmsMethods.js';
  29. import {mhchemParser} from 'mhchemparser/dist/mhchemParser.js';
  30. // Namespace
  31. let MhchemMethods: Record<string, ParseMethod> = {};
  32. MhchemMethods.Macro = BaseMethods.Macro;
  33. MhchemMethods.xArrow = AmsMethods.xArrow;
  34. /**
  35. * @param{TeXParser} parser The parser for this expression
  36. * @param{string} name The macro name being called
  37. * @param{string} machine The name of the fininte-state machine to use
  38. */
  39. MhchemMethods.Machine = function(parser: TexParser, name: string, machine: 'tex' | 'ce' | 'pu') {
  40. let arg = parser.GetArgument(name);
  41. let tex;
  42. try {
  43. tex = mhchemParser.toTex(arg, machine);
  44. } catch (err) {
  45. throw new TexError(err[0], err[1]);
  46. }
  47. parser.string = tex + parser.string.substr(parser.i);
  48. parser.i = 0;
  49. };
  50. new CommandMap(
  51. 'mhchem', {
  52. ce: ['Machine', 'ce'],
  53. pu: ['Machine', 'pu'],
  54. longrightleftharpoons: [
  55. 'Macro',
  56. '\\stackrel{\\textstyle{-}\\!\\!{\\rightharpoonup}}{\\smash{{\\leftharpoondown}\\!\\!{-}}}'
  57. ],
  58. longRightleftharpoons: [
  59. 'Macro',
  60. '\\stackrel{\\textstyle{-}\\!\\!{\\rightharpoonup}}{\\smash{\\leftharpoondown}}'
  61. ],
  62. longLeftrightharpoons: [
  63. 'Macro',
  64. '\\stackrel{\\textstyle\\vphantom{{-}}{\\rightharpoonup}}{\\smash{{\\leftharpoondown}\\!\\!{-}}}'
  65. ],
  66. longleftrightarrows: [
  67. 'Macro',
  68. '\\stackrel{\\longrightarrow}{\\smash{\\longleftarrow}\\Rule{0px}{.25em}{0px}}'
  69. ],
  70. //
  71. // Needed for \bond for the ~ forms
  72. //
  73. tripledash: [
  74. 'Macro',
  75. '\\vphantom{-}\\raise2mu{\\kern2mu\\tiny\\text{-}\\kern1mu\\text{-}\\kern1mu\\text{-}\\kern2mu}'
  76. ],
  77. xleftrightarrow: ['xArrow', 0x2194, 6, 6],
  78. xrightleftharpoons: ['xArrow', 0x21CC, 5, 7], // FIXME: doesn't stretch in HTML-CSS output
  79. xRightleftharpoons: ['xArrow', 0x21CC, 5, 7], // FIXME: how should this be handled?
  80. xLeftrightharpoons: ['xArrow', 0x21CC, 5, 7]
  81. },
  82. MhchemMethods
  83. );
  84. export const MhchemConfiguration = Configuration.create(
  85. 'mhchem', {handler: {macro: ['mhchem']}}
  86. );