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

Properly add parentheses to Fraction::Value objects based on precedence. #512

Merged
merged 1 commit into from
Dec 16, 2020
Merged
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
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