WeBWorK Problems

RadioButtons with Reduced Formulas

RadioButtons with Reduced Formulas

by Brittni Lorton -
Number of replies: 7

I have a problem that I am working on that uses RadioButtons. What I am looking for is a way to have the 5 options in my RadioButtons be reduced formulas with simplified randomized fractions. I have something that is currently sort of working, but it has an issue that it isn't simplifying all the formulas the same. For some reason, with $m=-1 or -2 then the formula displays with an extra set of parenthesis as y=(-1)x+b. Below is the current code and a picture of the issue I'm seeing. Any ideas? I have tried many different ways to create such a multiple choice question but I am not having much luck, this has been teh closest luck I've had but I'd be open to any suggestions for a way to code such a problem. Thanks!






















$mt = list_random(2,-2,1,-1);

$mb = list_random(1,2,3);

$m = Fraction("$mt/$mb");

$b = non_zero_random(-4,4,1);



$f1 = Formula("y=$m*x+$b")->reduce;

$f2 = Formula("y=$m*x-$b")->reduce;

$f3 = Formula("y=4*x+$m")->reduce;

$f4 = Formula("y=x+5")->reduce;

$f5 = Formula("y=$m*x+$m")->reduce;

$gr = init_graph(-6,-6,6,6,





$gr -> lb('reset'); # remove default labels

# axes labels

$gr->lb( new Label(5.5,0,'x', 'black', 'center', 'bottom','large'));

$gr->lb( new Label(0.1,5.5,"y", 'black', 'left', 'middle'));

# axes labels

foreach my $i (-10..10) {

  $gr->lb( new Label($i,0, $i, 'black', 'center', 'top', 'giant'));

  $gr->lb( new Label(-0.1,$i, $i, 'black', 'right', 'middle'));


add_functions($gr, "$m*x+$b for x in <-10,10> using color:blue and weight:3");

# Set up possible answers

$showPartialCorrectAnswers = 0;

$mc = RadioButtons(

    [ [" `$f1`", " `$f2` ", " `$f3` ", "`$f4` ", " `$f5` "] ], 1 );


What is the equation for the function shown?

>>[@ image(insertGraph($gr), width=>300, height=>300), @]*<<




\{ $mc->buttons() \}


$showPartialCorrectAnswers = 0;

ANS( $mc->cmp() );


Attachment radio.PNG
In reply to Brittni Lorton

Re: RadioButtons with Reduced Formulas

by Danny Glin -

I'm not seeing the same behaviour.  On my WW 2.17 server if $m is -1 I don't get parentheses, but if $m is a fraction I do.

Attachment Screenshot 2023-02-03 at 1.31.54 PM.png
In reply to Brittni Lorton

Re: RadioButtons with Reduced Formulas

by Glenn Rice -

When I test your code I don't see the parentheses around negative one either.  I see the parentheses on fractions as Danny sees, but I see more parentheses around the entire formula.  The reason for the additional parentheses is because MathObject formulas add those extra parentheses when converted to a string.  You are parsing the formulas using the backticks with MathJax, which doesn't remove those parentheses.  I recommend instead using the MathObject TeX result and passing it to MathJax via "\( ... \)" instead.

I have attached an example that uses this approach.  For fun I also converted your PGgraphmacros.pl graph to a PGtikz.pl graph.  I also would like to see more using that than the ugly result of PGgraphmacros.pl and GD.

In reply to Glenn Rice

Re: RadioButtons with Reduced Formulas

by Brittni Lorton -
Thanks for sharing, Glenn! That approach is still having the parenthesis around the negatives and the fractions now, so adds in more display issues than before. I guess it is because I am running 2.15?
In reply to Brittni Lorton

Re: RadioButtons with Reduced Formulas

by Alex Jordan -

It looks like your version of WeBWorK at CCD is 2.15. Is that right? If so, that will affect the issues here and how you could address them. IIRC some specific commits were made not that long ago (1 to 2 years?) that affect parentheses in Fraction objects and Formula objects in Fraction context. To the degree there is a bug, it may be fixed by version 2.17.

Note that on 2.15, Glenn's tikz will not work. I believe that support was introduced in 2.16.

In reply to Alex Jordan

Re: RadioButtons with Reduced Formulas

by Glenn Rice -
Good point Alex. I believe that you are correct. Now that I look at the screen shot I see that the points and problem source are in the problem itself, and that was changed in 2.16 as well. So the tikz will not work.

However, the outer parentheses that I saw will still be resolved by using TeX instead of the string. I am not sure why you are not seeing those.  That should be the same for 2.15.
In reply to Alex Jordan

Re: RadioButtons with Reduced Formulas

by Brittni Lorton -

We are on 2.15, you are right. 

So I suppose there is no fix for now then and I guess I should just wait until we upgrade to 2.17? I have tried the methods here and still no fix, such a strange an annoying display issue, but I guess I will just have to live with it.


In reply to Brittni Lorton

Re: RadioButtons with Reduced Formulas

by Alex Jordan -

If nothing else is working, you could resort to constructing the formula in a more tedious way where you print it piece by piece instead of using one single MathObject formula. Since you know you are dealing with:

y = ±m x ± b

where m may or may not be a fraction, may or may not be 1, it wouldn't be too hard to do that. It would make the problem code less easy to read, and might take significant time if you have many problem files like this.