diff --git a/lib/Parser.pm b/lib/Parser.pm index d3a249932c..b2d1affd68 100644 --- a/lib/Parser.pm +++ b/lib/Parser.pm @@ -401,8 +401,8 @@ sub Close { ($top->type eq 'Comma') ? $top->entryType : $top->typeRef, ($type ne 'start') ? ($open,$paren->{close}) : () )}; } else { - $top->{value}{hadParens} = 1; - } + $top->{value}{hadParens} = 1 unless $open eq 'start'; + } $self->pop; $self->push($top); $self->CloseFn() if ($paren->{function} && $self->prev->{type} eq 'fn'); } elsif ($paren->{formInterval} eq $type && $self->top->{value}->length == 2) { diff --git a/macros/contextFraction.pl b/macros/contextFraction.pl index 2c5585a93a..2644529566 100644 --- a/macros/contextFraction.pl +++ b/macros/contextFraction.pl @@ -618,7 +618,7 @@ sub reduce { # sub string { my $self = shift; - my $string = $self->SUPER::string($self, @_); + my $string = $self->SUPER::string(@_); return $string unless $self->{value}->classMatch('Fraction'); my $precedence = shift; my $frac = $self->context->operators->get('/')->{precedence}; @@ -627,15 +627,18 @@ sub string { } # -# Add parentheses if they are needed by precedence +# Add parentheses if they were there originally, or +# are needed by precedence and we asked for exxxtra parens # sub TeX { my $self = shift; - my $string = $self->SUPER::TeX($self, @_); + my $string = $self->SUPER::TeX(@_); 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; + my $noparens = shift; + $string = '\left(' . $string . '\right)' if $self->{hadParens} || + (defined $precedence && $precedence > $frac && !$noparens); return $string; } @@ -898,7 +901,6 @@ sub string { if ($self->getFlagWithAlias("showMixedNumbers","showProperFractions") && CORE::abs($a) > $b) {$n = int($a/$b); $a = CORE::abs($a) % $b; $n .= " " unless $a == 0} $n .= "$a/$b" unless $a == 0 && $n ne ''; - $n = "($n)" if defined $prec && $prec >= 1; return "$n"; } @@ -911,7 +913,6 @@ sub TeX { my $s = ""; ($a,$s) = (-$a,"-") if $a < 0; $n .= ($self->{isHorizontal} ? "$s$a/$b" : "${s}{\\textstyle\\frac{$a}{$b}}") unless $a == 0 && $n ne ''; - $n = "\\left($n\\right)" if defined $prec && $prec >= 1; return "$n"; }