Re: Re: Symbolize Problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg69754] Re: [mg69682] Re: [mg69678] Symbolize Problem*From*: "Chris Chiasson" <chris at chiasson.name>*Date*: Fri, 22 Sep 2006 01:04:26 -0400 (EDT)*References*: <200609211128.HAA07587@smc.vnet.net> <NDBBJGNHKLMPLILOIPPOKELGFCAA.djmp@earthlink.net>

David Park, That was very informative, thank you. I've heard about this method, but never tried it because I now tend to code using m files. I am hoping you would be inclined to put a version of it on this page of the wiki (it's easy to do): http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Typesetting which is part of the Mathematica wikibook http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Wikibook I started the typesetting page yesterday, but I don't know if the email about it went out over the MathGroup. I'd like to expand it to include the typesetting of cells, so that other people don't have to guess about how to format things, but that will have to come later. Thanks for writing, On 9/21/06, David Park <djmp at earthlink.net> wrote: > Chris, > > I was not able to understand what the original poster wanted to do, so I put > off a response. But your posting stimulated me to respond. > > I would like to present a construction that is very useful and quite easy > for formatting expressions into common textbook forms. (I learned this > method from Ted Ersek who said that he in turn had learned it from Neil > Soiffer of WRI.) > > The construction involves MakeBoxes and InterpretationBox. The trick is that > InterpretationBox has the attribute HoldAllComplete (I'm not certain why) > and so a construction has to be used that allows the specific formatting to > be computed when necessary. This is achieved by writing InterpretationBox as > a pure function and applying it to the list of computed arguments. > > As an example, suppose we want to format MatrixExp so it will format as a > regular exponential when it holds a symbolic argument, that is when it is > unevaluated. We can use the following construction. > > MakeBoxes[MatrixExp[x_], form : StandardForm | TraditionalForm] := > InterpretationBox[#1, #2, SyntaxForm -> "^"] & @@ > {SuperscriptBox["\[ExponentialE]", MakeBoxes[x, form]], MatrixExp[x]} > > Then we can use this as follows: > > MatrixExp[\[Theta]*J] > % /. J -> {{0, -1}, {1, 0}} > > Exp[J x] > {{Cos[x], -Sin[x]}, {Sin[x], Cos[x]}} > > The first argument of IntepretationBox gives the format for the displayed > output and the second argument gives the internal representation. If you > look at FullForm, for example, you will get the internal representation, > MatrixExp[J x] in this case. The output can be copied and pasted. It can be > edited, unless you block it, but the editing will have no effect. > > The SyntaxForm option has the default value of Automatic, which will > probably be good enough in most cases. In any case, I believe it gives the > precedence grouping for the formatted output, but am not entirely clear > about it. > > This is a much better method than using Format, which can't usually be > copied and pasted. Note that the input is still a standard expression > without box format. But using box format for input is not actually very > convenient. You have to have code to bring up a box structure and then you > have to tab around it in. So having standard input with formatted output is > far easier. I find that this method works for all the formatting I have to > do - and I do a lot of it. > > David Park > djmp at earthlink.net > http://home.earthlink.net/~djmp/ > > From: Chris Chiasson [mailto:chris at chiasson.name] To: mathgroup at smc.vnet.net > > I went through a phase with the Notation package too. It is quite > useful, but I recommend just working directly with Format + MakeBoxes > and MakeExpression if you're going to change things. Think of the > Notation package as a graphical wrapper for defining > MakeBoxes/MakeExpression rules via the notebook interface instead of > typing everything out. Anyway, you could probably guess how to define > the correct MakeExpression rule just from looking at the source code > you provided to us. > > MakeExpression[RowBox[{SuperscriptBox["\[Del]","2"],whatever_String}],_]:= > MakeExpression["delSquared"<>whatever] > > I strongly caution you to stay away from MakeExpression. Just think of > the problems WRI has with TraditionalForm input - and they invented > the Mathematica box model. Format & MakeBoxes, OOTH, are nice > functions for formatting your output. > > On 9/20/06, Peter <pjcrosbie at attglobal.net> wrote: > > I am attempting to write more readable code by defining some custom > > symbols. I can get simple constructions to sumbolize but I fail on > > anything too complicated. For example, > > > > Symbolize[$B"&(B_] > > > > Cell[BoxData[ > > RowBox[{"Symbolize", "[", > > TagBox["\[EmptyDownTriangle]_", > > NotationBoxTag, > > TagStyle->"NotationTemplateStyle"], "]"}]], "Input"] > > > > Works fine and symbolizes things like $B"&(Bf without problems. > > > > However, if I add a superscript to the $B"&(B as in > > > > Cell[BoxData[ > > RowBox[{"Symbolize", "[", > > TagBox[ > > RowBox[{ > > SuperscriptBox["\[EmptyDownTriangle]", "2"], "_"}], > > NotationBoxTag, > > TagStyle->"NotationTemplateStyle"], "]"}]], "Input"] > > > > then the symbol following the $B"&(B^2 is always interpreted as a > > multiplication. > > > > Any ideas would be really helpful. > > > > > > > -- > http://chris.chiasson.name/ > > > -- http://chris.chiasson.name/

**References**:**Re: Symbolize Problem***From:*"Chris Chiasson" <chris@chiasson.name>