Skip to content

Commit 248a101

Browse files
authored
Merge pull request #1793 from dpvc/issue1482
Better test for \text{} in \cases{}. #1482
2 parents 5cf6a89 + 5dcac77 commit 248a101

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

unpacked/jax/input/TeX/jax.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,20 +1701,68 @@
17011701
Entry: function (name) {
17021702
this.Push(STACKITEM.cell().With({isEntry: true, name: name}));
17031703
if (this.stack.Top().isCases) {
1704+
//
1705+
// Make second column be in \text{...} (unless it is already
1706+
// in a \text{...}, for backward compatibility).
1707+
//
17041708
var string = this.string;
1705-
var braces = 0, i = this.i, m = string.length;
1709+
var braces = 0, close = -1, i = this.i, m = string.length;
1710+
//
1711+
// Look through the string character by character...
1712+
//
17061713
while (i < m) {
17071714
var c = string.charAt(i);
1708-
if (c === "{") {braces++; i++}
1709-
else if (c === "}") {if (braces === 0) {m = 0} else {braces--; i++}}
1710-
else if (c === "&" && braces === 0) {
1715+
if (c === "{") {
1716+
//
1717+
// Increase the nested brace count and go on
1718+
//
1719+
braces++;
1720+
i++;
1721+
} else if (c === "}") {
1722+
//
1723+
// If there are too many close braces, just end (we will get an
1724+
// error message later when the rest of the string is parsed)
1725+
// Otherwise
1726+
// decrease the nested brace count,
1727+
// if it is now zero and we haven't already marked the end of the
1728+
// first brace group, record the position (use to check for \text{} later)
1729+
// go on to the next character.
1730+
//
1731+
if (braces === 0) {
1732+
m = 0;
1733+
} else {
1734+
braces--;
1735+
if (braces === 0 && close < 0) {
1736+
close = i - this.i;
1737+
}
1738+
i++;
1739+
}
1740+
} else if (c === "&" && braces === 0) {
1741+
//
1742+
// Extra alignment tabs are not allowed in cases
1743+
//
17111744
TEX.Error(["ExtraAlignTab","Extra alignment tab in \\cases text"]);
17121745
} else if (c === "\\") {
1746+
//
1747+
// If the macro is \cr or \\, end the search, otherwise skip the macro
1748+
// (multi-letter names don't matter, as we will skip the rest of the
1749+
// characters in the main loop)
1750+
//
17131751
if (string.substr(i).match(/^((\\cr)[^a-zA-Z]|\\\\)/)) {m = 0} else {i += 2}
1714-
} else {i++}
1752+
} else {
1753+
//
1754+
// Go on to the next character
1755+
//
1756+
i++;
1757+
}
17151758
}
1759+
//
1760+
// Check if the second column text is already in \text{},
1761+
// If not, process the second column as text and continue parsing from there,
1762+
// (otherwise process the second column as normal, since it is in \text{}
1763+
//
17161764
var text = string.substr(this.i,i-this.i);
1717-
if (!text.match(/^\s*\\text[^a-zA-Z]/)) {
1765+
if (!text.match(/^\s*\\text[^a-zA-Z]/) || close !== text.replace(/\s+$/,'').length - 1) {
17181766
this.Push.apply(this,this.InternalMath(text,0));
17191767
this.i = i;
17201768
}

0 commit comments

Comments
 (0)