18
$\begingroup$

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

Needs["Notation`"];
AddInputAlias["4" -> ParsedBoxWrapper["↔"]];
InfixNotation[ParsedBoxWrapper["↔"], FlatJoin];

enter image description here

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

enter image description here

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:

Notation`Private`internalCharacterInformation["⋗"] = {"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

and adding something like

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

and then using the Notations package...

$\endgroup$
3
  • $\begingroup$ Pardon me, but how should the operator be highlighted? Does your internalCharacterInformation method fix highlighting to your satisfaction? $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 7:29
  • $\begingroup$ 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. $\endgroup$
    – M.R.
    Commented Jun 3, 2012 at 7:43
  • $\begingroup$ 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. $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 8:04

3 Answers 3

25
$\begingroup$

You can get the syntax highlighting that you desire by modifying your UnicodeCharacters.tr file (path given by System`Dump`unicodeCharactersTR), though I don't know how advisable this practice is.

For example, adding:

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

I can use EscpennyEsc to enter:

Mathematica graphics


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)

  8. right whitespace padding


Completing the operator

Additional code is required to turn such a character into a valid operator. Please see these additional questions for the rest of the story:

$\endgroup$
4
  • 1
    $\begingroup$ But what exactly do these columns mean? $\endgroup$
    – M.R.
    Commented Jun 3, 2012 at 8:16
  • $\begingroup$ @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). $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 8:27
  • 1
    $\begingroup$ You think that column 6 lists the same "grouping (associativity) specifications" as written under More information of e.g. Infix? $\endgroup$ Commented Jun 3, 2012 at 11:07
  • $\begingroup$ @István yes I believe so but I haven't tested it yet. $\endgroup$
    – Mr.Wizard
    Commented Jun 3, 2012 at 11:54
12
$\begingroup$

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.

$\endgroup$
1
  • 1
    $\begingroup$ Exactly, so how can gone introduce a new character and tell Mathematica that it is supposed to be a binary operator? $\endgroup$
    – M.R.
    Commented Jun 3, 2012 at 7:05
4
$\begingroup$

You can fix the syntax coloring by using a TagBox to customize AutoStyleOptions (the syntax colorer thinks the operator is an undefined symbol):

AddInputAlias["4" -> ParsedBoxWrapper @ TagBox[
    "⟗",
    FlatJoin,
    BaseStyle->{AutoStyleOptions->{System`UndefinedSymbolStyle->{FontColor->GrayLevel[0]}}},
    SyntaxForm->"↔"
    ]
]

InfixNotation[
    ParsedBoxWrapper @ TagBox[
        "⟗",
        FlatJoin,
        BaseStyle->{AutoStyleOptions->{System`UndefinedSymbolStyle->{FontColor->GrayLevel[0]}}},
        SyntaxForm->"↔"
    ],
    FlatJoin
]

Note that this also fixes the error messages you get when defining the notation. Here is a screen shot showing that the operator turns black as desired:

enter image description here

$\endgroup$

Your Answer

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

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