@qtikit/mathml-to-latex
Version:
A JavaScript tool to convert mathml string to LaTeX string
57 lines (56 loc) • 2.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MMultiscripts = void 0;
var helpers_1 = require("../../../helpers");
var errors_1 = require("../../../errors");
var MMultiscripts = /** @class */ (function () {
function MMultiscripts(mathElement) {
this._mathmlElement = mathElement;
}
MMultiscripts.prototype.convert = function () {
var _a = this._mathmlElement, name = _a.name, children = _a.children;
var childrenLength = children.length;
if (childrenLength < 3)
throw new errors_1.InvalidNumberOfChildrenError(name, 3, childrenLength, 'at least');
var baseContent = (0, helpers_1.mathMLElementToLaTeXConverter)(children[0]).convert();
return this._prescriptLatex() + this._wrapInParenthesisIfThereIsSpace(baseContent) + this._postscriptLatex();
};
MMultiscripts.prototype._prescriptLatex = function () {
var children = this._mathmlElement.children;
var sub;
var sup;
if (this._isPrescripts(children[1])) {
sub = children[2];
sup = children[3];
}
else if (this._isPrescripts(children[3])) {
sub = children[4];
sup = children[5];
}
else
return '';
var subLatex = (0, helpers_1.mathMLElementToLaTeXConverter)(sub).convert();
var supLatex = (0, helpers_1.mathMLElementToLaTeXConverter)(sup).convert();
return "\\_{".concat(subLatex, "}^{").concat(supLatex, "}");
};
MMultiscripts.prototype._postscriptLatex = function () {
var children = this._mathmlElement.children;
if (this._isPrescripts(children[1]))
return '';
var sub = children[1];
var sup = children[2];
var subLatex = (0, helpers_1.mathMLElementToLaTeXConverter)(sub).convert();
var supLatex = (0, helpers_1.mathMLElementToLaTeXConverter)(sup).convert();
return "_{".concat(subLatex, "}^{").concat(supLatex, "}");
};
MMultiscripts.prototype._wrapInParenthesisIfThereIsSpace = function (str) {
if (!str.match(/\s+/g))
return str;
return new helpers_1.ParenthesisWrapper().wrap(str);
};
MMultiscripts.prototype._isPrescripts = function (child) {
return (child === null || child === void 0 ? void 0 : child.name) === 'mprescripts';
};
return MMultiscripts;
}());
exports.MMultiscripts = MMultiscripts;