Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions testsuite/tests/input/tex/Base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4002,6 +4002,36 @@ describe('Complete Array', () => {
expect(tex2mml('\\begin{array}{> {x} c} X \\end{array}')).toMatchSnapshot();
});

it('column {cc}', () => {
expect(tex2mml('\\begin{array}{{cc}} X Y \\end{array}')).toMatchSnapshot();
});

it('column {c}', () => {
expect(tex2mml('\\begin{array}{c{c}} X Y \\end{array}')).toMatchSnapshot();
});

it('column {{c}}', () => {
expect(tex2mml('\\begin{array}{{{c}}} X \\end{array}')).toMatchSnapshot();
});

it('column {r}c{l}', () => {
expect(tex2mml('\\begin{array}{{r}c{l}} X & Y & Z \\end{array}')).toMatchSnapshot();
});

it('column newline', () => {
expect(tex2mml('\\begin{array}{c\nc} X & Y\\end{array}')).toMatchSnapshot();
});

it('column > > c < <', () => {
expect(tex2mml('\\begin{array}{>{a}>{b}c<{x}<{y}} X \\end{array}')).toMatchSnapshot();
});

it('column brace errors', () => {
expectTexError(tex2mml('\\begin{array}{c{xx}} X Y \\end{array}')).toBe('Illegal pream-token (xx)');
expectTexError(tex2mml('\\begin{array}{c{{c}}} X Y \\end{array}')).toBe('Illegal pream-token ({c})');
expectTexError(tex2mml('\\begin{array}{c{c{c}}} X Y \\end{array}')).toBe('Illegal pream-token (c{c})');
});

it('BadPreamToken', () => {
expectTexError('\\begin{array}a').toBe('Illegal pream-token (a)');
});
Expand Down
90 changes: 90 additions & 0 deletions testsuite/tests/input/tex/__snapshots__/Base.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1820,6 +1820,22 @@ exports[`Complete Array column ! | @ 1`] = `
</math>"
`;

