WeBWorK Problems

display of Fraction objects

display of Fraction objects

by Dick Lane -
Number of replies: 3
I would like to have a bit more control for display of Fraction objects.  At the moment, even with \[ ... \] or \(\displaystyle ... \), a fraction object is displayed in small format because the object's TeX version has the form "{\textstyle\frac{top}{bot}}".

Perhaps a simple revision of line 817 in .../pg/macros/contextFraction.pl would be sufficient to change this.

I think producing merely "\frac..." would suffice because I could still choose between using \( $ratio \) for small format or \(\displaystyle $ratio \) or \[ $ratio \] to get large fractions.

Note: this is separate from (and probably independent of) choosing whether to show negative two divided by three as "\frac{-2}{3}" or "- \, \frac{2}{3}".
In reply to Dick Lane

Re: display of Fraction objects

by Dick Lane -
Perhaps it would be sufficient to have operators to let me extract numerator and denominator from a Fraction object.  That would let me control some of the formatting.  For instance, if a fraction appears as a bound for a definite integral, I prefer to use "a/b" instead of "\frac{a}{b}".  On the other hand, I would then need to handle the denominator=1 case.  (Does Knuth describe a polymorphic fraction object in the TeX book?)
In reply to Dick Lane

Re: display of Fraction objects

by Davide Cervone -
If $f is a Fraction object, you can use
    $f->with(isHorizontal=>true);
to produce a Fraction object that display as a/b rather than \frac{a}{b} in TeX output. It will handle the denominator 1 case properly.

If you want to get the numerator and denominator separately, use

    ($a,$b) = $f->value;

I think the reason for the \textstyle is because the fractions produce mixed numbers like 4 1/2 for "four and a half" and 4\frac{1}{2} in display mode is not correct for that. If you only used text mode for mixed numbers, then 9/2 would display differently from 1/2, which would be inconsistent.

While it is true that as an author, you could choose how you insert the fraction into the problem, you need to remember that the TeX output is used not only for the problem text, but also for the student answers, which (if I remember correctly) are done in display mode. If the default were to be plain \frac{a}{b}, then all mixed-number student answers would be wrongly formatted. The original motivation for the Fraction context was elementary education problems, where consistency of the format is more critical.

But you are right, there should be more control over the TeX output. There should probably be a parameter to determine the format to use.

Of course, you could subclass the Fraction class and replace its TeX output function. :-)

Davide

In reply to Davide Cervone

Re: display of Fraction objects

by Dick Lane -
Thanks, Davide, for the tips about isHorizontal and ->value.

Is there something similar to handle exponents in horizontal format but with coefficients in stacked format? E.g., I would like antiderivative of sqrt(x) to have TeX form \frac{2}{3} x^{3/2} rather than \frac{2}{3} x^{\frac{3}{2}}. If the exponent is stacked, then baseline of its denominator is very close to the baseline of x.