EmpheqConfiguration.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2021-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 empheq package.
  19. *
  20. * @author dpvc@mathjax.org (Davide P. Cervone)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import {CommandMap, EnvironmentMap} from '../SymbolMap.js';
  24. import ParseUtil from '../ParseUtil.js';
  25. import TexParser from '../TexParser.js';
  26. import TexError from '../TexError.js';
  27. import {BeginItem} from '../base/BaseItems.js';
  28. import {StackItem} from '../StackItem.js';
  29. import {EmpheqUtil} from './EmpheqUtil.js';
  30. /**
  31. * A StackItem for empheq environments.
  32. */
  33. export class EmpheqBeginItem extends BeginItem {
  34. /**
  35. * @override
  36. */
  37. public get kind() {
  38. return 'empheq-begin';
  39. }
  40. /**
  41. * @override
  42. */
  43. public checkItem(item: StackItem) {
  44. if (item.isKind('end') && item.getName() === this.getName()) {
  45. this.setProperty('end', false);
  46. }
  47. return super.checkItem(item);
  48. }
  49. }
  50. /**
  51. * The methods that implement the empheq package.
  52. */
  53. export const EmpheqMethods = {
  54. /**
  55. * Handle an empheq environment.
  56. *
  57. * @param {TexParser} parser The active tex parser.
  58. * @param {EmpheqBeginItem} begin The begin item for this environment.
  59. */
  60. Empheq(parser: TexParser, begin: EmpheqBeginItem) {
  61. if (parser.stack.env.closing === begin.getName()) {
  62. delete parser.stack.env.closing;
  63. parser.Push(parser.itemFactory.create('end').setProperty('name', parser.stack.global.empheq));
  64. parser.stack.global.empheq = '';
  65. const empheq = parser.stack.Top() as EmpheqBeginItem;
  66. EmpheqUtil.adjustTable(empheq, parser);
  67. parser.Push(parser.itemFactory.create('end').setProperty('name', 'empheq'));
  68. } else {
  69. ParseUtil.checkEqnEnv(parser);
  70. delete parser.stack.global.eqnenv;
  71. const opts = parser.GetBrackets('\\begin{' + begin.getName() + '}') || '';
  72. const [env, n] = (parser.GetArgument('\\begin{' + begin.getName() + '}') || '').split(/=/);
  73. if (!EmpheqUtil.checkEnv(env)) {
  74. throw new TexError('UnknownEnv', 'Unknown environment "%1"', env);
  75. }
  76. if (opts) {
  77. begin.setProperties(EmpheqUtil.splitOptions(opts, {left: 1, right: 1}));
  78. }
  79. parser.stack.global.empheq = env;
  80. parser.string = '\\begin{' + env + '}' + (n ? '{' + n + '}' : '') + parser.string.slice(parser.i);
  81. parser.i = 0;
  82. parser.Push(begin);
  83. }
  84. },
  85. /**
  86. * Create an <mo> with a given content
  87. *
  88. * @param {TexParser} parser The active tex parser.
  89. * @param {string} name The name of the macro being processed.
  90. * @param {string} c The character for the <mo>
  91. */
  92. EmpheqMO(parser: TexParser, _name: string, c: string) {
  93. parser.Push(parser.create('token', 'mo', {}, c));
  94. },
  95. /**
  96. * Create a delimiter <mo> with a given character
  97. *
  98. * @param {TexParser} parser The active tex parser.
  99. * @param {string} name The name of the macro being processed.
  100. */
  101. EmpheqDelim(parser: TexParser, name: string) {
  102. const c = parser.GetDelimiter(name);
  103. parser.Push(parser.create('token', 'mo', {stretchy: true, symmetric: true}, c));
  104. }
  105. };
  106. //
  107. // Define an environment map to add the new empheq environment
  108. //
  109. new EnvironmentMap('empheq-env', EmpheqUtil.environment, {
  110. empheq: ['Empheq', 'empheq'],
  111. }, EmpheqMethods);
  112. //
  113. // Define the empheq characters
  114. //
  115. new CommandMap('empheq-macros', {
  116. empheqlbrace: ['EmpheqMO', '{'],
  117. empheqrbrace: ['EmpheqMO', '}'],
  118. empheqlbrack: ['EmpheqMO', '['],
  119. empheqrbrack: ['EmpheqMO', ']'],
  120. empheqlangle: ['EmpheqMO', '\u27E8'],
  121. empheqrangle: ['EmpheqMO', '\u27E9'],
  122. empheqlparen: ['EmpheqMO', '('],
  123. empheqrparen: ['EmpheqMO', ')'],
  124. empheqlvert: ['EmpheqMO', '|'],
  125. empheqrvert: ['EmpheqMO', '|'],
  126. empheqlVert: ['EmpheqMO', '\u2016'],
  127. empheqrVert: ['EmpheqMO', '\u2016'],
  128. empheqlfloor: ['EmpheqMO', '\u230A'],
  129. empheqrfloor: ['EmpheqMO', '\u230B'],
  130. empheqlceil: ['EmpheqMO', '\u2308'],
  131. empheqrceil: ['EmpheqMO', '\u2309'],
  132. empheqbiglbrace: ['EmpheqMO', '{'],
  133. empheqbigrbrace: ['EmpheqMO', '}'],
  134. empheqbiglbrack: ['EmpheqMO', '['],
  135. empheqbigrbrack: ['EmpheqMO', ']'],
  136. empheqbiglangle: ['EmpheqMO', '\u27E8'],
  137. empheqbigrangle: ['EmpheqMO', '\u27E9'],
  138. empheqbiglparen: ['EmpheqMO', '('],
  139. empheqbigrparen: ['EmpheqMO', ')'],
  140. empheqbiglvert: ['EmpheqMO', '|'],
  141. empheqbigrvert: ['EmpheqMO', '|'],
  142. empheqbiglVert: ['EmpheqMO', '\u2016'],
  143. empheqbigrVert: ['EmpheqMO', '\u2016'],
  144. empheqbiglfloor: ['EmpheqMO', '\u230A'],
  145. empheqbigrfloor: ['EmpheqMO', '\u230B'],
  146. empheqbiglceil: ['EmpheqMO', '\u2308'],
  147. empheqbigrceil: ['EmpheqMO', '\u2309'],
  148. empheql: 'EmpheqDelim',
  149. empheqr: 'EmpheqDelim',
  150. empheqbigl: 'EmpheqDelim',
  151. empheqbigr: 'EmpheqDelim'
  152. }, EmpheqMethods);
  153. //
  154. // Define the package for our new environment
  155. //
  156. export const EmpheqConfiguration = Configuration.create('empheq', {
  157. handler: {
  158. macro: ['empheq-macros'],
  159. environment: ['empheq-env'],
  160. },
  161. items: {
  162. [EmpheqBeginItem.prototype.kind]: EmpheqBeginItem
  163. }
  164. });