This question follows on from the answer given by Michael Pilat in Preventing “Plus” from rearranging things. There he defined a custom +
notation using
Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]]
The problem with this is you can't copy and paste the output (although % or Out[] still works). To get around this you should use the Interpretation
type facility which allows an expression to be displayed as one thing, but interpreted as another when supplied as input. My modification of Michael's answer is
Format[myPlus[expr__]] := Interpretation[Row[{expr}, "+"], myPlus[expr]]
This can be copied and pasted successfully. The problem lies in modifying copied expressions. You can convert a copied expression back to InputForm
using Ctrl-Shift-I
then change anything you want and use the InputForm
in any expression. But if you try to change it back to StandardForm
using Ctrl-Shift-N
then you enter an recursion where the second argument in the Interpretation
repeatedly gets evaluated. This is despite Interpretation
having the attribute HoldAll
(which works properly during normal evaluation).
Normally, when defining simple notations I use the low-level MakeBoxes
, eg
myPlus/:MakeBoxes[myPlus[expr__],fmt_]:=With[{r=Riffle[MakeBoxes/@{expr},"+"]},
InterpretationBox[RowBox[r],myPlus[expr]]]
which works perfectly, so I have not encountered this recursion problem before.
So my question (finally) is:
What went wrong with my Format
type command and how can it by fixed?
Or: How do you make a high-level equivalent of my MakeBoxes
type command?