Add support for \text{rm,it,bf,tt,sf,normal}.
And allow \text to nest inside \text.
This commit is contained in:
parent
576380c11c
commit
e1c5f5db1c
|
@ -99,7 +99,7 @@ Options.prototype.withPhantom = function() {
|
||||||
*/
|
*/
|
||||||
Options.prototype.withFont = function(font) {
|
Options.prototype.withFont = function(font) {
|
||||||
return this.extend({
|
return this.extend({
|
||||||
font: font,
|
font: font || this.font,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -430,6 +430,10 @@ var fontMap = {
|
||||||
variant: "normal",
|
variant: "normal",
|
||||||
fontName: "Main-Regular",
|
fontName: "Main-Regular",
|
||||||
},
|
},
|
||||||
|
"textit": {
|
||||||
|
variant: "italic",
|
||||||
|
fontName: "Main-Italic",
|
||||||
|
},
|
||||||
|
|
||||||
// "mathit" is missing because it requires the use of two fonts: 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
|
// and Math-Italic. This is handled by a special case in makeOrd which ends
|
||||||
|
|
|
@ -249,15 +249,16 @@ groupTypes.ordgroup = function(group, options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
groupTypes.text = 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++) {
|
for (var i = 0; i < inner.length - 1; i++) {
|
||||||
if (inner[i].tryCombine(inner[i + 1])) {
|
if (inner[i].tryCombine(inner[i + 1])) {
|
||||||
inner.splice(i + 1, 1);
|
inner.splice(i + 1, 1);
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return makeSpan(["mord", "text", options.style.cls()],
|
return makeSpan(["mord", "text", newOptions.style.cls()],
|
||||||
inner, options);
|
inner, newOptions);
|
||||||
};
|
};
|
||||||
|
|
||||||
groupTypes.color = function(group, options) {
|
groupTypes.color = function(group, options) {
|
||||||
|
|
|
@ -126,16 +126,27 @@ defineFunction("\\sqrt", {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Some non-mathy text
|
// Non-mathy text, possibly in a font
|
||||||
defineFunction("\\text", {
|
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,
|
numArgs: 1,
|
||||||
argTypes: ["text"],
|
argTypes: ["text"],
|
||||||
greediness: 2,
|
greediness: 2,
|
||||||
|
allowedInText: true,
|
||||||
}, function(context, args) {
|
}, function(context, args) {
|
||||||
var body = args[0];
|
var body = args[0];
|
||||||
return {
|
return {
|
||||||
type: "text",
|
type: "text",
|
||||||
body: ordargument(body),
|
body: ordargument(body),
|
||||||
|
style: textFunctionStyles[context.funcName],
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,14 @@
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mathrm {
|
||||||
|
font-style: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.textit {
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
|
|
||||||
.mathit {
|
.mathit {
|
||||||
font-family: KaTeX_Math;
|
font-family: KaTeX_Math;
|
||||||
font-style: italic;
|
font-style: italic;
|
||||||
|
|
|
@ -1382,6 +1382,43 @@ describe("An HTML font tree-builder", function() {
|
||||||
expect(markup).toContain("<span class=\"mord mathfrak\">R</span>");
|
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() {
|
it("should render a combination of font and color changes", function() {
|
||||||
var markup = katex.renderToString("\\color{blue}{\\mathbb R}");
|
var markup = katex.renderToString("\\color{blue}{\\mathbb R}");
|
||||||
var span = "<span class=\"mord mathbb\" style=\"color:blue;\">R</span>";
|
var span = "<span class=\"mord mathbb\" style=\"color:blue;\">R</span>";
|
||||||
|
|
Loading…
Reference in New Issue
Block a user