/* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */ /* vim: set ts=2 et sw=2 tw=80: */ /************************************************************* * * MathJax/extensions/MathML/content-mathml.js * * This file provides methods to convert Content-MathML to * Presentation MathML for processing by MathJax. The transform is * performed in a DOM filter for the MathML input jax, so that the * Show Math As menu will still show the Original MathML as Content MathML, * but the Presentation MathML can be obtained from the main MathML menu. * * To load it, include * * MathML: { * extensions: ["content-mathml.js"] * } * * in your configuration. * * A portion of this file is taken from ctop.js which is * Copyright (c) David Carlisle 2001, 2002, 2008, 2009, 2013, * and is used by permission of David Carlisle, who has agreed to allow us * to release it under the Apache2 license (see below). That portion is * indicated via comments. * * The remainder falls under the copyright that follows. * --------------------------------------------------------------------- * * Copyright (c) 2013-2020 The MathJax Consortium * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ MathJax.Extension["MathML/content-mathml"] = (function(HUB) { /* * Content MathML to Presentation MathML conversion * * based on David Carlisle's ctop.js - https://web-xslt.googlecode.com/svn/trunk/ctop/ctop.js * */ var isMSIE = HUB.Browser.isMSIE; if (isMSIE) { try {document.namespaces.add("m","http://www.w3.org/1998/Math/MathML")} catch (err) {} } var CONFIG = HUB.CombineConfig("MathML.content-mathml",{ // render `a+(-b)` as `a-b`? collapsePlusMinus: true, /* mathvariant to use with corresponding type attribute */ cistyles: { vector: 'bold-italic', matrix: 'bold-upright' }, /* Symbol names to translate to characters */ symbols: { gamma: '\u03B3' } }); var CToP = { version: "2.7.9", settings: CONFIG, /* Transform the given elements from Content MathML to Presentation MathML and replace the original elements */ transformElements: function(elements) { for (var i = 0, l = elements.length; i= 0) element = CToP.cloneNode(element,true); // removes namespaces var mathNode = CToP.cloneNode(element); for (var j = 0, l = element.childNodes.length; jn instead of -n // element.appendChild(document.createTextNode(textContent.substr(1))); var mrow = CToP.createElement('mrow'); CToP.appendToken(mrow,'mo','\u2212'); mrow.appendChild(element); element = mrow; } else { element.appendChild(document.createTextNode(textContent)); } parentNode.appendChild(element); return element; }, /* Transform a Content MathML node to Presentation MathML node(s), and attach it to the parent */ applyTransform: function(parentNode,contentMMLNode,precedence) { if (!contentMMLNode) { var merror = CToP.createElement('merror'); CToP.appendToken(merror,'mtext','Missing child node'); parentNode.appendChild(merror); return; } var nodeName = contentMMLNode.nodeName.replace(/.*:/,''); if (contentMMLNode.nodeType === 1) { if (CToP.tokens[nodeName]) { CToP.tokens[nodeName](parentNode,contentMMLNode,precedence); } else if (contentMMLNode.childNodes.length === 0) { var mml = CToP.MML[nodeName]; if (mml && mml.isa && mml.isa(CToP.mbase)) { parentNode.appendChild(CToP.cloneNode(contentMMLNode)); } else { CToP.appendToken(parentNode,'mi',nodeName); } } else { var clonedChild = CToP.cloneNode(contentMMLNode); parentNode.appendChild(clonedChild); for (var j = 0, l = contentMMLNode.childNodes.length; j1) { CToP.applyTransform(mrow,args[0],tokenPrecedence); } CToP.appendToken(mrow,'mo',name); if (args.length>0) { var z = args[(args.length === 1)?0:1]; CToP.applyTransform(mrow,z,tokenPrecedence); } if (needsBrackets) { CToP.appendToken(mrow,'mo',')'); } parentNode.appendChild(mrow); } }, /* Transform an infix operator * * (function factory) */ infix: function(name,tokenPrecedence) { return function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); var needsBrackets = precedence>tokenPrecedence; if (needsBrackets) { CToP.appendToken(mrow,'mo','('); } for (var j = 0, l = args.length; j0) { CToP.appendToken(mrow,'mo',name); } CToP.applyTransform(mrow,args[j],tokenPrecedence); } if (needsBrackets) { CToP.appendToken(mrow,'mo',')'); } parentNode.appendChild(mrow); } }, /* Transform an iterated operation, e.g. summation * * (function factory */ iteration: function(name,limitSymbol) { return function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); var mo = CToP.createElement('mo'); CToP.setTextContent(mo,name); var munderover = CToP.createElement('munderover'); munderover.appendChild(mo); var mrow1 = CToP.createElement('mrow'); var i, j, num_qualifiers, num_bvars, children, bvar, num_children, num_args; for (i = 0, num_qualifiers = qualifiers.length; i',1), lt: CToP.transforms.infix('<',1), geq: CToP.transforms.infix('\u2265',1), leq: CToP.transforms.infix('\u2264',1), equivalent: CToP.transforms.infix('\u2261',1), approx: CToP.transforms.infix('\u2248',1), subset: CToP.transforms.infix('\u2286',2), prsubset: CToP.transforms.infix('\u2282',2), cartesianproduct: CToP.transforms.infix('\u00D7',2), "cartesian_product": CToP.transforms.infix('\u00D7',2), vectorproduct: CToP.transforms.infix('\u00D7',2), scalarproduct: CToP.transforms.infix('.',2), outerproduct: CToP.transforms.infix('\u2297',2), sum: CToP.transforms.iteration('\u2211','='), product: CToP.transforms.iteration('\u220F','='), forall: CToP.transforms.bind('\u2200','.',','), exists: CToP.transforms.bind('\u2203','.',','), lambda: CToP.transforms.bind('\u03BB','.',','), limit: CToP.transforms.iteration('lim','\u2192'), sdev: CToP.transforms.fn('\u03c3'), determinant: CToP.transforms.fn('det'), max: CToP.transforms.minmax('max'), min: CToP.transforms.minmax('min'), real: CToP.transforms.fn('\u211b'), imaginary: CToP.transforms.fn('\u2111'), set: CToP.transforms.set('{','}'), list: CToP.transforms.set('(',')'), exp: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var msup = CToP.createElement('msup'); CToP.appendToken(msup,'mi','e'); CToP.applyTransform(msup,args[0],0); parentNode.appendChild(msup); }, union: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (bvars.length) { CToP.transforms.iteration('\u22C3','=')(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } else { CToP.transforms.infix('\u222A',2)(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } }, intersect: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (bvars.length) { CToP.transforms.iteration('\u22C2','=')(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } else { var mrow = CToP.createElement('mrow'); var needsBrackets = precedence>2; if (needsBrackets) { CToP.appendToken(mrow,'mo','('); } for (var j = 0, l = args.length; j0) { CToP.appendToken(mrow,'mo','\u2229'); if (args[j].nodeName === 'apply') { var child = CToP.getChildren(args[j])[0]; argBrackets = child.nodeName === 'union'; } } if (argBrackets) { CToP.appendToken(mrow,'mo','('); } CToP.applyTransform(mrow,args[j],2); if (argBrackets) { CToP.appendToken(mrow,'mo',')'); } } if (needsBrackets) { CToP.appendToken(mrow,'mo',')'); } parentNode.appendChild(mrow); } }, floor: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); CToP.appendToken(mrow,'mo','\u230a'); CToP.applyTransform(mrow,args[0],0); CToP.appendToken(mrow,'mo','\u230b'); parentNode.appendChild(mrow); }, conjugate: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mover = CToP.createElement('mover'); CToP.applyTransform(mover,args[0],0); CToP.appendToken(mover,'mo','\u00af'); parentNode.appendChild(mover); }, abs: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); CToP.appendToken(mrow,'mo','|'); CToP.applyTransform(mrow,args[0],0); CToP.appendToken(mrow,'mo','|'); parentNode.appendChild(mrow); }, and: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (bvars.length || qualifiers.length) { CToP.transforms.iteration('\u22c0','=')(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,4); } else { CToP.transforms.infix('\u2227',2)(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } }, or: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (bvars.length || qualifiers.length) { CToP.transforms.iteration('\u22c1','=')(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,4); } else { CToP.transforms.infix('\u2228',2)(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } }, xor: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (bvars.length || qualifiers.length) { CToP.transforms.iteration('xor','=')(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,4); } else { CToP.transforms.infix('xor',2)(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence); } }, card: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); CToP.appendToken(mrow,'mo','|'); CToP.applyTransform(mrow,args[0],0); CToP.appendToken(mrow,'mo','|'); parentNode.appendChild(mrow); }, mean: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { if (args.length === 1) { var mover = CToP.createElement('mover'); CToP.applyTransform(mover,args[0],0); CToP.appendToken(mover,'mo','\u00af'); parentNode.appendChild(mover); } else { parentNode.appendChild(CToP.createmfenced(args,'\u27e8','\u27e9')); } }, moment: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var degree, momentabout, children, i, j, l; for (i = 0, l = qualifiers.length; i1) { argrow.appendChild(CToP.createmfenced(args,'(',')')); } else { CToP.applyTransform(argrow,args[0],0); } if (degree) { var msup = CToP.createElement('msup'); msup.appendChild(argrow); children = CToP.getChildren(degree); for (j = 0, l = children.length; j3; if (needsBrackets) { CToP.appendToken(mrow,'mo','('); } for (var j = 0, l = args.length; j0) { CToP.appendToken(mrow,'mo',(args[j].nodeName === 'cn') ? "\u00D7" :"\u2062"); } CToP.applyTransform(mrow,args[j],3); } if (needsBrackets) { CToP.appendToken(mrow,'mo',')'); } parentNode.appendChild(mrow); }, plus: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); var needsBrackets = precedence>2; if (needsBrackets) { CToP.appendToken(mrow,'mo','('); } for (var j = 0, l = args.length; j0) { var n; if (CToP.settings.collapsePlusMinus) { if (arg.nodeName === 'cn' && !(children.length) && (n = Number(CToP.getTextContent(arg))) <0) { CToP.appendToken(mrow,'mo','\u2212'); CToP.appendToken(mrow,'mn', -n); } else if (arg.nodeName === 'apply' && children.length === 2 && children[0].nodeName === 'minus') { CToP.appendToken(mrow,'mo','\u2212'); CToP.applyTransform(mrow,children[1],2); } else if (arg.nodeName === 'apply' && children.length>2 && children[0].nodeName === 'times' && children[1].nodeName === 'cn' && (n = Number(CToP.getTextContent(children[1]))) < 0) { CToP.appendToken(mrow,'mo','\u2212'); children[1].textContent = -n; // OK to change MathML since it is being discarded afterward CToP.applyTransform(mrow,arg,2); } else{ CToP.appendToken(mrow,'mo','+'); CToP.applyTransform(mrow,arg,2); } } else { CToP.appendToken(mrow,'mo','+'); CToP.applyTransform(mrow,arg,2); } } else { CToP.applyTransform(mrow,arg,2); } } if (needsBrackets) { CToP.appendToken(mrow,'mo',')'); } parentNode.appendChild(mrow); }, transpose: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var msup = CToP.createElement('msup'); CToP.applyTransform(msup,args[0],precedence); CToP.appendToken(msup,'mi','T'); parentNode.appendChild(msup); }, power: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var msup = CToP.createElement('msup'); CToP.applyTransform(msup,args[0],3); CToP.applyTransform(msup,args[1],precedence); parentNode.appendChild(msup); }, selector: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var msub = CToP.createElement('msub'); var mrow = args ? args[0]: CToP.createElement('mrow'); CToP.applyTransform(msub,mrow,0); var mrow2 = CToP.createElement('mrow'); for (var i = 1, l = args.length; i1) { CToP.applyTransform(mrow,args[1],0); } } CToP.appendToken(mrow,'mo','\u230B'); parentNode.appendChild(mrow); }, factorial: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mrow = CToP.createElement('mrow'); CToP.applyTransform(mrow,args[0],4); CToP.appendToken(mrow,'mo','!'); parentNode.appendChild(mrow); }, root: function(parentNode,contentMMLNode,firstArg,args,bvars,qualifiers,precedence) { var mr; if (firstArg.nodeName === 'root' && (qualifiers.length === 0 || (qualifiers[0].nodeName === 'degree' && CToP.getTextContent(qualifiers[0]) === '2'))) { mr = CToP.createElement('msqrt'); for (var i = 0, l = args.length; i1) { var msup = CToP.createElement('msup'); CToP.applyTransform(msup,bvar,0); CToP.appendToken(msup,'mn',degree); bottomrow.appendChild(msup); } else { CToP.applyTransform(bottomrow,bvar,0); } } for (i = 0, l = lambdaSequence.length; i0) { if (hadFirst) { CToP.appendToken(degreeRow,'mo','+'); } CToP.appendToken(degreeRow,'mn',degree); } } if (args.length) { differendNode = args[0]; } for (i = 0, l = bvars.length; i