Support for top-level \newline and \\ in inline math (#1298)

* Support for top-level \newline and \\ in inline math

This was a little tricky because `\\` was defined as an endOfExpression.
Instead made `\\` a termination specific to an array environment.
Outside an array environment, buildHTML handles the `cr` object,
resulting in a `.newline` class.  Currently this turns into a
`display: block` (with appropriate vertical spacing) only in inline math,
matching LaTeX.

* Simplify code

* Fix Jest errors

* NewLine screenshot test

* Bug fix: \\ only works at top level of inline

* Add \newline and \cr to test

* Switch test to pmatrix

* Add vertical space test

* Add \\ vs. \newline tests

* Fix flow errors

* Add \cr test

* Add documentation for \\ at top level

* Comment out newRow

* Fix commenting out
This commit is contained in:
Erik Demaine
2018-05-13 09:58:24 -04:00
committed by GitHub
parent bb1dc0c431
commit 4801ab875a
15 changed files with 116 additions and 23 deletions

57
src/functions/cr.js Normal file
View File

@@ -0,0 +1,57 @@
//@flow
// Row breaks within tabular environments, and line breaks at top level
import defineFunction from "../defineFunction";
import buildCommon from "../buildCommon";
import mathMLTree from "../mathMLTree";
import { calculateSize } from "../units";
import ParseError from "../ParseError";
defineFunction({
type: "cr",
names: ["\\\\", "\\cr", "\\newline"],
props: {
numArgs: 0,
numOptionalArgs: 1,
argTypes: ["size"],
allowedInText: true,
},
handler: (context, args, optArgs) => {
return {
type: "cr",
// \\ and \cr both end the row in a tabular environment
// This flag isn't currently needed by environments/array.js
//newRow: context.funcName !== "\\newline",
// \\ and \newline both end the line in an inline math environment
newLine: context.funcName !== "\\cr",
size: optArgs[0],
};
},
// The following builders are called only at the top level,
// not within tabular environments.
htmlBuilder: (group, options) => {
if (!group.value.newLine) {
throw new ParseError(
"\\cr valid only within a tabular environment");
}
const span = buildCommon.makeSpan(["mspace", "newline"], [], options);
if (group.value.size) {
span.style.marginTop =
calculateSize(group.value.size.value, options) + "em";
}
return span;
},
mathmlBuilder: (group, options) => {
const node = new mathMLTree.MathNode("mspace");
node.setAttribute("linebreak", "newline");
if (group.value.size) {
node.setAttribute("height",
calculateSize(group.value.size.value, options) + "em");
}
return node;
},
});