Old font command support: \rm, \sf, \tt, \bf, \it (#675)

Squashed:
* \rm, \sf, \tt, \bf, \it support
* Fix space consumption after macros without arguments
* Add tests for old font commands
This commit is contained in:
Erik Demaine 2017-04-07 17:06:23 -04:00 committed by Martin von Gagern
parent 40ec1b92b8
commit 171e38f28a
5 changed files with 49 additions and 5 deletions

View File

@ -394,6 +394,17 @@ const styleFuncs = [
"\\displaystyle", "\\textstyle", "\\scriptstyle", "\\scriptscriptstyle",
];
// Old font functions
const oldFontFuncs = {
"\\rm": "mathrm",
"\\sf": "mathsf",
"\\tt": "mathtt",
"\\bf": "mathbf",
"\\it": "mathit",
//"\\sl": "textsl",
//"\\sc": "textsc",
};
/**
* Parses an implicit group, which is a group that starts at the end of a
* specified, and ends right before a higher explicit group ends, or at EOL. It
@ -462,7 +473,8 @@ Parser.prototype.parseImplicitGroup = function() {
result.position = end.position;
return result;
} else if (utils.contains(sizeFuncs, func)) {
// If we see a sizing function, parse out the implict body
// If we see a sizing function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(false);
return new ParseNode("sizing", {
// Figure out what size to use based on the list of functions above
@ -470,7 +482,8 @@ Parser.prototype.parseImplicitGroup = function() {
value: body,
}, this.mode);
} else if (utils.contains(styleFuncs, func)) {
// If we see a styling function, parse out the implict body
// If we see a styling function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(true);
return new ParseNode("styling", {
// Figure out what style to use by pulling out the style from
@ -478,6 +491,22 @@ Parser.prototype.parseImplicitGroup = function() {
style: func.slice(1, func.length - 5),
value: body,
}, this.mode);
} else if (func in oldFontFuncs) {
const style = oldFontFuncs[func];
// If we see an old font function, parse out the implicit body
this.consumeSpaces();
const body = this.parseExpression(true);
if (style.slice(0, 4) === 'text') {
return new ParseNode("text", {
style: style,
body: new ParseNode("ordgroup", body, this.mode),
}, this.mode);
} else {
return new ParseNode("font", {
font: style,
body: new ParseNode("ordgroup", body, this.mode),
}, this.mode);
}
} else {
// Defer to parseFunction if it's not a function we handle
return this.parseFunction(start);
@ -629,9 +658,7 @@ Parser.prototype.parseGroupOfType = function(innerMode, optional) {
if (innerMode === "text") {
// text mode is special because it should ignore the whitespace before
// it
while (this.nextToken.text === " ") {
this.consume();
}
this.consumeSpaces();
}
// By the time we get here, innerMode is one of "text" or "math".
// We switch the mode of the parser, recurse, then restore the old mode.
@ -640,6 +667,12 @@ Parser.prototype.parseGroupOfType = function(innerMode, optional) {
return res;
};
Parser.prototype.consumeSpaces = function() {
while (this.nextToken.text === " ") {
this.consume();
}
};
/**
* Parses a group, essentially returning the string formed by the
* brace-enclosed tokens plus some position information.

View File

@ -591,6 +591,11 @@ defineFunction([
"\\scriptscriptstyle",
], 0, null);
// Old font changing functions
defineFunction([
"\\rm", "\\sf", "\\tt", "\\bf", "\\it", //"\\sl", "\\sc",
], 0, null);
defineFunction([
// styles
"\\mathrm", "\\mathit", "\\mathbf",

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -118,6 +118,12 @@ NestedFractions: |
\dfrac{\frac{a}{b}}{\frac{c}{d}}\dfrac{\dfrac{a}{b}}
{\dfrac{c}{d}}\frac{\frac{a}{b}}{\frac{c}{d}}
NullDelimiterInteraction: a \bigl. + 2 \quad \left. + a \right)
OldFont: |
\begin{matrix}
\rm rm & it & \it it & \bf bf & \sf sf & \tt tt \\
\text{\rm rm} & \text{rm} & \text{\it it} & \text{\bf bf} & \text{\sf sf} & \text{\tt tt} \\
i\rm r\it i & \text{r\it i\rm r}
\end{matrix}
OpLimits: |
{\sin_2^2 \lim_2^2 \int_2^2 \sum_2^2}
{\displaystyle \lim_2^2 \int_2^2 \intop_2^2 \sum_2^2}