exports[`Complete Array column > > c < < 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{&gt;{a}&gt;{b}c&lt;{x}&lt;{y}} X \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" data-frame-styles="" framespacing=".5em .125em" data-latex="baXyx\\end{array}">
<mtr>
<mtd>
<mi data-latex="b">b</mi>
<mi data-latex="a">a</mi>
<mi data-latex="X">X</mi>
<mi data-latex="y">y</mi>
<mi data-latex="x">x</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column > space 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{&gt; {x} c} X \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" data-frame-styles="" framespacing=".5em .125em" data-latex="xX\\end{array}">
Expand Down Expand Up @@ -1872,6 +1888,62 @@ exports[`Complete Array column @ p m 1`] = `
</math>"
`;

exports[`Complete Array column {{c}} 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{{{c}}} X \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" data-frame-styles="" framespacing=".5em .125em" data-latex="\\begin{array}{{{c}}} X \\end{array}">
<mtr data-latex="{{{c}}}">
<mtd>
<mi data-latex="X">X</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column {c} 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{c{c}} X Y \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="center center" data-frame-styles="" framespacing=".5em .125em" data-latex="\\begin{array}{c{c}} X Y \\end{array}">
<mtr data-latex="{c{c}}">
<mtd>
<mi data-latex="X">X</mi>
<mi data-latex="Y">Y</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column {cc} 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{{cc}} X Y \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="center center" data-frame-styles="" framespacing=".5em .125em" data-latex="\\begin{array}{{cc}} X Y \\end{array}">
<mtr data-latex="{{cc}}">
<mtd>
<mi data-latex="X">X</mi>
<mi data-latex="Y">Y</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column {r}c{l} 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{{r}c{l}} X &amp; Y &amp; Z \\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="right center left" data-frame-styles="" framespacing=".5em .125em" data-latex="\\begin{array}{{r}c{l}} X &amp; Y &amp; Z \\end{array}">
<mtr data-latex="{{r}c{l}}">
<mtd>
<mi data-latex="X">X</mi>
</mtd>
<mtd>
<mi data-latex="Y">Y</mi>
</mtd>
<mtd>
<mi data-latex="Z">Z</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column b 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{b{1cm}b{1cm}b{1cm}}a &amp; b &amp;c\\\\ d &amp; e &amp; f\\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="left left left" columnwidth="1cm 1cm 1cm" data-frame-styles="" framespacing=".5em .125em" data-latex="\\text{f}\\end{array}">
Expand Down Expand Up @@ -2179,6 +2251,24 @@ exports[`Complete Array column m 1`] = `
</math>"
`;

exports[`Complete Array column newline 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{c
c} X &amp; Y\\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="center center" data-frame-styles="" framespacing=".5em .125em" data-latex="\\begin{array}{c
c} X &amp; Y\\end{array}">
<mtr data-latex="{c
c}">
<mtd>
<mi data-latex="X">X</mi>
</mtd>
<mtd>
<mi data-latex="Y">Y</mi>
</mtd>
</mtr>
</mtable>
</math>"
`;

exports[`Complete Array column p 1`] = `
"<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\begin{array}{p{1cm}p{1cm}p{1cm}}a &amp; b &amp;c\\\\ d &amp; e &amp; f\\end{array}" display="block">
<mtable columnspacing="1em" rowspacing="4pt" columnalign="left left left" columnwidth="1cm 1cm 1cm" data-frame-styles="" framespacing=".5em .125em" data-latex="\\text{f}\\end{array}">
Expand Down
44 changes: 38 additions & 6 deletions ts/input/tex/ColumnParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ export class ColumnParser {
':': (state) => this.addRule(state, 'dashed'),
'>': (state) =>
(state.cstart[state.j] =
(state.cstart[state.j] || '') + this.getBraces(state)),
this.getBraces(state) + (state.cstart[state.j] || '')),
'<': (state) =>
(state.cend[state.j - 1] =
(state.cend[state.j - 1] || '') + this.getBraces(state)),
this.getBraces(state) + (state.cend[state.j - 1] || '')),
'@': (state) => this.addAt(state, this.getBraces(state)),
'!': (state) => this.addBang(state, this.getBraces(state)),
'*': (state) => this.repeat(state),
'{': (state) => this.brace(state),
//
// Non-standard for math-mode versions
//
Expand All @@ -94,6 +95,7 @@ export class ColumnParser {
// Ignored
//
' ': (_state) => {},
'\n': (_state_) => {},
};

/**
Expand Down Expand Up @@ -128,6 +130,16 @@ export class ColumnParser {
cextra: array.cextra,
};
//
// Remove one pair of braces, if there are any
//
if (template.charAt(0) === '{' && template.slice(-1) === '}') {
const braced = this.getBraces(state);
if (braced.length === template.length - 2) {
state.template = braced;
}
state.i = 0;
}
//
// Loop through the template to process the column specifiers
//
let n = 0;
Expand All @@ -141,10 +153,7 @@ export class ColumnParser {
const code = state.template.codePointAt(state.i);
const c = (state.c = String.fromCodePoint(code));
state.i += c.length;
if (!Object.hasOwn(this.columnHandler, c)) {
throw new TexError('BadPreamToken', 'Illegal pream-token (%1)', c);
}
this.columnHandler[c](state);
this.processColumn(state, c);
}
//
// Set array definition values
Expand All @@ -156,6 +165,19 @@ export class ColumnParser {
this.setPadding(state, array);
}

/**
* Process a column specifier, or throw an error if not defined.
*
* @param {ColumnState} state The current state of the parser
* @param {string} c The column type to process
*/
protected processColumn(state: ColumnState, c: string) {
if (!Object.hasOwn(this.columnHandler, c)) {
throw new TexError('BadPreamToken', 'Illegal pream-token (%1)', c);
}
this.columnHandler[c](state);
}

/**
* @param {ColumnState} state The current state of the parser
* @param {ArrayItem} array The array stack item to adjust
Expand Down Expand Up @@ -420,4 +442,14 @@ export class ColumnParser {
new Array(n).fill(cols).join('') + state.template.substring(state.i);
state.i = 0;
}

/**
* Process a braced column type
*
* @param {ColumnState} state The current state of the parser
*/
public brace(state: ColumnState) {
state.i--;
this.processColumn(state, this.getBraces(state));
}
}
Loading