WeBWorK Problems

MathObjects: TeXing Functions

MathObjects: TeXing Functions

by Robin Cruz -
Number of replies: 2

I tried to get a function to show up in it's TeX form as follows:

$a = random(1,9,1);
$b = non_zero_random(-9,9,1);
$f = Formula("$a + $b x");
$x = random(1,9,1);
Evaluate the algebraic expression for the given value(s):

\( {$f->TeX} \), for \( x = $x \)
Answer:  \{ ans_rule(20) \}

This is what shows up: (2+(-5)*x)

If I change the context to texStrings before BEGIN_TEXT, then back to normalStrings after END_TEXT and remove the "->TeX", then the formula shows up nicely.  I thought we could do this without changing the context?


In reply to Robin Cruz

Re: MathObjects: TeXing Functions

by Davide Cervone -
You can do it without setting the context, but you need to use \{ ... \} not just {...} inside the math mode. This causes the $f->TeX to be performed as a command and the result of it to be put in math mode. The way you currently have it, $f is replaced by its string version, so it is the same as \( { (2+(-5)*x)->TeX } \); the braces are part of the math formula, and so is the ->TeX!

You may also want to use $f = Formula("$a + $b x")->reduce; in order to avoid the "plus a negative" and coefficients of 1, and so on.