BraketMethods.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Methods for TeX parsing of the braket package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {ParseMethod} from '../Types.js';
  23. import BaseMethods from '../base/BaseMethods.js';
  24. import TexParser from '../TexParser.js';
  25. import {TEXCLASS} from '../../../core/MmlTree/MmlNode.js';
  26. import TexError from '../TexError.js';
  27. let BraketMethods: Record<string, ParseMethod> = {};
  28. BraketMethods.Macro = BaseMethods.Macro;
  29. /**
  30. * Generate a bra-ket expression.
  31. * @param {TexParser} parser The current TeX parser.
  32. * @param {string} name Name of the current control sequence.
  33. * @param {string} open Opening delimiter.
  34. * @param {string} close Closing delimiter.
  35. * @param {boolean} stretchy Is it stretchy.
  36. * @param {number} barmax Maximum number of bars allowed.
  37. */
  38. BraketMethods.Braket = function(parser: TexParser, _name: string,
  39. open: string, close: string,
  40. stretchy: boolean, barmax: number) {
  41. let next = parser.GetNext();
  42. if (next === '') {
  43. throw new TexError('MissingArgFor', 'Missing argument for %1', parser.currentCS);
  44. }
  45. let single = true;
  46. if (next === '{') {
  47. parser.i++;
  48. single = false;
  49. }
  50. parser.Push(
  51. parser.itemFactory.create('braket')
  52. .setProperties({barmax: barmax, barcount: 0, open: open,
  53. close: close, stretchy: stretchy, single: single}));
  54. };
  55. /**
  56. * Generate a bar. If inside a bra-ket expressions it's handled accordingly.
  57. * @param {TexParser} parser The current TeX parser.
  58. * @param {string} name Name of the current control sequence.
  59. */
  60. BraketMethods.Bar = function(parser: TexParser, name: string) {
  61. let c = name === '|' ? '|' : '\u2225';
  62. let top = parser.stack.Top();
  63. if (top.kind !== 'braket' ||
  64. top.getProperty('barcount') >= top.getProperty('barmax')) {
  65. let mml = parser.create('token', 'mo', {texClass: TEXCLASS.ORD, stretchy: false}, c);
  66. parser.Push(mml);
  67. return;
  68. }
  69. if (c === '|' && parser.GetNext() === '|') {
  70. parser.i++;
  71. c = '\u2225';
  72. }
  73. let stretchy = top.getProperty('stretchy');
  74. if (!stretchy) {
  75. let node = parser.create('token', 'mo', {stretchy: false, braketbar: true}, c);
  76. parser.Push(node);
  77. return;
  78. }
  79. let node = parser.create('node', 'TeXAtom', [], {texClass: TEXCLASS.CLOSE});
  80. parser.Push(node);
  81. top.setProperty('barcount', top.getProperty('barcount') as number + 1);
  82. node = parser.create('token', 'mo', {stretchy: true, braketbar: true}, c);
  83. parser.Push(node);
  84. node = parser.create('node', 'TeXAtom', [], {texClass: TEXCLASS.OPEN});
  85. parser.Push(node);
  86. };
  87. export default BraketMethods;