WeBWorK Problems

Getting dollar signs to show in TeX

Getting dollar signs to show in TeX

by Robin Cruz -
Number of replies: 7

I am updating problems that I wrote last year.  I have a problem template that has an array in it which worked last year, i.e. the dollar signs showed up in the array:

------------Code that worked last year-------------------------

\[ \begin{array}{c|c|c}
            & \mbox{Marginal} & \cr
\mbox{Fixed Cost} & \mbox{Cost per item} & \mbox{Item Sells For} \cr
\hline
$DOLLAR$fix & $DOLLAR$m & $DOLLAR$price \cr
\end{array} \]

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

The values are displayed with a "36;" in place of the dollar sign, 36;2175, where $fix = 2175.  If I make the values Currency objects then the values show up as: 2,175.00 with commas and the period but no dollar sign.  This much shows up in \[ $f \] but nothing shows up if I use \( $f \) where $f = Currency($fix).

I can probably do this in a table, but I didn't want the lines of a table to show up.  This code worked last year.  How do I make it work now?  

Another question:  I am using the $DOLLAR command because I want the money to show up in the form, $ddd and not $ddd.cc.  Is there a way in Currency format to not have the cents show if there are no cents ? i.e. show $25 and not $25.00, but if the value is $5.40 then require the student to enter the correct currency format?

Another note:  Then a Currency object is the correct answer and a hardcopy with "Correct answers" is made, the values show up as "\$2,459", which is the TeX format, but it would be nicer if it showed up without the backslash.

The pertinent code is pasted below if that helps.

Thanks -- rac

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

DOCUMENT();        # This should be the first executable line in the problem.

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",

  "contextCurrency.pl"  #This is used later in the answer section
);

TEXT(&beginproblem);

######################################
#  Setup

$fix = random(15000,20000,1000);
$m = random(12,30,1);
$price = $m + random(5,50,5);

######################################
#  Main text

BEGIN_TEXT
Assume each function is linear and use the information given below to
find:
$BR
\[ \begin{array}{c|c|c}
            & \mbox{Marginal} & \cr
\mbox{Fixed Cost} & \mbox{Cost per item} & \mbox{Item Sells For} \cr
\hline
$DOLLAR$fix & $DOLLAR$m & $DOLLAR$price \cr
\end{array} \]
 
END_TEXT

ENDDOCUMENT();      

In reply to Robin Cruz

Re: Getting dollar signs to show in TeX

by Robin Cruz -
I just came across another problem while using contextCurrency.pl.
Here's the code (not much to it):
-----------------------------------------------
Context("Currency");
$ans_a = Compute($b/200);
ANS($ans_a->cmp);
------------------------------------------------
It seems to work fine except if $b/200 is a value like 0.275, i.e. the
thousandths digit is a 5. In this case, neither $0.27 nor $0.28 are
considered the correct answer.

Thanks --rac
In reply to Robin Cruz

Re: Getting dollar signs to show in TeX

by Davide Cervone -
There are a couple of things going on, here. First, you should probably use $ans_a=Currency($b/200) in order to make the answer an actual currency value rather than a plan Real. (The student can still enter a currency value, but will not be required to, and the correct answer will show as .275 not the currency value.)

The second is that the tolerances for checking currencies are set to absolute with tolerance equal to .005 so that the answers will be checked to the nearest cent. This is designed to work for values that only have two digits. This will even work for values with more decimals, except for the one case of exactly .005 (since the tolerance is a strict less than, not less-than-or-equal-to).

There are several solutions to this: first, you could specify a slightly larger tolerance, using

    $ans_a = Currency($b/200)->with(tolerance=>.00500001);
for example. But this will allow both $.28 and $.27 to be considered correct.

Alterntively, you could force the answer to be a 2-digit one

    $ans_a = Currency(spf($b/200,"%.2f"));
where spf(number,format) formats the number using the sprintf-style format string. In this case, %.2f means to use a real number (the "f") that has 2 decimal digits (the ".2"). Perhaps something like this should be added to the creation of currency objects. I'll think about the right way to do it.

Davide

In reply to Robin Cruz

Re: Getting dollar signs to show in TeX

by Davide Cervone -
You have quite a few questions, so let me do them one at a time.

The values are displayed with a "36;" in place of the dollar sign, 36;2175, where $fix = 2175.

This is because $DOLLAR is not meant to be used in math mode; it doesn't produce the correct values for that. It produces a \$ in hardcopy mode, and &36; in HTML mode (which is the HTML way of specifying a character by its numeric value). So when you do your table, it inserts &36; and TeX reads the & as a column separator, leaving the 36; as part of the text within the column.

