Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add additional TeXArg types #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions math_keyboard/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## FUTURE VERSION

* Adds additional `TeXArg` types `parentheses_lr`, `caret_braces`, and `underscore_braces`.

## 0.2.1

* Added Android support to the demo project.
Expand Down
32 changes: 30 additions & 2 deletions math_keyboard/lib/src/foundation/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,32 @@ class TeXFunction extends TeX {
switch (type) {
case TeXArg.braces:
return '{';
case TeXArg.underscore_braces:
return '_{';
case TeXArg.caret_braces:
return '^{';
case TeXArg.brackets:
return '[';
default:
case TeXArg.parentheses:
return '(';
case TeXArg.parentheses_lr:
return r'\left(';
}
}

/// Returns the closing character for a function argument.
String closingChar(TeXArg type) {
switch (type) {
case TeXArg.braces:
case TeXArg.underscore_braces:
case TeXArg.caret_braces:
return '}';
case TeXArg.brackets:
return ']';
default:
case TeXArg.parentheses:
return ')';
case TeXArg.parentheses_lr:
return r'\right)';
}
}

Expand Down Expand Up @@ -238,6 +248,18 @@ enum TeXArg {
/// In most of the cases, braces will be used. (E.g arguments of fractions).
braces,

/// _{ }
///
/// This allows you to define subscript arguments on functions that support
/// it (like summation)
underscore_braces,

/// ^{ }
///
/// This allows you to define superscript arguments on functions that support
/// it (like summation)
caret_braces,

/// [ ]
///
/// Brackets are only used for the nth root at the moment.
Expand All @@ -249,4 +271,10 @@ enum TeXArg {
/// for functions like sin, cos, tan, etc. as well, so the user doesn't have
/// to close the parentheses manually.
parentheses,

/// \left( \right)
///
/// These parentheses automatically expand to match the size of their content,
/// making them more visually appealing in most cases.
parentheses_lr,
}
18 changes: 14 additions & 4 deletions math_keyboard/lib/src/foundation/tex2math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ class TeXParser {
(string(r'\frac') | string(r'\log')).map((v) => [v, 'f']);
final function = simpleFunction | otherFunction | sqrt | nrt;

final lp = (string('(') | char('{') | string(r'\left|') | char('['))
final lp = (string('(') |
char('{') |
string(r'\left|') |
char('[') |
string(r'\left('))
.map((v) => [v, 'l']);

final rp = (string(')') | char('}') | string(r'\right|') | char(']'))
final rp = (string(')') |
char('}') |
string(r'\right|') |
char(']') |
string(r'\right)'))
.map((v) => [v, 'r']);

final plus = char('+').map((v) => [
Expand Down Expand Up @@ -170,9 +178,11 @@ class TeXParser {
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] == '(') {
if ((_stream[i][0] == ')' || _stream[i][0] == r'\right)') &&
(_stream[i + 1][0] == '(' || _stream[i + 1][0] == r'\left(')) {
insertTimes = true;
} else if (_stream[i][0] == '}' && _stream[i + 1][0] == '(') {
} else if (_stream[i][0] == '}' &&
(_stream[i + 1][0] == '(' || _stream[i + 1][0] == r'\left(')) {
// 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
Expand Down
Loading