Skip to content

Commit

Permalink
Latex parsing of math space chars.
Browse files Browse the repository at this point in the history
Added \' ' to the list of commands.
  • Loading branch information
kostub committed Jul 22, 2016
1 parent 6529332 commit 6661e0b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
53 changes: 47 additions & 6 deletions iosMath/lib/MTMathListBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,17 @@ - (MTMathList*)buildInternal:(BOOL) oneCharOnly stopChar:(unichar) stop

- (NSString*) readCommand
{
static NSSet<NSNumber*>* singleCharCommands = nil;
if (!singleCharCommands) {
NSArray* singleChars = @[ @'{', @'}', @'$', @'#', @'%', @'_', @'|', @' ', @',', @'>', @';', @'!' ];
singleCharCommands = [[NSSet alloc] initWithArray:singleChars];
}
// a command is a string of all upper and lower case characters.
NSMutableString* mutable = [NSMutableString string];
while([self hasCharacters]) {
unichar ch = [self getNextCharacter];
// Single char commands
if (mutable.length == 0 && (ch == '{' || ch == '}' || ch == '$' || ch == '&' || ch == '#' || ch == '%' || ch == '_' || ch == '|')) {
if (mutable.length == 0 && [singleCharCommands containsObject:@(ch)]) {
// These are single char commands.
[mutable appendString:[NSString stringWithCharacters:&ch length:1]];
break;
Expand Down Expand Up @@ -609,6 +614,7 @@ + (NSDictionary*) supportedCommands
@"#" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"#"],
@"%" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"%"],
@"_" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"_"],
@" " : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@" "],
@"backslash" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\\"],

// Punctuation
Expand Down Expand Up @@ -645,6 +651,14 @@ + (NSDictionary*) supportedCommands
@"imath" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\U0001D6A4"],
@"jmath" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\U0001D6A5"],
@"partial" : [MTMathAtom atomWithType:kMTMathAtomOrdinary value:@"\U0001D715"],

// Spacing
@"," : [[MTMathSpace alloc] initWithSpace:3],
@">" : [[MTMathSpace alloc] initWithSpace:4],
@";" : [[MTMathSpace alloc] initWithSpace:5],
@"!" : [[MTMathSpace alloc] initWithSpace:-3],
@"quad" : [[MTMathSpace alloc] initWithSpace:18], // quad = 1em = 18mu
@"qquad" : [[MTMathSpace alloc] initWithSpace:36], // qquad = 2em
};

}
Expand Down Expand Up @@ -808,6 +822,22 @@ + (NSDictionary*) accentToCommands
return accentToCommands;
}

+ (NSDictionary*) spaceToCommands
{
static NSDictionary* spaceToCommands = nil;
if (!spaceToCommands) {
spaceToCommands = @{
@3 : @",",
@4 : @">",
@5 : @";",
@(-3) : @"!",
@18 : @"quad",
@36 : @"qquad",
};
}
return spaceToCommands;
}

