Message Boards Message Boards


[?] Understand orderings in ToExpression and RowBox?

Posted 3 years ago
8 Replies
2 Total Likes

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


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:

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

it comes back with:

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


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

Thank you.

8 Replies
Posted 3 years 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[].

Posted 3 years ago

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

Yes, though

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

may be more convenient

Posted 3 years 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:

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

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

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"}]

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

Posted 3 years 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.

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


and sometimes as if it were a string:

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

The products are automatically reordered in canonical order.

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

Group Abstract Group Abstract