Skip to content

Commit

Permalink
Merge pull request #512 from dpvc/fix-fraction-parens
Browse files Browse the repository at this point in the history
Properly add parentheses to Fraction::Value objects based on precedence.
  • Loading branch information
Alex-Jordan authored Dec 16, 2020
2 parents a0da682 + 9f2ca68 commit 438d97c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions macros/contextFraction.pl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,32 @@ sub reduce {
return $self->SUPER::reduce;
}

#
# Add parentheses if they were there originally, or are needed by precedence
#
sub string {
my $self = shift;
my $string = $self->SUPER::string($self, @_);
return $string unless $self->{value}->classMatch('Fraction');
my $precedence = shift;
my $frac = $self->context->operators->get('/')->{precedence};
$string = '(' . $string . ')' if $self->{hadParens} || (defined $precedence && $precedence > $frac);
return $string;
}

#
# Add parentheses if they are needed by precedence
#
sub TeX {
my $self = shift;
my $string = $self->SUPER::TeX($self, @_);
return $string unless $self->{value}->classMatch('Fraction');
my $precedence = shift;
my $frac = $self->context->operators->get('/')->{precedence};
$string = '\left(' . $string . '\right)' if defined $precedence && $precedence > $frac;
return $string;
}

###########################################################################

package context::Fraction::Real;
Expand Down

0 comments on commit 438d97c

Please sign in to comment.