diff --git a/src/buildCommon.js b/src/buildCommon.js index ca9bf03e..b0cdc192 100644 --- a/src/buildCommon.js +++ b/src/buildCommon.js @@ -682,6 +682,16 @@ const spacingFunctions: {[string]: {| size: string, className: string |}} = { }, }; +// A lookup table to determine whether a spacing function/symbol should be +// treated like a regular space character. +const regularSpace: {[string]: boolean} = { + " ": true, + "\\ ": true, + "~": true, + "\\space": true, + "\\nobreakspace": true, +}; + /** * Maps TeX font commands to objects containing: * - variant: string used for "mathvariant" attribute in buildMathML.js @@ -777,4 +787,5 @@ export default { svgData, tryCombineChars, spacingFunctions, + regularSpace, }; diff --git a/src/buildHTML.js b/src/buildHTML.js index 7e697e63..71c57deb 100644 --- a/src/buildHTML.js +++ b/src/buildHTML.js @@ -419,8 +419,7 @@ groupTypes.supsub = function(group, options) { }; groupTypes.spacing = function(group, options) { - if (group.value === "\\ " || group.value === "\\space" || - group.value === " " || group.value === "~") { + if (buildCommon.regularSpace.hasOwnProperty(group.value)) { // Spaces are generated by adding an actual space. Each of these // things has an entry in the symbols table, so these will be turned // into appropriate outputs. diff --git a/src/buildMathML.js b/src/buildMathML.js index ef68aa4b..420a8008 100644 --- a/src/buildMathML.js +++ b/src/buildMathML.js @@ -228,8 +228,7 @@ groupTypes.supsub = function(group, options) { groupTypes.spacing = function(group) { let node; - if (group.value === "\\ " || group.value === "\\space" || - group.value === " " || group.value === "~") { + if (buildCommon.regularSpace.hasOwnProperty(group.value)) { node = new mathMLTree.MathNode( "mtext", [new mathMLTree.TextNode("\u00a0")]); } else { diff --git a/test/katex-spec.js b/test/katex-spec.js index 47601277..b490c13b 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -875,6 +875,11 @@ describe("A text parser", function() { expect(textWithEmbeddedMath).toParse(); }); + it("should parse spacing functions", function() { + expect("a b\\, \\; \\! \\: ~ \\thinspace \\medspace \\quad \\ ").toBuild(); + expect("\\enspace \\thickspace \\qquad \\space \\nobreakspace").toBuild(); + }); + it("should omit spaces after commands", function() { expect("\\text{\\textellipsis !}") .toParseLike("\\text{\\textellipsis!}"); @@ -2999,11 +3004,11 @@ describe("The \\mathchoice function", function() { describe("Symbols", function() { it("should parse \\text{\\i\\j}", () => { - expect("\\text{\\i\\j}").toParse(); + expect("\\text{\\i\\j}").toBuild(); }); it("should parse spacing functions in math or text mode", () => { - expect("A\\;B\\,C\\nobreakspace \\text{A\\;B\\,C\\nobreakspace}").toParse(); + expect("A\\;B\\,C\\nobreakspace \\text{A\\;B\\,C\\nobreakspace}").toBuild(); }); it("should render ligature commands like their unicode characters", () => {