WeBWorK Problems

Creating a Fraction of two answer boxes in PGML

Creating a Fraction of two answer boxes in PGML

by roo biki -
Number of replies: 9

I'm having students simplify algebraic expressions, and often the correct answer will take the form of a fraction.  To ensure they've simplified things correctly, I'll need to parse their numerator and denominator separately.

In order to make this as natural as possible for the students, I'd like to have their answer boxes look like this:

The following code definitely does not work at all, but I think it helps to give a picture of what I'm hoping accomplish.  Let's pretend I want the student to enter the fraction of 5/6, but with the 5 and the 6 separately into the numerator and denominator boxes.

DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "PGML.pl"
);

TEXT(beginproblem());

Context("Numeric");

$num = 5;
$den = 6;

BEGIN_PGML

Answer: [`\dfrac{[______]{$num}}{[______]{$den}}`]

END_PGML

ENDDOCUMENT();

How can I adapt this to work as intended?

In reply to roo biki

Re: Creating a Fraction of two answer boxes in PGML

by Glenn Rice -
You should see https://openwebwork.github.io/pg-docs/sample-problems/Algebra/AlgebraicFractionAnswer.html for an example of one way to do this.
In reply to Glenn Rice

Re: Creating a Fraction of two answer boxes in PGML

by roo biki -

Thanks for the reference!  I tried to give this a go, but I'm running into an issue with formatting.  In particular, whenever I build a table using either DataTable or LayoutTable, it automatically gets placed on a new line, and I couldn't find an option to prevent this.

Here's the closest I could get to what I'm looking for:

DOCUMENT();

loadMacros(
  'PGstandard.pl', 
  'PGML.pl',
  'niceTables.pl',
);


$frac = DataTable(
            [ [ [ ans_rule(4), midrule => 1 ] ], [ ans_rule(4) ] ],
            center => 0
        );

BEGIN_PGML

Enter the fraction [`\dfrac{5}{6}`] = [$frac]*

END_PGML

ENDDOCUMENT();

This generates what is shown below:


If I use LayoutTable (as shown in the referenced example) the only difference is that the answer boxes are further away from the horizontal rule that forms the fraction.

Note: Before tinkering, I did try importing the referenced example unchanged, this is what I ended up with:


But I'm looking to have the fraction inline, as shown below (taken from Library/Mizzou/Algebra/radicals_rationalizing/denominator_01.pg).  They use the AnswerFormatHelp.pl macro, which is apparently deprecated:


Can anyone suggest a way to achieve what I need using the niceTables.pl macro?  Much thanks for your attention!

In reply to roo biki

Re: Creating a Fraction of two answer boxes in PGML

by Glenn Rice -
Yeah, I should have checked the version of PG that you are using. You are most likely not using 2.18. The sample problem that I linked will only work correctly with 2.18. For previous versions you should use https://webwork.maa.org/wiki/AlgebraicFractions, although you should adapt that to PGML.

Yes, AnswerFormatHelp.pl is deprecated. Use helpLink instead. You call it the same way and with the same arguments, but don't load the AnswerFormatHelp.pl macro. It is in PGbasicmacros.pl which is included by PGstandard.pl (which you should be loading in your problem in any case).
In reply to Glenn Rice

Re: Creating a Fraction of two answer boxes in PGML

by roo biki -

Thanks Glenn, this version worked.  I checked and I am using 2.16.  Hopefully we can upgrade to the latest version in the future.

I think I've almost got what I need, but I'm having trouble adapting to PGML.

In particular, these two segments:

ColumnTable(
  "\( \displaystyle $fraction = \)",
  ans_rule(2).$BR.$HR.ans_rule(2),
  indent => 0, separation => 10, valign => "MIDDLE"
  );
  
BEGIN_TEXT

$showfraction

END_TEXT

The latter part I'm pretty sure just needs to change to

BEGIN_PGML
[`[$showfraction]`]
END_PGML

But I'm not sure what to do with the line break and horizontal rule in the construction of the column table.  Based on PGML syntax, it seems that I would replace them with the strings "__" and "----", respectively, but that didn't work as I had hoped.


In reply to roo biki

Re: Creating a Fraction of two answer boxes in PGML

by roo biki -
I think I figured it out. It seems I can leave the line breaks and horizontal rules in the construction of the table as $BR and $HR, respectively.

But then in the rendering of the problem, I need to enter

BEGIN_PGML

[@$showfraction@]*
END_PGML

Is this the right way to go about this?

In reply to roo biki

Re: Creating a Fraction of two answer boxes in PGML

by Peter Staab -
You can just do

[$showFraction]*

to do this. You don't need the [@ @]. That runs perl code, wherever the above one just looks up the value of $showFraction.
In reply to Peter Staab

Re: Creating a Fraction of two answer boxes in PGML

by roo biki -

Thanks very much for pointing this out Peter -- I have made this change.

There are two last things about the following segment of the referenced example that I'm struggling to follow:

if ($displayMode eq 'TeX') {
  $showfraction =
  "\[ $fraction = ".$multians->ans_rule(10).$multians->ans_rule(10)." \]";
} else {
  $showfraction =
  ColumnTable(
  "\( \displaystyle $fraction = \)",
  $multians->ans_rule(20).$BR.$HR.$multians->ans_rule(20),
  indent => 0, separation => 10, valign => "MIDDLE"
  );
}

1) Under what circumstances would $displayMode be equal to 'TeX'?  From my playing around it seems that only the else block of code ever actually fires.

2) Assuming that $displayMode eq 'TeX' evaluates to True, then instead of setting up the table, the it looks like two ans_rule's are concatenated directly with the string $fraction.  How does this result in a fraction?

In reply to roo biki

Re: Creating a Fraction of two answer boxes in PGML

by Glenn Rice -

$displayMode is 'TeX' when a hardcopy is generated.

The old example that was referenced does a poor job with the 'TeX' $displayMode.  It should use

'\frac{' . $multians->ans_rule(10) . '}{' . $multians->ans_rule(10) . '}'

That will work for any version of PG.