MathtoolsConfiguration.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************
  2. * Copyright (c) 2020-2022 MathJax Consortium
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @fileoverview Configuration file for the mathtools package.
  18. *
  19. * @author v.sorge@mathjax.org (Volker Sorge)
  20. * @author dpvc@mathjax.org (Davide P. Cervone)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import {CommandMap} from '../SymbolMap.js';
  24. import NodeUtil from '../NodeUtil.js';
  25. import {expandable} from '../../../util/Options.js';
  26. import {ParserConfiguration} from '../Configuration.js';
  27. import {TeX} from '../../tex.js';
  28. import ParseOptions from '../ParseOptions.js';
  29. import './MathtoolsMappings.js';
  30. import {MathtoolsUtil} from './MathtoolsUtil.js';
  31. import {MathtoolsTagFormat} from './MathtoolsTags.js';
  32. import {MultlinedItem} from './MathtoolsItems.js';
  33. /**
  34. * The name of the paried-delimiters command map.
  35. */
  36. export const PAIREDDELIMS = 'mathtools-paired-delims';
  37. /**
  38. * Create the paired-delimiters command map, and link it into the configuration.
  39. * @param {ParserConfiguration} config The current configuration.
  40. */
  41. function initMathtools(config: ParserConfiguration) {
  42. new CommandMap(PAIREDDELIMS, {}, {});
  43. config.append(Configuration.local({handler: {macro: [PAIREDDELIMS]}, priority: -5}));
  44. }
  45. /**
  46. * Add any pre-defined paired delimiters, and subclass the configured tag format.
  47. * @param {ParserConfiguration} config The current configuration.
  48. * @param {TeX} jac The TeX input jax
  49. */
  50. function configMathtools(config: ParserConfiguration, jax: TeX<any, any, any>) {
  51. const parser = jax.parseOptions;
  52. const pairedDelims = parser.options.mathtools.pairedDelimiters;
  53. for (const cs of Object.keys(pairedDelims)) {
  54. MathtoolsUtil.addPairedDelims(parser, cs, pairedDelims[cs]);
  55. }
  56. MathtoolsTagFormat(config, jax);
  57. }
  58. /**
  59. * A filter to fix up mmultiscripts elements.
  60. * @param {ParseOptions} data The parse options.
  61. */
  62. export function fixPrescripts({data}: {data: ParseOptions}) {
  63. for (const node of data.getList('mmultiscripts')) {
  64. if (!node.getProperty('fixPrescript')) continue;
  65. const childNodes = NodeUtil.getChildren(node);
  66. let n = 0;
  67. for (const i of [1, 2]) {
  68. if (!childNodes[i]) {
  69. NodeUtil.setChild(node, i, data.nodeFactory.create('node', 'none'));
  70. n++;
  71. }
  72. }
  73. for (const i of [4, 5]) {
  74. if (NodeUtil.isType(childNodes[i], 'mrow') && NodeUtil.getChildren(childNodes[i]).length === 0) {
  75. NodeUtil.setChild(node, i, data.nodeFactory.create('node', 'none'));
  76. }
  77. }
  78. if (n === 2) {
  79. childNodes.splice(1, 2);
  80. }
  81. }
  82. }
  83. /**
  84. * The configuration for the mathtools package
  85. */
  86. export const MathtoolsConfiguration = Configuration.create(
  87. 'mathtools', {
  88. handler: {
  89. macro: ['mathtools-macros', 'mathtools-delimiters'],
  90. environment: ['mathtools-environments'],
  91. delimiter: ['mathtools-delimiters'],
  92. character: ['mathtools-characters']
  93. },
  94. items: {
  95. [MultlinedItem.prototype.kind]: MultlinedItem
  96. },
  97. init: initMathtools,
  98. config: configMathtools,
  99. postprocessors: [[fixPrescripts, -6]],
  100. options: {
  101. mathtools: {
  102. 'multlinegap': '1em', // horizontal space for multlined environments
  103. 'multlined-pos': 'c', // default alignment for multlined environments
  104. 'firstline-afterskip': '', // space for first line of multlined (overrides multlinegap)
  105. 'lastline-preskip': '', // space for last line of multlined (overrides multlinegap)
  106. 'smallmatrix-align': 'c', // default alignment for smallmatrix environments
  107. 'shortvdotsadjustabove': '.2em', // space to remove above \shortvdots
  108. 'shortvdotsadjustbelow': '.2em', // space to remove below \shortvdots
  109. 'centercolon': false, // true to have colon automatically centered
  110. 'centercolon-offset': '.04em', // vertical adjustment for centered colons
  111. 'thincolon-dx': '-.04em', // horizontal adjustment for thin colons (e.g., \coloneqq)
  112. 'thincolon-dw': '-.08em', // width adjustment for thin colons
  113. 'use-unicode': false, // true to use unicode characters rather than multi-character
  114. // version for \coloneqq, etc., when possible
  115. 'prescript-sub-format': '', // format for \prescript subscript
  116. 'prescript-sup-format': '', // format for \prescript superscript
  117. 'prescript-arg-format': '', // format for \prescript base
  118. 'allow-mathtoolsset': true, // true to allow \mathtoolsset to change settings
  119. pairedDelimiters: expandable({}), // predefined paired delimiters
  120. // name: [left, right, body, argcount, pre, post]
  121. tagforms: expandable({}), // tag form definitions
  122. // name: [left, right, format]
  123. }
  124. }
  125. }
  126. );