From e1c5f5db1c699e650990b4be52d95b62029cf83b Mon Sep 17 00:00:00 2001 From: Eddie Kohler Date: Mon, 28 Nov 2016 11:39:28 -0500 Subject: [PATCH] Add support for \text{rm,it,bf,tt,sf,normal}. And allow \text to nest inside \text. --- src/Options.js | 2 +- src/buildCommon.js | 4 ++++ src/buildHTML.js | 7 ++++--- src/functions.js | 15 +++++++++++++-- static/katex.less | 8 ++++++++ test/katex-spec.js | 37 +++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/Options.js b/src/Options.js index e6e0996..e5de594 100644 --- a/src/Options.js +++ b/src/Options.js @@ -99,7 +99,7 @@ Options.prototype.withPhantom = function() { */ Options.prototype.withFont = function(font) { return this.extend({ - font: font, + font: font || this.font, }); }; diff --git a/src/buildCommon.js b/src/buildCommon.js index a045ad3..bbd174d 100644 --- a/src/buildCommon.js +++ b/src/buildCommon.js @@ -430,6 +430,10 @@ var fontMap = { variant: "normal", fontName: "Main-Regular", }, + "textit": { + variant: "italic", + fontName: "Main-Italic", + }, // "mathit" is missing because it requires the use of two fonts: Main-Italic // and Math-Italic. This is handled by a special case in makeOrd which ends diff --git a/src/buildHTML.js b/src/buildHTML.js index 9e19188..dccf161 100644 --- a/src/buildHTML.js +++ b/src/buildHTML.js @@ -249,15 +249,16 @@ groupTypes.ordgroup = function(group, options) { }; groupTypes.text = function(group, options) { - var inner = buildExpression(group.value.body, options, true); + var newOptions = options.withFont(group.value.style); + var inner = buildExpression(group.value.body, newOptions, true); for (var i = 0; i < inner.length - 1; i++) { if (inner[i].tryCombine(inner[i + 1])) { inner.splice(i + 1, 1); i--; } } - return makeSpan(["mord", "text", options.style.cls()], - inner, options); + return makeSpan(["mord", "text", newOptions.style.cls()], + inner, newOptions); }; groupTypes.color = function(group, options) { diff --git a/src/functions.js b/src/functions.js index 24cf245..6966ea7 100644 --- a/src/functions.js +++ b/src/functions.js @@ -126,16 +126,27 @@ defineFunction("\\sqrt", { }; }); -// Some non-mathy text -defineFunction("\\text", { +// Non-mathy text, possibly in a font +var textFunctionStyles = { + "\\text": undefined, "\\textrm": "mathrm", "\\textsf": "mathsf", + "\\texttt": "mathtt", "\\textnormal": "mathrm", "\\textbf": "mathbf", + "\\textit": "textit", +}; + +defineFunction([ + "\\text", "\\textrm", "\\textsf", "\\texttt", "\\textnormal", + "\\textbf", "\\textit", +], { numArgs: 1, argTypes: ["text"], greediness: 2, + allowedInText: true, }, function(context, args) { var body = args[0]; return { type: "text", body: ordargument(body), + style: textFunctionStyles[context.funcName], }; }); diff --git a/static/katex.less b/static/katex.less index 7b8c293..764e446 100644 --- a/static/katex.less +++ b/static/katex.less @@ -49,6 +49,14 @@ display: inline-block; } + .mathrm { + font-style: normal; + } + + .textit { + font-style: italic; + } + .mathit { font-family: KaTeX_Math; font-style: italic; diff --git a/test/katex-spec.js b/test/katex-spec.js index de326d6..2018b91 100644 --- a/test/katex-spec.js +++ b/test/katex-spec.js @@ -1382,6 +1382,43 @@ describe("An HTML font tree-builder", function() { expect(markup).toContain("R"); }); + it("should render \\text{R} with the correct font", function() { + var markup = katex.renderToString("\\text{R}"); + expect(markup).toContain("R"); + }); + + it("should render \\textit{R} with the correct font", function() { + var markup = katex.renderToString("\\textit{R}"); + expect(markup).toContain("R"); + }); + + it("should render \\text{\\textit{R}} with the correct font", function() { + var markup = katex.renderToString("\\text{\\textit{R}}"); + expect(markup).toContain("R"); + }); + + it("should render \\text{R\\textit{S}T} with the correct fonts", function() { + var markup = katex.renderToString("\\text{R\\textit{S}T}"); + expect(markup).toContain("R"); + expect(markup).toContain("S"); + expect(markup).toContain("T"); + }); + + it("should render \\textbf{R} with the correct font", function() { + var markup = katex.renderToString("\\textbf{R}"); + expect(markup).toContain("R"); + }); + + it("should render \\textsf{R} with the correct font", function() { + var markup = katex.renderToString("\\textsf{R}"); + expect(markup).toContain("R"); + }); + + it("should render \\texttt{R} with the correct font", function() { + var markup = katex.renderToString("\\texttt{R}"); + expect(markup).toContain("R"); + }); + it("should render a combination of font and color changes", function() { var markup = katex.renderToString("\\color{blue}{\\mathbb R}"); var span = "R";