Defining my own invisible operator



There are some invisible operators in Mathematica (\[ImplicitPlus],\[InvisibleTimes],\[InvisibleComma],\[InvisibleApplication]). How can I make my own that would not interfere with built-in functions (Plus,Times,Sequence,Apply)? Is there some invisible character like those different kinds of spaces (\[RawSpace],\[ThinSpace],\[VeryThinSpace], etc.) that won't be interpreted as multiplication, so that I can define my own patterns and behaviour with it?

EDIT: OK, I think this is it:

TemplateBox[{}, "op",
  InterpretationFunction :> (RowBox@{"~op~"} &), 
  DisplayFunction :> (RowBox@{"\[InvisibleSpace]"} &)];
SetOptions[EvaluationNotebook[], InputAliases -> {"op" -> %}]
SetAttributes[op, Flat];

Now if I type 1[ESC]op[ESC]2[ESC]op[ESC]3 it will be interpreted as 1~op~2~op~3 which means op[1,2,3] if op has Flat attribute, but will be displayed as 123.

I wonder why nobody came up with it, what all the gurus doing?

Whatever, now I can finally create pretty EDSLs in Mathematica :D


Posted 2014-02-13T10:38:00.840

Reputation: 6 720

1Related? – Öskå – 2014-02-13T10:40:23.973

@Öskå Yes, basically I need an invisible infix operator of my choice. – swish – 2014-02-13T10:44:40.297

2An operator like that will be endless fun when debugging. – Yves Klett – 2014-02-17T07:03:32.313

2@YvesKlett \[InvisibleBug] ! – Dr. belisarius – 2014-02-18T05:08:40.363

2@belisarius hehe plenty of those already :-) – Yves Klett – 2014-02-18T06:22:11.200

Unfortunately it does not seem possible to overload MakeExpression for this purpose. But I guess that was a foolish idea anyway :P – Jacob Akkerboom – 2014-02-21T15:48:16.943



I can't provide working code because I don't have access to Mathematica now, but you can do this using MakeExpression. You can reverse engineer the required box structure by typing the InfixForm of Times[x,y] in an Input Cell. Select the input cell, and press Shift-Ctrl-E to see the underlying box structure for infix multiplication. You want the same structure, except with the operator you want.

You can probably get examples if you search for MakeExpression, MakeBoxes at MathGroup.

Ted Ersek

Posted 2014-02-13T10:38:00.840

Reputation: 5 036