Search code examples
maxima

How to display a fraction as a mixed number in maxima?


I'm giving a course where students are used to the mixed number notation. However, all the calculations that maxima does, use the more traditional notation of fractions. Is it possible to present 3/2 as 1 1/2. I need this for the latex output only.

(%i4) tex(3/2);
$${{3}\over{2}}$$
(%o4)                                false

So instead of this I would like to get:

(%i4) tex(3/2);
$$1 {{1}\over{2}}$$
(%o4)                                false

Is this possible?


Solution

  • You can assign TeX properties via texput. Rational numbers are represented as ((RAT) mmm nnn) which you can see via :lisp $x where x is a Maxima variable which is a rational number. So, you can set the TeX property by:

    texput (?rat, texrat);
    

    where your function texrat is defined as (for example):

    texrat(x) := block ([i, r], 
                        i:floor(x), 
                        r:x-i, 
                        sconcat ("{", i, "} {{", num(r), "}\\over{", denom(r), "}}"));
    

    Example:

    (%i11) tex(sin(12/7));
    $$\sin \left({1} {{5}\over{7}}\right)$$
    

    Note that the new function is applied to a rational even when it's inside another operator.

    Of course you can change the output of texrat to make it whatever you want.

    Note that the ? before rat is necessary in the call to texput.

    Some of this stuff is undocumented; sorry about that.