4
$\begingroup$

Let's say I define

Lg[n_]:=Log[2,n].

How do I make

Inactive[Lg][x]//TraditionalForm

output

lg(x)
$\endgroup$
5
$\begingroup$

From GeneralUtilities`PrintDefinitions @ Inactive one can gather that it is NumericFunction attribute which enables () in TraditionalForm, as opposed to [].

So if you don't mind we can set them. And one thing that is left is to change Lg to lowercase during typesetting:

ClearAll[Lg];

Lg[n_] := Log[2, n];

SetAttributes[Lg, NumericFunction];
Lg /: MakeBoxes[Lg, TraditionalForm] := "lg"

Inactive[Lg][x] // TraditionalForm

enter image description here

$\endgroup$
5
$\begingroup$

In general I would definitely prefer @Kuba's solution, but in case you can't add the NumericFunction attribute for some reason and you still want (…) instead of […], or if you want more control in general, you can do something like the following:

Unprotect@Inactive
Inactive /: MakeBoxes[Inactive[Lg][n_], TraditionalForm] := 
 RowBox@{"ln", "(", MakeBoxes[n, TraditionalForm], ")"}
Protect@Inactive

Of course, this simple formatting rule does not show the "Inactive[…]" tooltip that is normally added (if you want it, just adapt the above rule)

Important: Be sure to evaluate this before anything else relating to Inactive, otherwise it won't work.

Why does it not work otherwise?

The issue is that Inactive expressions are formatted via upvalues (or apparently FormatValues to be more precise) of Inactive. This means that you cannot attach the rule to MakeBoxes or Format as you would normally, as these are applied too late in the process. And to make sure that your definition is evaluated before any of the other definitions, you have to make your definition before any of the others are loaded.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.