10
$\begingroup$

Notation package looks poorly documented.

For example, GridBoxDividers option is not documented at all.

Can someone give me a simplistic example of a Notation set up? For example, suppose I wish to represent

MyHead[a,b]

as

$\frac{a}{b}$

with

GridBox[{{"a"}, {"b"}}, GridBoxDividers -> {"Rows" -> {{False, True}}}]

If I enter

Notation[GridBox[{{ToString[x_]}, {ToString[y_]}}, 
   GridBoxDividers -> {"Rows" -> {{False, True}}}]
\[DoubleLongLeftRightArrow]
MyHead[x_, y_]]   

I get an error

Notation::noexbtag: The external representation GridBox[{{x_},{y_}},GridBoxDividers->{Rows->{{False,True}}}] does not have an embedded TemplateBox with tag NotationTemplateTag. The Notation statement Notation[GridBox[{{x_},{y_}},GridBoxDividers->{Rows->{{<<2>>}}}][DoubleLongLeftRightArrow]MyHead[x_,y_]] may not have been entered using the palette, or the embedded TemplateBox may have been deleted. The embedded TemplateBox ensures correct parsing and retention of proper styling and grouping information. >>

$\endgroup$

1 Answer 1

19
$\begingroup$

Some people make good use of the Notation package but I have never been very successful with it. A good reason to learn a little about MakeBoxes and TemplateBox.

The following MakeBoxes definition will format your expression.

MyHead /: 
 MakeBoxes[MyHead[a_, b_], 
  form : (StandardForm | TraditionalForm) : StandardForm] :=
 InterpretationBox[#1, #2] & @@ {GridBox[{{MakeBoxes[a, 
       form]}, {MakeBoxes[b, form]}}, 
    GridBoxDividers -> {False, {{False, True}}}], MyHead[a, b]}

The InterpretationBox's arguments are held so using the trick of making it a pure function and applying it to a List allows you compute the format if necessary. The list could be generated by an extended Module calculation. The first item in the list is the formatted expression and the second is the internal expression, the same as the initial expression. The following should then work.

MyHead[a, b]

Mathematica graphics

MyHead[5 x + 3, 2 x - 1]

Mathematica graphics

One disadvantage of this is that the output can be copied and pasted but not edited.

An alternative approach uses TemplateBox, which is a documented function in Version 9.

Clear[MyHead];
MyHead /: 
 MakeBoxes[MyHead[a_, b_], 
  form : (StandardForm | TraditionalForm) : StandardForm] :=
 Module[{dispFunc},
  dispFunc = 
   GridBox[{{#1}, {#2}}, 
     GridBoxDividers -> {False, {{False, True}}}] &;
  TemplateBox[{MakeBoxes[a, form], MakeBoxes[b, form]}, "MyHead",
   DisplayFunction -> dispFunc,
   InterpretationFunction -> (RowBox[{"MyHead", "[", #1, ",", #2, 
        "]"}] &)]
  ]

With TemplateBox you write a pure function for the DisplayFunction and for the InterpretationFunction. The first item in Template generates the items that will fill the slots. The resulting output can be copied, pasted and the items that fill the slots can be edited - but not other parts of the formatted expression.

This approach has normal input and formatted output. Formatted input is not always that convenient because it can be a chore to manuever between the various input boxes. If you want formatted input then you need to use MakeExpressions and probably put the form on a palette to paste in your notebook. I don't use MakeExpressions much so I'm going to skip giving an example for that.

$\endgroup$
3
  • $\begingroup$ Good stuff, big +1. Great that you are here, David, we missed you and your expertise! $\endgroup$ Jan 25, 2013 at 20:03
  • $\begingroup$ Thanks Leonid, and I have to add that your expertise is just where I am weak so glad to see you and also the other people here. It's taken me a while to learn how to use StackExchange. $\endgroup$
    – David Park
    Jan 25, 2013 at 20:47
  • $\begingroup$ Thanks, David. A side note: if you wish to notify some user of your reply (this refers to comments), you may want to add the @ sign in front of the user name, for that user to be pinged when you reply to a comment, for example, @Leonid. Otherwise they won't know that you replied (I didn't). Also, please do use chat in case you have any questions regarding SE and / or how to use it - there will be plenty of people willing to help you out with whatever issues you may have. $\endgroup$ Jan 28, 2013 at 10:53

Your Answer

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

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