Message Boards Message Boards

GROUPS:

[✓] Understand orderings in ToExpression and RowBox?

Posted 6 months ago
564 Views
|
8 Replies
|
2 Total Likes
|

(Posted to StackExchange a week ago, with no response.)

With:

ToExpression[RowBox[{"c", ToBoxes[Plot[Sin[x], {x, -2, 2}]], "a"}]]

I get the three elements of the array, but with "a" and then "c" at the beginning. I would like to retain the order as I have input it.

I found that:

ToExpression[RowBox[{"c", "b", "a"}]]

reverses the order, but:

ToExpression[RowBox[{"\(", "c", "b", "a", "\)"}]]

retains the input order (and removes spaces in between).

However, applying this to my original snippet:

ToExpression[
    RowBox[{"\(", "c", ToBoxes[Plot[Sin[x], {x, -2, 2}]], "a", "\)"}]]

it comes back with:

RowBox[{"c", "\"Private`zztop$3\"", "a"}]

(zztop??).

Please help me understand the restrictions here, as well as possible work-arounds.

Thank you.

8 Replies

It seems that ToExpression has to give an interpretation to RowBox, and sometimes it treats it as if it were a product:

ToExpression[RowBox[{"c","b","a"}]]//FullForm
Times[a,b,c]

and sometimes as if it were a string:

ToExpression[RowBox[{"\(", "c", "b", "a", "\)"}]] // FullForm
"cba"

The products are automatically reordered in canonical order.

Posted 6 months ago

Gianluca, thank you for your reply, as well as the explanation of the "interpretation" context. (I still do not understand where the "zztop" came from.)

Do you have a suggestion for a workaround?

Again, I am trying to concatenate together strings, and expressions like plots, and to retain the input order. I am using ToExpression[] since this is all fed back into CellGroup[ExpressionCell[ ... ]].

Thank you.

Can you give us a simple example of what you are trying to achieve? I am not an expert in box structures.

Can you add a link to the SE post?

I agree more details about the final goal would be on point as this looks like a XY problem. Meanwhile, here's what I can add:

boxes = RowBox[{"c", ToBoxes[Plot[Sin[x], {x, -2, 2}]], "a"}];

boxes is a typeset/box representation of a product: c Graphics[...] a. Where did it came from and what is your plan? If you only want to display it:

RawBoxes @ boxes

If you want to convert it to an expression but keep the input order:

ToExpression[boxes, StandardForm, Hold]   (*HoldForm or whatever you need*)

But if your initial plan was to put c next to the plot which is next to a then why not:

Row[{"c", Plot[Sin[x], {x, -2, 2}], "a"}]
Posted 6 months ago

Hello. Here is the link to the SE discussion. (Instead of continuing both here and there, is there a preferred place?)

Certainly my intentions were to keep my question as simple as possible, and not to post an XY problem.

Based on Gianluca's explanation, I moved to string/text, rather than expression, i.e.:

TextCell[StringForm["`` `` ``", "c", Plot[Sin[x], {x, -2, 2}], "a"]]

This does the job.

However, since this was feeding back into CellGroup[], and from Kuba's answer (here and at SE), I need to move to the lower-level Cell[] and CellGroupData[], I can no longer use TextCell[]. Sigh.

So I am back to boxes (as Kuba suggested in SE), with:

Cell@CellGroupData[{Cell@
    BoxData@RowBox[{"c", ToBoxes[Plot[Sin[x], {x, -2, 2}]], "a"}]
   }]

which can be, e.g., CellPrint[]ed.

Yes, though

Cell@CellGroupData[{Cell@
    BoxData@ToBoxes@Row[{"c",Plot[Sin[x], {x, -2, 2}], "a"}]
   }]

may be more convenient

Posted 6 months ago

Kuba, thank you again. (BTW, I took the RowBox form from you. :-) )

Posted 6 months ago

One final note: apparently one needs to keep things "low-level" with CellGroupData[], but also, for the groupings to work, it needs to be fed back into NotebookWrite[] and not CellPrint[].

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract