3

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?

QuantumDot
  • 19,601
  • 7
  • 45
  • 121

2 Answers2

4

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.

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

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.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355