commit e1c5f5db1c699e650990b4be52d95b62029cf83b
parent 576380c11ce369feb663b00f90b8cbbb1d284ad4
Author: Eddie Kohler <ekohler@gmail.com>
Date: Mon, 28 Nov 2016 11:39:28 -0500
Add support for \text{rm,it,bf,tt,sf,normal}.
And allow \text to nest inside \text.
Diffstat:
6 files changed, 67 insertions(+), 6 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -1382,6 +1382,43 @@ describe("An HTML font tree-builder", function() {
expect(markup).toContain("<span class=\"mord mathfrak\">R</span>");
});
+ it("should render \\text{R} with the correct font", function() {
+ var markup = katex.renderToString("\\text{R}");
+ expect(markup).toContain("<span class=\"mord mathrm\">R</span>");
+ });
+
+ it("should render \\textit{R} with the correct font", function() {
+ var markup = katex.renderToString("\\textit{R}");
+ expect(markup).toContain("<span class=\"mord textit\">R</span>");
+ });
+
+ it("should render \\text{\\textit{R}} with the correct font", function() {
+ var markup = katex.renderToString("\\text{\\textit{R}}");
+ expect(markup).toContain("<span class=\"mord textit\">R</span>");
+ });
+
+ 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("<span class=\"mord mathrm\">R</span>");
+ expect(markup).toContain("<span class=\"mord textit\">S</span>");
+ expect(markup).toContain("<span class=\"mord mathrm\">T</span>");
+ });
+
+ it("should render \\textbf{R} with the correct font", function() {
+ var markup = katex.renderToString("\\textbf{R}");
+ expect(markup).toContain("<span class=\"mord mathbf\">R</span>");
+ });
+
+ it("should render \\textsf{R} with the correct font", function() {
+ var markup = katex.renderToString("\\textsf{R}");
+ expect(markup).toContain("<span class=\"mord mathsf\">R</span>");
+ });
+
+ it("should render \\texttt{R} with the correct font", function() {
+ var markup = katex.renderToString("\\texttt{R}");
+ expect(markup).toContain("<span class=\"mord mathtt\">R</span>");
+ });
+
it("should render a combination of font and color changes", function() {
var markup = katex.renderToString("\\color{blue}{\\mathbb R}");
var span = "<span class=\"mord mathbb\" style=\"color:blue;\">R</span>";