You could set $DOLLAR = '\$'; earlier in the problem, and that would do it (if you don't use $DOLLAR outside of math mode elsewhere in the problem). This is one of the limitations of the current approach to macros and text, because they can't tell if they are in math mode or plain text, and so can't change what they display based on that. (It is the reason why Context()->texStrings and Context()->normalStrings are necessary). I have some ideas about how to overcome the problem, but nothing that will be usable soon.

If I make the values Currency objects then the values show up as: 2,175.00 with commas and the period but no dollar sign. This much shows up in \[ $f \] but nothing shows up if I use \( $f \) where $f = Currency($fix).

This is probably because you have not turned on TeX strings with Context()->texStrings. Without this, the Currency object will produce a plain-text string, and that includes an unquoted dollar sign. In display math, this will produce an error that will be ignored silently, and in in-line math, it will end the mathematics, which is why you see no dollar in the first one and nothing at all in the second one. If you use Context()->texStrings before the BEGIN_TEXT where you are displaying these values, it should work out (both in your table and in the separate math items you indicated).

I am using the $DOLLAR command because I want the money to show up in the form, $ddd and not $ddd.cc. Is there a way in Currency format to not have the cents show if there are no cents ?

Not originally, but I just added one. You can select this mode by using

    Context()->flags->set(trimTrailingZeros=>1);
to have this happen globally, or for only a specific value, use
    $m = Compute("$50")->with(trimTrailingZeros=>1);
to get $50 rather than $50.00 as the output.

When a Currency object is the correct answer and a hardcopy with "Correct answers" is made, the values show up as "\$2,459", which is the TeX format, but it would be nicer if it showed up without the backslash.
Yes, this is what happens. This is because in hardcopy mode, the Currency values usually need to quote the dollar sign so that when they are used in plain text, the dollar sign does not accidentally initial math mode, causing errors in producing the hardcopy. The object doesn't know if its value is going to be used for the text of the correct answer, or within the main text of a problem, or within math mode, each of which need a different kind of format. So it does what it thinks is the best thing. It is clearly a compromise, but I don't see how to do better with WeBWorK's current architecture. Davide
In reply to Davide Cervone

Re: Getting dollar signs to show in TeX

by Robin Cruz -

OK.  I'm good with everything so far.  The problems I've been writing look very nice with the Currency context.

I ran into another difficulty today.  Currency objects are not working with the multiple choice questions -- at least not the way I've been doing them.  Also note that in a multiple choice question the $DOLLAR symbol gives error message when a hardcopy is requested and the answers with the $DOLLAR symbol are not printed in the hardcopy.

Perhaps there is a better way to do the multiple choice question?  I've used multiple choice questions before, but I guess never with money:

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

$mc = new_multiple_choice();
     $mc->qa("A company received a $DOLLAR$A, six-month, $r$PERCENT interest
             bearing note from a customer.  The note was discounted
             the same day by a bank at $d$PERCENT.  The
             amount of cash received by company from the bank was",
 "$DOLLAR$cash");
     $mc->extra("$DOLLAR$n1",
 "$DOLLAR$n2",
        "$DOLLAR$n3");
--------------------------------------------------------

I first tried this with $A, $cash, $n1, $n2 and $n3 being Currency objects, but what shows up is  ,500 instead of $10,500 on the screen.

I appreciate your help on this -- rac

In reply to Robin Cruz

Re: Getting dollar signs to show in TeX

by Robert Byerly -
Hi Rachel,

Interesting timing on this one. One of our professors who was writing some problems ran into what sounds like the same difficulty just two days ago. He was not using currency objects, but otherwise the problem was similar.

As it turned out, he was trying to print the question as follows:

BEGIN_TEXT
\{$mc->print_q\}
$PAR
\{$mc->print_a\}
END_TEXT
The problems disappeared when we changed this to

TEXT($mc->print_q, "$PAR", $mc->print_a);
Apparently the extra processing being done on material in a BEGIN_TEXT/END_TEXT block (by EV3?) is causing the problem. I think I know what's going on, but I'm sure one of the experts can give a more coherent explanation than I can.

Bob Byerly
In reply to Robert Byerly

Re: Getting dollar signs to show in TeX

by Robin Cruz -

Your fix also works with Currency objects. 

Thanks, Bob. 

--Robin A Cruz

In reply to Robert Byerly

Re: Getting dollar signs to show in TeX

by Davide Cervone -
Apparently the extra processing being done on material in a BEGIN_TEXT/END_TEXT block (by EV3?) is causing the problem.

Bob is right, it is EV3 that is doing the damage. It lies behind the scenes for BEGIN_TEXT/END_TEXT, and it does three things: it processes commands between \{...}, it does variable substitution for $varname and it does math processing for \(...\) and \[...\].

The three jobs are done in the order listed above. The commands are done first so that variables within the commands are not accidentally replaced before they are executed, and variable substitution is done next so that mathematics contained in the variables (or in the output of commands) will be processed properly.

But that means that dollar signs that appear in the output of a command will have have variable substitution performed on it. That is what is happening with the output of your list. (That is why you saw "$10,000" turn into ",100", since the $10 is viewed as a variable with name 10 (which was empty, so substituted as nothing).

The TEXT() call that Bob suggested does not process the text, but simply outputs it, so the variable substitution doesn't occur.

Another approach would be to save the output in a variable and use that within the BEGIN_TEXT/END_TEXT instead of command execution (since it the result of variable substitution do not have additional variable substitutions performed on them). For example,

    $questions = $mc->print_q;
    $answers = $mc->print_a;
    BEGIN_TEXT
    $questions
    $PAR
    $answers
    END_TEXT

Hope that clears up some of it.

Davide