WeBWorK Problems

Modify contextCurrency.pl

Modify contextCurrency.pl

by Robin Cruz -
Number of replies: 3

Hi, all,

I am helping a group in Kenya write WeBWorK problems for middle school students.  Here is the issue:

There is a difference in how large numbers are stated in Kenya and the U.S. In Kenya, a large number will be written as: 2 345 011 with spaces instead of commas. The issue is especially bothersome in problems with currency. I can change the currency symbol to "sh" (shillings) but WeBWorK puts in the commas automatically.  I think a custom macro would be nice, so they could load the macro and have it all set.  I've included code for a problem below.  It would also be nice to have something for plain large numbers that would be course wide--not sure how that would work.

Thanks--rac

------------Start code-------------------------------------------

DOCUMENT();  

loadMacros(

"PGstandard.pl",

"PGML.pl",

"contextCurrency.pl"

);

####################

# Set-up

Context("Currency");

Context()->currency->set(symbol=>'sh');

Context()->currency->setSymbol(sh=>{associativity=>"left"});

Context()->flags->set(trimTrailingZeros=>1);

#Context()->currency->set(comma=>' ',decimal=>'.'); 

$a = random(300,800);

$a = Compute("sh $a");

$b = random(1000,10000);

$b = Compute("sh $b");

$sum = $a+$b;

##################################

# Main text

BEGIN_PGML

Add: [`[$a] + [$b] = `] [____]{$sum}{12}

END_PGML

ENDDOCUMENT();

---------------------------------------------------------------------

In reply to Robin Cruz

Re: Modify contextCurrency.pl

by Alex Jordan -
After looking at this, it seems there is a bug with contextCurrency.pl. (I did not look deeply enough to see where the bug is.)

I did find a workaround for the bug, but even then, setting the comma to be a space, the space is not retained in output. So in addition to the bug, I think that the comma specification might be collapsing all whitespace, and something else needs to be done to allow a space as the comma.
In reply to Alex Jordan

Re: Modify contextCurrency.pl

by Robin Cruz -
I had a thought which is more of a hack than just making it take a space for the comma. Could the comma be set to have font color white or transparent?

Thanks for you help--rac
In reply to Robin Cruz

Re: Modify contextCurrency.pl

by Alex Jordan -

There are some bugs with contextCurrency. I've reported them, and I may even try to fix them later.

But below is a version of your problem that should work for what you need. Some notes:

  • One of the bugs seems to have to do with using Context()->currency->set() multiple times. I put all the settings into one call. This was the error you were hitting.
  • The comma is '~~,'. This is a bit of arcane PG. The double tilde will be translated into a single backslash, so that in math output a `\,` which is a thin space. You might want some other space like `\;` or `\ `, and you could adjust accordingly.
  • With the comma set this way to be a LaTeX command, you can't use a currency object outside of math mode or you will see the backslashComma. Usually with a currency object, you can use it inside or outside of math mode and it's OK either way. So this would be something to be aware of.
  • I was reading about Kenyan currency notation, and I wonder if you want the decimal to be a `/` instead? But there is that thing where zero amounts before/after the decimal are written with `-` or `=`, and I don't believe contextCurrency.pl has a way to honor that.
  • The code below uses the Currency() constructor directly instead of Compute(), which has to parse strings with "sh" in them. The reason I made that change is so that if I comment out the line that sets the 'sh', the problem will still work. Just using dollar signs. But I think this might be better for other reasons.

DOCUMENT();  

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

Context("Currency");
Context()->currency->set(symbol => 'sh', comma => '~~,', decimal => '.');
Context()->flags->set(trimTrailingZeros => 1);

$a = Currency(random(300,800));
$b = Currency(random(1000,10000));
$sum = $a+$b;

BEGIN_PGML
Add: [`[$a] + [$b] ={}`] [_]{$sum}{12}
END_PGML

ENDDOCUMENT();