I added the code below to the PG problem, which modifies a subroutine in Parser::Legacy::ObjectWithUnits.
It appears to let me make
$p=NumberWithUnits(5,'%/min');
which:
- outputs fine in a problem statement using images
- outputs fine in a problem statement using MathJax
- outputs fine in a problem statement in hardcopy
- outputs fine in the correct answer field in HTML
- outputs fine in the correct answer field in hardcopy
- lets "5 %/min" work as correct input from a student
- lets "0.05 min^-1" work as correct input from a student
I believe I have learned from Davide that I should not be modifying a subroutine from Parser::Legacy::ObjectWithUnits from within a PG problem, because it will have effects that extend beyond this one problem. So I might make the corresponding (similar, not exactly the same) changes to pg/lib/Parser/Legacy/NumberWithUnits.pm. If I change both NumberWithUnits.pm and Units.pm as mentioned in my earlier post, does it provide an all-around valid way to get "%" to count as a unit? If so I might turn this into a pull request, unless it is bad form to be making new edits in a "Legacy" module.
Additionally, I have discovered that a student cannot submit something like "0.05/min" for an answer, even though "0.05 min^-1" is OK. The module doesn't recognize denominator-only units. I might investigate giving that functionality too.
package Parser::Legacy::ObjectWithUnits;
sub TeXunits {
my $units = shift;
$units =~ s/~~^~~(?([-+]?~~d+)~~)?/^{$1}/g;
$units =~ s/~~*/~~~~,/g;
$units =~ s/%/~~~~%/g; # new line
return '{\rm '.$units.'}' unless $units =~ m!^(.*)/(.*)$!;
my $displayMode = $envir{displayMode}; # modified line
return '{\textstyle\frac{'.$1.'}{'.$2.'}}' if ($displayMode eq 'HTML_tth');
return '{\textstyle\frac{\rm\mathstrut '.$1.'}{\rm\mathstrut '.$2.'}}';
}
package main;