Skip to content

Commit

Permalink
Implicit times between parentheses (#28)
Browse files Browse the repository at this point in the history
* add times between closing and opening parenthesis

* bump version

* insert times

* changelog
  • Loading branch information
edhom committed Jan 12, 2022
1 parent a2cbd00 commit 8e72442
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
4 changes: 4 additions & 0 deletions math_keyboard/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.4

* Fixed missing implicit times operator between a closing and an opening parenthesis.

## 0.1.3

* Added new symbols icon for functions button.
Expand Down
43 changes: 37 additions & 6 deletions math_keyboard/lib/src/foundation/tex2math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class TeXParser {

for (var i = 0; i < _stream.length; i++) {
/// wrong syntax: fr fo lr lo oo (b/r postfix or wrong)
/// need times: bb bf bl rb rf !f !l
/// need times: bb bf bl rb rf rl !f !l
/// negative number: -(bfl) / l-(bfl)
// negative number
Expand Down Expand Up @@ -161,18 +161,49 @@ class TeXParser {
continue;
}
if (i < _stream.length - 1 && _stream[i][1] == 'r') {
var insertTimes = false;
switch (_stream[i + 1][1]) {
case 'b':
case 'f':
_stream.insert(i + 1, [
r'\times',
['o', 3, 'l'],
]);
i++;
insertTimes = true;
break;
case 'l':
// In case there is a closing parenthesis directly followed by an
// opening one, some further checks are necessary.
if (_stream[i][0] == ')' && _stream[i + 1][0] == '(') {
insertTimes = true;
} else if (_stream[i][0] == '}' && _stream[i + 1][0] == '(') {
// This case is unfavorable. If the '}' closes the second argument
// of a fraction or marks the end of an exponent, we want to
// insert 'times'. However, if '}' closes the base argument of a
// logarithm function, we don't want to insert a times symbol.
// That's why we have to check what's in front of the matching
// opening '{'.
final stack = ['}'];
var j = i - 1;
while (j > 0 && stack.isNotEmpty) {
if (_stream[j][0] == '{') {
stack.removeLast();
} else if (_stream[j][0] == '}') {
stack.add('}');
}
j--;
}
if (j >= 0 && _stream[j][0] != '_') {
insertTimes = true;
}
}
break;
default:
break;
}
if (insertTimes) {
_stream.insert(i + 1, [
r'\times',
['o', 3, 'l'],
]);
i++;
}
continue;
}
if (i < _stream.length - 1 && _stream[i][0] == '!') {
Expand Down
2 changes: 1 addition & 1 deletion math_keyboard/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: math_keyboard
description: >-2
Math expression editing using an on-screen software keyboard or physical keyboard input in a
typeset input field in Flutter.
version: 0.1.3
version: 0.1.4
homepage: https://github.com/simpleclub/math_keyboard/tree/main/math_keyboard

environment:
Expand Down
27 changes: 27 additions & 0 deletions math_keyboard/test/foundation/tex2math_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ void main() {
Parser().parse(exp).toString(),
);
});

test('implicit2', () {
const tex = '(23)({c})';
const exp = '23*c';
expect(
TeXParser(tex).parse().toString(),
Parser().parse(exp).toString(),
);
});

test('implicit3', () {
const tex = '23^{2}({c})';
const exp = '23^2*c';
expect(
TeXParser(tex).parse().toString(),
Parser().parse(exp).toString(),
);
});
});

group('frac', () {
Expand Down Expand Up @@ -154,6 +172,15 @@ void main() {
Parser().parse(exp).toString(),
);
});

test('nRoot', () {
const tex = r'2 \times \sqrt[3]{{x}}';
const exp = '(2.0 * (x^(1.0 / 3.0)))';
expect(
TeXParser(tex).parse().toString(),
Parser().parse(exp).toString(),
);
});
});

group('logarithm', () {
Expand Down

0 comments on commit 8e72442

Please sign in to comment.