3 Answers
3

My other answer is a nice solution for interactively looking at boxes, but in the comments, Mr.Wizard seems to be indicating that he's more interested in programmatic usage, and that he's definitely interested in seeing the box form after the FE has stripped non-semantic boxes to send to the kernel. So here's a totally different method for doing this which achieves those goals.

The first argument to UndocumentedTestFEParserPacket must be a string, so this solution precludes 2D input unless you formulate the 2D input using linear syntax. The second argument indicates whether the result should strip non-semantic boxes in the same way that the FE does at Shift+Enter time. True indicates that it should strip (note the return value does not include the space between b and c). Replacing it with False would leave the non-semantic boxes exactly as if they were being written to a notebook file.

If you're wondering what a non-semantic box is, this includes non-semantic spaces and several different box types if they have StripOnInput set to true. The list, and default values of StripOnInput options can be found in the Option Inspector (just search for StripOnInput). StyleBox also takes the StripOnInput option. By default, StyleBox is stripped in math but not in 2D or 3D graphics. Here's an example of StyleBox stripping.

I had sometimes wondered what this weirdly named packet did, and now we know! Thanks!
–
Oleksandr R.Oct 21 '12 at 3:13

This looks like the function I thought must exist. The name is amazing. Is there a reason you don't use FrontEndExecute?
–
Mr.Wizard♦Oct 21 '12 at 6:00

1

@Mr.Wizard, FrontEndExecute is implemented in terms of MathLink`CallFrontEnd, so the kernel definitions are functionally equivalent. I have reasons for preferring MathLink`CallFrontEnd, but they're quirky, and most people probably wouldn't find them relevant. I can't take credit for the name...I think Jason Harris came up with it.
–
John FultzOct 21 '12 at 6:20

This is very helpful, thank you. I notice that when I paste {1*^4, 000123, a*b c} it is displayed as {1.\[Times]10^4, 000123, a*b c} -- is there a way to keep 1*^4 as 1*^4 in the InputField itself? Also, I notice that this preserves the spaces e.g. "b", " ", "c" while $PreRead does not; are you able to comment on that difference?
–
Mr.Wizard♦Oct 20 '12 at 3:32

@Mr.Wizard, I noticed when I was preparing to post the answer that there seemed to be some oddness around your 1*^4 example as well. I didn't investigate in great detail what's going on here, but I'm thinking there might be a minor bug here. As far as spaces are concerned, there are a few canonicalizations the FE is allowed to make before sending boxes to the kernel. Stripping non-semantic spaces and non-semantic StyleBoxes are among those changes. That the FE does this makes it much easier to write robust MakeExpression rules in the kernel.
–
John FultzOct 20 '12 at 15:47

That makes sense. Is there any way to use InputField to get the version that is sent to the Kernel or $PreRead?
–
Mr.Wizard♦Oct 20 '12 at 16:43

Ah...well, it turns out there's a completely different way to do this which additionally allows you to strip (or not) boxes, so long as you can formulate the input as a String. That kind of sounds like what you want. I'll edit the answer to reflect both solutions.
–
John FultzOct 20 '12 at 22:12

1

On second thought, I think I'll just add a whole new answer. It really is a totally different approach.
–
John FultzOct 20 '12 at 22:19

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.