3

I know the example usages of MakeBoxes in the Tutorial like this. but I want to know a subtle distinction between (1) and (3)

gplus /: MakeBoxes[gplus[x_, y_, n_], StandardForm] := 
 RowBox[{MakeBoxes[x, StandardForm], 
   SubscriptBox["\[CirclePlus]", MakeBoxes[n, StandardForm]], 
   MakeBoxes[y, StandardForm]}]

In(1)

MakeBoxes[gplus[a, b, m + n], StandardForm]

Out(1)

RowBox[{"a", SubscriptBox["[CirclePlus]", RowBox[{"m", "+", "n"}]], "b"}]

In(2)

MakeBoxes[gplus[a, b, m + n], StandardForm] // RawBoxes

Out(2)

enter image description here

In(3)

gplus[a, b, m + n]

Out(3)

enter image description here

I had thought that if I type the In(1) then it will be like to Out(3). I want to know the diffenrences after In(1) enter typing and In(3) enter typing. When I type the shift-enter keys. what the front end mechanism does like RawBoxes to do.

Junho Lee
  • 5,155
  • 1
  • 15
  • 33
  • I'm not seeing the difference between output (1) and output (2). What should I be looking at? – Mr.Wizard Jul 02 '14 at 06:43
  • I'm sorry I couldn't well explain of that. Just I want to know intrinsic processes after type shift-enter. – Junho Lee Jul 02 '14 at 07:06
  • Sorry, but I still don't understand your question, unless perhaps it is akin to my own here: (29264). Are you asking how expressions in a Notebook are evaluated? Okay, I just saw your comment above. – Mr.Wizard Jul 02 '14 at 07:06
  • I think I now know what you want but I don't have time to give a proper answer. If someone hasn't already done that tomorrow I'll try to remember to do so. – Mr.Wizard Jul 02 '14 at 07:13
  • My short answer would be: to get the expected notebook display, you have to feed the boxes to DisplayForm. – Jens Jul 02 '14 at 18:42

1 Answers1

6

I assume you are looking for a pretty specific answer. If this is less information than you are asking for feel free to comment or edit the question and I will expand.

As you know, the frontend represents expressions using boxes. These are wrappers that are concerned with appearance, and are peripheral to core symbolic evaluation. Therefore, each step of the evaluation procedure generally entails stripping boxes, evaluating symbolically, and adding boxes back as appropriate. But this is happening at every layer of an expression, and how boxes are dealt with may itself involve some evaluation.

Loosely speaking, then, when you hit SHIFT+ENTER, three things happen at every layer. Mathematica uses MakeExpression to rearrange boxes into canonical forms, then it evaluates those expressions, then it wraps them using MakeBoxes. We can override different parts of this process as we please.

Let's start at the beginning.

MakeExpression[RowBox[{"f", "[", x_, "]"}], form_] := (
  Print@"MakeExpression";
  MakeExpression[RowBox[{"g", "[", x, "+", "2", "]"}], form]);
f[1 + 2]

MakeExpression

g[5]

We can see that this transformation occurs prior to evaluation.

f[1 + 2] // Hold

MakeExpression

Hold[g[(1 + 2) + 2]]

Similarly,

g[x_] := (
  Print@"g";
  h[x]);
f[1 + 2]

MakeExpression

g

h[5]

Once evaluation is complete, the expression is rewrapped.

MakeBoxes[h[x_], form_] := (
  Print@"MakeBoxes";
  MakeBoxes[i[x], form]);
f[1 + 2]

MakeExpression

g

MakeBoxes

i[5]

What comes out of MakeBoxes does not get interpreted as input.

Attributes@MakeBoxes

{HoldAllComplete}

MakeBoxes[h[1 + 2]]

MakeBoxes

RowBox[{"i", "[", RowBox[{"1", "+", "2"}], "]"}]

RawBoxes is just a wrapper that tells Mathematica not to transform what's inside into boxes. In other words, it indicates what's inside has already been wrapped, and Mathematica renders it directly.

% //RawBoxes

i[1 + 2]

We can confirm that this is not being interpreted as input.

i[x_] := "foo";
MakeBoxes[h[1 + 2]] // RawBoxes

MakeBoxes

i[1 + 2]

But copy that and paste it in a new cell:

i[1 + 2]

"foo"

And we are back full circle to MakeExpression.

mfvonh
  • 8,460
  • 27
  • 42