+ (MTMathList *)buildFromString:(NSString *)str
{
MTMathListBuilder* builder = [[MTMathListBuilder alloc] initWithString:str];
Expand Down Expand Up @@ -848,10 +878,7 @@ + (NSString *)mathListToString:(MTMathList *)ml
NSDictionary* textToCommands = [self textToCommands];
NSMutableString* str = [NSMutableString string];
for (MTMathAtom* atom in ml.atoms) {
NSString* command = textToCommands[atom.nucleus];
if (command) {
[str appendFormat:@"\\%@ ", command];
} else if (atom.type == kMTMathAtomFraction) {
if (atom.type == kMTMathAtomFraction) {
MTFraction* frac = (MTFraction*) atom;
if (frac.hasRule) {
[str appendFormat:@"\\frac{%@}{%@}", [self mathListToString:frac.numerator], [self mathListToString:frac.denominator]];
Expand Down Expand Up @@ -906,6 +933,15 @@ + (NSString *)mathListToString:(MTMathList *)ml
MTAccent* accent = (MTAccent*) atom;
NSDictionary* accentToCommands = [MTMathListBuilder accentToCommands];
[str appendFormat:@"\\%@{%@}", accentToCommands[accent.nucleus], [self mathListToString:accent.innerList]];
} else if (atom.type == kMTMathAtomSpace) {
MTMathSpace* space = (MTMathSpace*) atom;
NSDictionary* spaceToCommands = [MTMathListBuilder spaceToCommands];
NSString* command = spaceToCommands[@(space.space)];
if (command) {
[str appendFormat:@"\\%@ ", command];
} else {
[str appendFormat:@"\\mkern%.1fmu", space.space];
}
} else if (atom.nucleus.length == 0) {
[str appendString:@"{}"];
} else if ([atom.nucleus isEqualToString:@"\u2236"]) {
Expand All @@ -915,7 +951,12 @@ + (NSString *)mathListToString:(MTMathList *)ml
// math minus
[str appendString:@"-"];
} else {
[str appendString:atom.nucleus];
NSString* command = textToCommands[atom.nucleus];
if (command) {
[str appendFormat:@"\\%@ ", command];
} else {
[str appendString:atom.nucleus];
}
}

if (atom.superScript) {
Expand Down
23 changes: 22 additions & 1 deletion iosMathTests/MTMathListBuilderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ - (void)tearDown
@[ @"\\pi+\\theta\\geq 3",@[ @(kMTMathAtomVariable), @(kMTMathAtomBinaryOperator), @(kMTMathAtomVariable), @(kMTMathAtomRelation), @(kMTMathAtomNumber)], @"\\pi +\\theta \\geq 3"],
// aliases
@[ @"\\pi\\ne 5 \\land 3", @[ @(kMTMathAtomVariable), @(kMTMathAtomRelation), @(kMTMathAtomNumber), @(kMTMathAtomBinaryOperator), @(kMTMathAtomNumber)], @"\\pi \\neq 5\\wedge 3"],
// control space
@[ @"x \\ y", @[ @(kMTMathAtomVariable), @(kMTMathAtomOrdinary), @(kMTMathAtomVariable)], @"x\\ y"],
// spacing
@[ @"x \\quad y \\; z \\! q", @[ @(kMTMathAtomVariable), @(kMTMathAtomSpace), @(kMTMathAtomVariable),@(kMTMathAtomSpace), @(kMTMathAtomVariable),@(kMTMathAtomSpace), @(kMTMathAtomVariable)], @"x\\quad y\\; z\\! q"],
];
}

Expand Down Expand Up @@ -853,6 +857,24 @@ - (void) testAccent
XCTAssertEqualObjects(latex, @"\\bar{x}", @"%@", desc);
}

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

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @1, @"%@", desc);
MTMathSpace* space = list.atoms[0];
XCTAssertEqual(space.type, kMTMathAtomSpace, @"%@", desc);
XCTAssertEqualObjects(space.nucleus, @"", @"%@", desc);
XCTAssertEqual(space.space, -3);

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

static NSArray* getTestDataParseErrors() {
return @[
@[@"}a", @(MTParseErrorMismatchBraces)],
Expand All @@ -861,7 +883,6 @@ - (void) testAccent
@[@"{5+3", @(MTParseErrorMismatchBraces)],
@[@"5+3}", @(MTParseErrorMismatchBraces)],
@[@"{1+\\frac{3+2", @(MTParseErrorMismatchBraces)],
@[@"\\ + 3", @(MTParseErrorInvalidCommand)],
@[@"1+\\left", @(MTParseErrorMissingDelimiter)],
@[@"\\left(\\frac12\\right", @(MTParseErrorMissingDelimiter)],
@[@"\\left 5 + 3 \\right)", @(MTParseErrorInvalidDelimiter)],
Expand Down

0 comments on commit 6661e0b

Please sign in to comment.