Skip to content

Commit

Permalink
Adding latex support for \choose, \brack, \brace
Browse files Browse the repository at this point in the history
  • Loading branch information
kostub committed Jul 14, 2016
1 parent 7a7a3c8 commit e7b7ba9
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 12 deletions.
44 changes: 32 additions & 12 deletions iosMath/lib/MTMathListBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ - (MTMathAtom*) atomForCommand:(NSString*) command

- (MTMathList*) stopCommand:(NSString*) command list:(MTMathList*) list stopChar:(unichar) stopChar
{
static NSDictionary<NSString*, NSArray*>* fractionCommands = nil;
if (!fractionCommands) {
fractionCommands = @{ @"over" : @[],
@"atop" : @[],
@"choose" : @[ @"(", @")"],
@"brack" : @[ @"[", @"]"],
@"brace" : @[ @"{", @"}"]};
}
if ([command isEqualToString:@"right"]) {
NSString* delim = [self getDelimiterValue:@"right"];
if (!delim) {
Expand All @@ -326,18 +334,18 @@ - (MTMathList*) stopCommand:(NSString*) command list:(MTMathList*) list stopChar
_currentInnerAtom.rightBoundary = [MTMathAtom atomWithType:kMTMathAtomBoundary value:delim];
// return the list read so far.
return list;
} else if ([command isEqualToString:@"over"]) {
MTFraction* frac = [[MTFraction alloc] init];
frac.numerator = list;
frac.denominator = [self buildInternal:NO stopChar:stopChar];
if (_error) {
return nil;
} else if ([fractionCommands objectForKey:command]) {
MTFraction* frac = nil;
if ([command isEqualToString:@"over"]) {
frac = [[MTFraction alloc] init];
} else {
frac = [[MTFraction alloc] initWithRule:NO];
}
NSArray* delims = [fractionCommands objectForKey:command];
if (delims.count == 2) {
frac.leftDelimiter = delims[0];
frac.rightDelimiter = delims[1];
}
MTMathList* fracList = [MTMathList new];
[fracList addAtom:frac];
return fracList;
} else if ([command isEqualToString:@"atop"]) {
MTFraction* frac = [[MTFraction alloc] initWithRule:NO];
frac.numerator = list;
frac.denominator = [self buildInternal:NO stopChar:stopChar];
if (_error) {
Expand Down Expand Up @@ -768,7 +776,19 @@ + (NSString *)mathListToString:(MTMathList *)ml
if (frac.hasRule) {
[str appendFormat:@"\\frac{%@}{%@}", [self mathListToString:frac.numerator], [self mathListToString:frac.denominator]];
} else {
[str appendFormat:@"{%@ \\atop %@}", [self mathListToString:frac.numerator], [self mathListToString:frac.denominator]];
NSString* command = nil;
if (!frac.leftDelimiter && !frac.rightDelimiter) {
command = @"atop";
} else if ([frac.leftDelimiter isEqualToString:@"("] && [frac.rightDelimiter isEqualToString:@")"]) {
command = @"choose";
} else if ([frac.leftDelimiter isEqualToString:@"{"] && [frac.rightDelimiter isEqualToString:@"}"]) {
command = @"brace";
} else if ([frac.leftDelimiter isEqualToString:@"["] && [frac.rightDelimiter isEqualToString:@"]"]) {
command = @"brack";
} else {
command = [NSString stringWithFormat:@"atopwithdelims%@%@", frac.leftDelimiter, frac.rightDelimiter];
}
[str appendFormat:@"{%@ \\%@ %@}", [self mathListToString:frac.numerator], command, [self mathListToString:frac.denominator]];
}
} else if (atom.type == kMTMathAtomRadical) {
[str appendString:@"\\sqrt"];
Expand Down
115 changes: 115 additions & 0 deletions iosMathTests/MTMathListBuilderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ - (void) testFrac
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertTrue(frac.hasRule);
XCTAssertNil(frac.rightDelimiter);
XCTAssertNil(frac.leftDelimiter);

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
Expand Down Expand Up @@ -505,6 +507,8 @@ - (void) testOver
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertTrue(frac.hasRule);
XCTAssertNil(frac.rightDelimiter);
XCTAssertNil(frac.leftDelimiter);

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
Expand Down Expand Up @@ -541,6 +545,8 @@ - (void) testOverInParens
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertTrue(frac.hasRule);
XCTAssertNil(frac.rightDelimiter);
XCTAssertNil(frac.leftDelimiter);

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
Expand Down Expand Up @@ -574,6 +580,8 @@ - (void) testAtop
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertNil(frac.rightDelimiter);
XCTAssertNil(frac.leftDelimiter);

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
Expand Down Expand Up @@ -610,6 +618,8 @@ - (void) testAtopInParens
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertNil(frac.rightDelimiter);
XCTAssertNil(frac.leftDelimiter);

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
Expand All @@ -631,6 +641,111 @@ - (void) testAtopInParens
XCTAssertEqualObjects(latex, @"5+{1 \\atop c}+8", @"%@", desc);
}

- (void) testChoose
{
NSString *str = @"n \\choose k";
MTMathList* list = [MTMathListBuilder buildFromString:str];
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @1, @"%@", desc);
MTFraction* frac = list.atoms[0];
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertEqualObjects(frac.rightDelimiter, @")");
XCTAssertEqualObjects(frac.leftDelimiter, @"(");

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
MTMathAtom *atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"n", @"%@", desc);

atom = list.atoms[0];
subList = frac.denominator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"k", @"%@", desc);

// convert it back to latex
NSString* latex = [MTMathListBuilder mathListToString:list];
XCTAssertEqualObjects(latex, @"{n \\choose k}", @"%@", desc);
}

- (void) testBrack
{
NSString *str = @"n \\brack k";
MTMathList* list = [MTMathListBuilder buildFromString:str];
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @1, @"%@", desc);
MTFraction* frac = list.atoms[0];
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertEqualObjects(frac.rightDelimiter, @"]");
XCTAssertEqualObjects(frac.leftDelimiter, @"[");

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
MTMathAtom *atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"n", @"%@", desc);

atom = list.atoms[0];
subList = frac.denominator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"k", @"%@", desc);

// convert it back to latex
NSString* latex = [MTMathListBuilder mathListToString:list];
XCTAssertEqualObjects(latex, @"{n \\brack k}", @"%@", desc);
}

- (void) testBrace
{
NSString *str = @"n \\brace k";
MTMathList* list = [MTMathListBuilder buildFromString:str];
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @1, @"%@", desc);
MTFraction* frac = list.atoms[0];
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertEqualObjects(frac.rightDelimiter, @"}");
XCTAssertEqualObjects(frac.leftDelimiter, @"{");

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
MTMathAtom *atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"n", @"%@", desc);

atom = list.atoms[0];
subList = frac.denominator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"k", @"%@", desc);

// convert it back to latex
NSString* latex = [MTMathListBuilder mathListToString:list];
XCTAssertEqualObjects(latex, @"{n \\brace k}", @"%@", desc);
}

static NSArray* getTestDataParseErrors() {
return @[
@[@"}a", @(MTParseErrorMismatchBraces)],
Expand Down

0 comments on commit e7b7ba9

Please sign in to comment.