WeBWorK Problems

extra parens in a formula with fractions

extra parens in a formula with fractions

by Alex Jordan -
Number of replies: 2

Consider this MWE.

DOCUMENT();

loadMacros(qw(PGstandard.pl PGML.pl contextFraction.pl));

Context("Fraction");
$f = Fraction(1/2);
$g = Compute("$f x");

BEGIN_PGML
[`[$g]`]
END_PGML

ENDDOCUMENT();


This displays $g as
\left({\frac{1}{2}}\right)x

rather than
{\frac{1}{2}}x

Parentheses are introduced because the string interpolation for "$f x" adds parentheses around $f. Then they are in the string passed to Compute, so I suspect that the hadParens property on the division operator in the parse tree is set to 1 (or something like that) and this leads to the string or TeX method on $g using those parens, even if they are not needed for order of operations precedence.

Or maybe they *are* needed for precedence? Is the implied multiplication operator a higher precedence than the division operator?

In any case, I'm wondering if there is a way to manipulate the context so that the above comes out as just

{\frac{1}{2}}x
I've tried the flag showExtraParens=>0 but that does not suppress the parens here in this case.

I know I could construct $g a different way to make the parens disappear, but there is a whole block of exercises that I'd like to change without micromanaging their code. If there is a context solution to this, it would be better for what I have to do here.


In reply to Alex Jordan

Re: extra parens in a formula with fractions

by Jaimos Skriletz -

It appears that contextFraction handles parens a bit different than other objects, and will always show them if the original string had them. There is a parser flag hadParens that is set if the original string had parens and contextFraction will show parens in the string and TeX output in this case. Looking through the code I don't see any context way to modify this behavior, but I was able to test and remove the parens by adding the following to your example right under the definition of $g.

$g->{tree}{lop}{hadParens} = 0;

If you look at the TeX and string methods of contextFraction.pl you will see that parenthesis are always shown in the output if this is set.

In reply to Alex Jordan

Re: extra parens in a formula with fractions

by Davide Cervone -

You and Jaimos are correct that this comes from the hadParens property. It is possible to remove this programmatically, if your problems include PGcourse.pl as the final macro file being loaded, you could add

$context{Fraction}{parser}{Value} = 'my::context::Frac::Parser::Value'
	if defined $context{Fraction};

package my::context::Frac::Parser::Value;
our @ISA = ('context::Fraction::Parser::Value');

sub string {
	my $self = shift;
	delete $self->{hadParens} if $self->{value}->classMatch('Fraction');
	$self->SUPER::string(@_);
}

sub TeX {
	my $self = shift;
	delete $self->{hadParens} if $self->{value}->classMatch('Fraction');
	$self->SUPER::TeX(@_);
}

package main;
This overrides the object class that is inserting the parentheses and subclasses it, replacing the string and TeX methods to clear the hadParens property. Alternatively, you could make a separate macro file that contains these lines and use loadMacros() to load it in all the problems you are wanting to modify. Or you could put a copy of contextFraction.pl in your course's templates/macros directory and edit it to remove the two references to hadParens. Any of those should work.