# How can one define an infix operator with an arbitrary unicode character?

You can setup "UsefulFunction[a, b]" to use custom infix notation "a [LeftRightArrow] b" as follows:

Needs["Notation"];
InfixNotation[ParsedBoxWrapper["\[LeftRightArrow]"], FlatJoin];


But using a unicode character that does not have a mathematica definition (e.g. "\[name]") such as "\:27d7" gives you an error:

Now the syntax highlighting is broken, and that is really my question: how can you tell mathematica to correctly syntax highlight new unicode infix operators?

(Note: the messages can by avoided by adding internal information on the character as follows:

NotationPrivateinternalCharacterInformation["⋗"] = {"0x2295", "Infix", "450", "None", "3", "3", "MyOp"}; InfixNotation[ParsedBoxWrapper["⋗"], FlatJoin]


Edit:

I'm now pretty sure that the answer will involve editing /Applications/Mathematica.app/SystemFiles/FrontEnd/TextResources/UnicodeCharacters.tr

0x22D7      \[FlatJoin]     ($fj$   $&FlatJoin;$   $\oplus$)        Infix       320     None        4       4


and then using the Notations package...

-
Pardon me, but how should the operator be highlighted? Does your internalCharacterInformation method fix highlighting to your satisfaction? –  Mr.Wizard Jun 3 '12 at 7:29
It should be highlighted just like the \[LeftRightArrow] in the first output image: red when used improperly, and black otherwise. Also, no the NotationPrivateinternalCharacterInformation trick only stops InfixNotation from complaining. –  M.R. Jun 3 '12 at 7:43
See my answer below. I shall admit I've never used this kind of modification myself and I don't know what kinds of problems may result. –  Mr.Wizard Jun 3 '12 at 8:04

You can get the syntax highlighting that you desire by modifying your UnicodeCharacters.tr file, though I don't know how advisable this practice is.

0x20B0      \[PennyOp]      ($penny$)  Infix       155     None        5       5


I can use EscpennyEsc to enter:

I am not aware of documentation of the format of this file but as best I can tell the columns are:

1. the Unicode address in hex

2. the FullForm String representation

3. input aliases separated by tabs

4. the use or type of the symbol

5. parsing precedence

6. Left, Right or None -- I assume an associativity control

7. left whitespace padding to place around the character (in StandardForm)

-
But what exactly do these columns mean? –  M.R. Jun 3 '12 at 8:16
@Mike I'm still figuring that out. I believe: the first column is the Unicode address in hex; the second is the FullForm string representation; the third is any input aliases separated by tabs; the fourth is the use or type of the symbol; the fifth is precedence; the sixth is Left, Right or None -- I assume a binding control; the last two columns are left and right whitespace padding to place around the character (in StandardForm). –  Mr.Wizard Jun 3 '12 at 8:27
You think that column 6 lists the same "grouping (associativity) specifications" as written under More information of e.g. Infix? –  István Zachar Jun 3 '12 at 11:07
@István yes I believe so but I haven't tested it yet. –  Mr.Wizard Jun 3 '12 at 11:54

Instead of using the Notation package, you can achieve the translation by doing the following:

MakeExpression[RowBox[{x_, "⟗", y_}], StandardForm] :=
MakeExpression[
RowBox[{"FlatJoin", "[", x, ",", y, "]"}],
StandardForm
]


This takes care of the input translation. Now it's possible to enter expressions like

1 ⟗ (3 + 4 ⟗ 2)


and have Mathematica understand that it means

FlatJoin[1, 3 + FlatJoin[4, 2]]


If you want FlatJoin to remain undefined and just have the symbolic display, you may also want to define an output format:

FlatJoin /: MakeBoxes[FlatJoin[x_, y_], StandardForm] :=
RowBox[{"(", MakeBoxes[x, StandardForm], "⟗",
MakeBoxes[y, StandardForm], ")"}]


But as soon as you provide a definition for FlatJoin, this output format won't really be needed.

Edit

The above gets rid of the error messages reported in the question. To get syntax highlighting for incomplete expressions, Mathematica needs to know that it's dealing with a binary operator. The easiest way to do that is of course to make use of one of the operators that is already pre-defined, see the list in the documentation; you can select any operator that has no built-in meaning, see this doc page.

The reason why syntax highlighting doesn't work for general (unicode or other) atomic characters in the abbreviated infix notation 1 ⟗ 2` is that they are interpreted as multiplications by a variable of that name.

-
Exactly, so how can gone introduce a new character and tell Mathematica that it is supposed to be a binary operator? –  M.R. Jun 3 '12 at 7:05