Maxima typesetting of sin(x)^2

Viewed 103

How can I tell Maxima to typeset sin(x)^2 as $$\sin(x)^2$$ (or $$\sin\left(x\right)^2$$) rather than $$\sin^2 x$$?

(The accepted answer to this question suggests using

:lisp (setf (get '%sin 'tex) nil) 

to remove the special TeX handler for the sin function. Having done that, we get $$\sin^2\left(x\right)$$, but the position of the superscript is unchanged.)

2 Answers

Some digging in src/mactex.lisp yields the following which seems to work. First remove the TEX and TEX-RBP (right-binding power -- has to do with precedence) properties. Then nuke the function which special-cases trig-like functions for TEX-MEXPT (handles exponents).

(%i1) :lisp (mapcar #'(lambda (x) (setf (get x 'tex) nil) (setf (get x 'tex-rbp) nil)) *tex-mexpt-trig-like-fns*)

(%i1) :lisp (defun maybe-tex-mexpt-trig-like (&rest a) (declare (ignore a)))

Let's see what Maxima thinks is the list of trig-like functions.

(%i1) :lisp *tex-mexpt-trig-like-fns*

(%SIN %COS %TAN %SINH %COSH %TANH %ASIN %ACOS %ATAN %ASINH %ACOSH %ATANH)

OK, let's go through them and see what TeX output we get.

(%i1) for f in [sin, cos, tan, sinh, cosh, tanh, asin, acos, atan, asinh, acosh, atanh]
        do tex(f(x)^2);
$$\sin \left(x\right)^2$$
$$\cos \left(x\right)^2$$
$$\tan \left(x\right)^2$$
$$\sinh \left(x\right)^2$$
$$\cosh \left(x\right)^2$$
$$\tanh \left(x\right)^2$$
$$\arcsin \left(x\right)^2$$
$$\arccos \left(x\right)^2$$
$$\arctan \left(x\right)^2$$
$${\rm asinh}\; \left(x\right)^2$$
$${\rm acosh}\; \left(x\right)^2$$
$${\rm atanh}\; \left(x\right)^2$$
(%o1)                                done

I guess the \; output for asinh etc. is a bit of a wart. We can fix that too if you need it.

The tex output code is an agglomeration of heuristics which work most of the time. As you can see, it's not always obvious how to get a different output.

Here is a slight addendum to Robert Dodier's answer. I am using Maxima in connection with the STACK system for computer-aided assessment (home page, Github). Because STACK modifies the behaviour of Maxima's TeX system, a different approach is needed in that context. One possibility is to delete this clause from the file stacktex.lisp (which has an attached comment suggesting that you could delete it). Having done that, it is still useful to remove the right binding power annotation as in Robert Dodier's answer, otherwise you will get (\sin(x))^2 instead of \sin(x)^2.

Related