Change cell style to 'text' in {array}, {matrix}, {cases}. Add {darray} and {dcases}. (#606)

* Change cell style to 'text' in {array}, {matrix}, {cases}.
* Add {darray} and {dcases} which use display style for their cells.
* Add ArrayMode test with \frac's inside {array} in display mode.
This commit is contained in:
Erik Demaine
2017-01-15 16:17:45 -05:00
committed by Martin von Gagern
parent 38ba9f9187
commit f1c02226cc
4 changed files with 44 additions and 10 deletions

View File

@@ -8,15 +8,23 @@ const ParseNode = parseData.ParseNode;
/**
* Parse the body of the environment, with rows delimited by \\ and
* columns delimited by &, and create a nested list in row-major order
* with one group per cell.
* with one group per cell. If given an optional argument style
* ("text", "display", etc.), then each cell is cast into that style.
*/
function parseArray(parser, result) {
function parseArray(parser, result, style) {
let row = [];
const body = [row];
const rowGaps = [];
while (true) {
const cell = parser.parseExpression(false, null);
row.push(new ParseNode("ordgroup", cell, parser.mode));
let cell = parser.parseExpression(false, null);
cell = new ParseNode("ordgroup", cell, parser.mode);
if (style) {
cell = new ParseNode("styling", {
style: style,
value: [cell],
}, parser.mode);
}
row.push(cell);
const next = parser.nextToken.text;
if (next === "&") {
parser.consume();
@@ -82,9 +90,21 @@ function defineEnvironment(names, props, handler) {
}
}
// Decides on a style for cells in an array according to whether the given
// environment name starts with the letter 'd'.
function dCellStyle(envName) {
if (envName.substr(0, 1) === "d") {
return "display";
} else {
return "text";
}
}
// Arrays are part of LaTeX, defined in lttab.dtx so its documentation
// is part of the source2e.pdf file of LaTeX2e source documentation.
defineEnvironment("array", {
// {darray} is an {array} environment where cells are set in \displaystyle,
// as defined in nccmath.sty.
defineEnvironment(["array", "darray"], {
numArgs: 1,
}, function(context, args) {
let colalign = args[0];
@@ -111,7 +131,7 @@ defineEnvironment("array", {
cols: cols,
hskipBeforeAndAfter: true, // \@preamble in lttab.dtx
};
res = parseArray(context.parser, res);
res = parseArray(context.parser, res, dCellStyle(context.envName));
return res;
});
@@ -138,7 +158,7 @@ defineEnvironment([
type: "array",
hskipBeforeAndAfter: false, // \hskip -\arraycolsep in amsmath
};
res = parseArray(context.parser, res);
res = parseArray(context.parser, res, dCellStyle(context.envName));
if (delimiters) {
res = new ParseNode("leftright", {
body: [res],
@@ -152,7 +172,12 @@ defineEnvironment([
// A cases environment (in amsmath.sty) is almost equivalent to
// \def\arraystretch{1.2}%
// \left\{\begin{array}{@{}l@{\quad}l@{}} … \end{array}\right.
defineEnvironment("cases", {
// {dcases} is a {cases} environment where cells are set in \displaystyle,
// as defined in mathtools.sty.
defineEnvironment([
"cases",
"dcases",
], {
}, function(context) {
let res = {
type: "array",
@@ -173,7 +198,7 @@ defineEnvironment("cases", {
postgap: 0,
}],
};
res = parseArray(context.parser, res);
res = parseArray(context.parser, res, dCellStyle(context.envName));
res = new ParseNode("leftright", {
body: [res],
left: "\\{",