3
$\begingroup$

Bug introduced in 10.4 or earlier and persisting through 11.3

CASE:3910218


When you input

{ 1 , 2 } (*spaces intended, retype them if you want to copy this*)

The underlying structure (Ctrl+Shift+E) is:

boxes = RowBox[{"{", " ", RowBox[{"1", " ", ",", " ", "2"}], " ", "}"}]

You can evaluate input cell or do something like

ToExpression[boxes]

{1,2}

and it works nicely, however:

MakeExpression[boxes, StandardForm]

Syntax::sntxf : "{ " cannot be followed by "1 , 2 }".

ErrorBox[RowBox[{"{", " ", ErrorBox[RowBox[{"1", " ", ",", " ", "2"}]], " ", "}"}]]

Questions

  1. Is this a bug or have I missed something?

  2. What would be the best workaround?

    MakeExpression[boxes /. " " -> Nothing, StandardForm] looks fine

  3. What does the FrontEnd do before sending boxes for evaluation, LinkSnooper shows that " " are not present when the content is sent:

    EnterExpressionPacket[
        MakeExpression[
           BoxData[RowBox[{"{", RowBox[{"1", ",", "2"}], "}"}]]
         , StandardForm
        ]
    ]
    
$\endgroup$
  • $\begingroup$ Have you tried Trace[ToExpression[boxes], TraceInternal -> True]? It calls StripBoxes as the first step which removes the redundant spaces. Then the output is sent to MakeExpression which returns ErrorBox[ErrorBox[RowBox[{"1", ",", "2"}]]] as intermediate result. But I don't understand why this intermediate result is ignored and the expected output is returned nevertheless. $\endgroup$ – Alexey Popkov Jun 26 '17 at 16:11
  • $\begingroup$ From the Docs for StripBoxes: "When boxes are sent by the front end to the kernel for evaluation, the front end strips the boxes using a more extensive process that may depend upon active stylesheets and front end option settings. However, when working with functions that directly serialize notebook constructs such as NotebookRead, no stripping will occur." $\endgroup$ – Alexey Popkov Jun 26 '17 at 16:17
  • $\begingroup$ As a workaround MakeExpression[StripBoxes@boxes, StandardForm] seems to work (but note the citation in the previous comment: "... the front end strips the boxes using a more extensive process ..."). $\endgroup$ – Alexey Popkov Jun 26 '17 at 16:20
  • $\begingroup$ @AlexeyPopkov Ah, I was aware of StripBoxes but didn;t have time to play with Trace, also I didn't know if ToExpression does the same thing as what is done at Shift+Return event. p.s. I think StripBoxes is worth an answer, $\endgroup$ – Kuba Jun 26 '17 at 16:34
  • $\begingroup$ I've checked it with version 5.2 and the behavior is exactly the same despite the Docs claiming that StripBoxes is introduced in version 6.0 (actually StripBoxes was present already in version 3.0!). I'm pretty sure it isn't a bug, just incomplete documentation on the difference between ToExpression and MakeExpression. $\endgroup$ – Alexey Popkov Sep 21 '17 at 9:45
3
$\begingroup$

Tracing the evaluation with Trace[ToExpression[boxes], TraceInternal -> True] reveals that ToExpression calls StripBoxes as the first step which removes the redundant spaces. So the obvious workaround is

MakeExpression[StripBoxes@boxes, StandardForm]

which seems to work.

Note however that according to the Docs page for StripBoxes,

When boxes are sent by the front end to the kernel for evaluation, the front end strips the boxes using a more extensive process that may depend upon active stylesheets and front end option settings.

$\endgroup$
  • $\begingroup$ StripBoxes is what I use now as " " is not the only problem it seems. Multi space " " is just another example. And all this is what I get from Boxes InputField. $\endgroup$ – Kuba Jun 27 '17 at 5:25
  • $\begingroup$ I faced another issue, please check this MakeFormattedBoxes@Plot[Sin @ x, {x, 0, (3 + TemplateSlot["x5"]) Pi}, ImageSize -> 200 ] if you wrap this with RawBoxes and evaluate the outpput cell you get it will work fine. ToExpression @ StripBoxes @ MakeFormattedBoxes[...] does not work though. Any ideas? I'm hesitating if it is worth another question. $\endgroup$ – Kuba Jul 31 '17 at 21:00
  • $\begingroup$ There are no special boxes there, the couse seems to be unorthodox RowBox placement. I could do Flatten->StringJoin->MakeExpression but it feels like an overkill. $\endgroup$ – Kuba Aug 1 '17 at 7:36
  • $\begingroup$ @Kuba Then it may worth a separate question. $\endgroup$ – Alexey Popkov Aug 1 '17 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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