Skip to content

Commit 8e72442

Browse files
authored
Implicit times between parentheses (#28)
* add times between closing and opening parenthesis * bump version * insert times * changelog
1 parent a2cbd00 commit 8e72442

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

math_keyboard/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.4
2+
3+
* Fixed missing implicit times operator between a closing and an opening parenthesis.
4+
15
## 0.1.3
26

37
* Added new symbols icon for functions button.

math_keyboard/lib/src/foundation/tex2math.dart

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class TeXParser {
129129

130130
for (var i = 0; i < _stream.length; i++) {
131131
/// wrong syntax: fr fo lr lo oo (b/r postfix or wrong)
132-
/// need times: bb bf bl rb rf !f !l
132+
/// need times: bb bf bl rb rf rl !f !l
133133
/// negative number: -(bfl) / l-(bfl)
134134
135135
// negative number
@@ -161,18 +161,49 @@ class TeXParser {
161161
continue;
162162
}
163163
if (i < _stream.length - 1 && _stream[i][1] == 'r') {
164+
var insertTimes = false;
164165
switch (_stream[i + 1][1]) {
165166
case 'b':
166167
case 'f':
167-
_stream.insert(i + 1, [
168-
r'\times',
169-
['o', 3, 'l'],
170-
]);
171-
i++;
168+
insertTimes = true;
169+
break;
170+
case 'l':
171+
// In case there is a closing parenthesis directly followed by an
172+
// opening one, some further checks are necessary.
173+
if (_stream[i][0] == ')' && _stream[i + 1][0] == '(') {
174+
insertTimes = true;
175+
} else if (_stream[i][0] == '}' && _stream[i + 1][0] == '(') {
176+
// This case is unfavorable. If the '}' closes the second argument
177+
// of a fraction or marks the end of an exponent, we want to
178+
// insert 'times'. However, if '}' closes the base argument of a
179+
// logarithm function, we don't want to insert a times symbol.
180+
// That's why we have to check what's in front of the matching
181+
// opening '{'.
182+
final stack = ['}'];
183+
var j = i - 1;
184+
while (j > 0 && stack.isNotEmpty) {
185+
if (_stream[j][0] == '{') {
186+
stack.removeLast();
187+
} else if (_stream[j][0] == '}') {
188+
stack.add('}');
189+
}
190+
j--;
191+
}
192+
if (j >= 0 && _stream[j][0] != '_') {
193+
insertTimes = true;
194+
}
195+
}
172196
break;
173197
default:
174198
break;
175199
}
200+
if (insertTimes) {
201+
_stream.insert(i + 1, [
202+
r'\times',
203+
['o', 3, 'l'],
204+
]);
205+
i++;
206+
}
176207
continue;
177208
}
178209
if (i < _stream.length - 1 && _stream[i][0] == '!') {

math_keyboard/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: math_keyboard
22
description: >-2
33
Math expression editing using an on-screen software keyboard or physical keyboard input in a
44
typeset input field in Flutter.
5-
version: 0.1.3
5+
version: 0.1.4
66
homepage: https://github.com/simpleclub/math_keyboard/tree/main/math_keyboard
77

88
environment:

math_keyboard/test/foundation/tex2math_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ void main() {
5656
Parser().parse(exp).toString(),
5757
);
5858
});
59+
60+
test('implicit2', () {
61+
const tex = '(23)({c})';
62+
const exp = '23*c';
63+
expect(
64+
TeXParser(tex).parse().toString(),
65+
Parser().parse(exp).toString(),
66+
);
67+
});
68+
69+
test('implicit3', () {
70+
const tex = '23^{2}({c})';
71+
const exp = '23^2*c';
72+
expect(
73+
TeXParser(tex).parse().toString(),
74+
Parser().parse(exp).toString(),
75+
);
76+
});
5977
});
6078

6179
group('frac', () {
@@ -154,6 +172,15 @@ void main() {
154172
Parser().parse(exp).toString(),
155173
);
156174
});
175+
176+
test('nRoot', () {
177+
const tex = r'2 \times \sqrt[3]{{x}}';
178+
const exp = '(2.0 * (x^(1.0 / 3.0)))';
179+
expect(
180+
TeXParser(tex).parse().toString(),
181+
Parser().parse(exp).toString(),
182+
);
183+
});
157184
});
158185

159186
group('logarithm', () {

0 commit comments

Comments
 (0)