3
$\begingroup$

I would like to write a TraditionalForm output for a string of non-commutatively multiplied objects.

I don't really like the default output of the built-in symbol NonCommutativeMultiply, because it inserts $*\!*$ between each pair of symbols.

NonCommutativeMultiply[a, c + d, c] // TraditionalForm

$a*\!*\,(c+d)*\!*\,c$

If I would like to format OperatorProduct[a,(c+d),c] // TraditionalForm like $a\,(c+d)\,c$, how would I go about doing that?

$\endgroup$

2 Answers 2

4
$\begingroup$

You can try using Format along with Inactive

First, need to Unprotect NonCommutativeMultiply:

Unprotect[NonCommutativeMultiply];
Format[NonCommutativeMultiply[x__], TraditionalForm] := Inactive[Times][x]

This will look like:

NonCommutativeMultiply[a, c + d, c] // TraditionalForm

$a*(c+d)*c$

which is not quite right yet. For the finishing touches we get some help from Mr. Wizard

MakeBoxes[p : Inactive[h_][args___], form_] := 
 MakeBoxes[Interpretation[HoldForm@h[args], p], form]

Now the result is

NonCommutativeMultiply[a, c + d, c] // TraditionalForm

$a(b+d)c$

as desired. Beware of course that if you try to interpret this an input it will not be noncommutative.

$\endgroup$
2
  • 1
    $\begingroup$ Very nice. I managed to consolidate your answer into a one-liner: Format[NonCommutativeMultiply[x__], TraditionalForm] := HoldForm@Times[x] (after unprotecting) $\endgroup$
    – QuantumDot
    Aug 11, 2015 at 17:05
  • $\begingroup$ Of course, I need to quit forgetting about the usefulness of HoldForm. $\endgroup$
    – chuy
    Aug 11, 2015 at 17:36
0
$\begingroup$

I would use a TemplateBox to do this (I also included a tooltip to be able to distinguish it from normal multiplication):

Unprotect[NonCommutativeMultiply];
MakeBoxes[NonCommutativeMultiply[a__], TraditionalForm] ^:= TemplateBox[
    Thread @ Unevaluated @ Parenthesize[{a},TraditionalForm,Times,None],
    "NonCommutativeMultiply",
    DisplayFunction -> (RowBox[{SlotSequence[1]}]&),
    Tooltip -> "**"
]
Protect[NonCommutativeMultiply];

Using a TemplateBox means the output is copy/pastable. For instance:

NonCommutativeMultiply[a, b + c, d] // TraditionalForm

a(b+c)d

Copying the above output and evaluating:

enter image description here

a ** (b + c) ** d

gives the usual StandardForm output for NonCommutativeMultiply.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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