From 9f2ca68282a260bc51ce4ab361e7d04bb7ec433b Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Wed, 9 Dec 2020 14:50:38 -0500 Subject: [PATCH] Properly add parentheses to Fraction::Value objects based on precedence. --- macros/contextFraction.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/macros/contextFraction.pl b/macros/contextFraction.pl index 25ce008832..fd85418503 100644 --- a/macros/contextFraction.pl +++ b/macros/contextFraction.pl @@ -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;