Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
ToExpression[RowBox[{"a","(*","what", "*)"}],StandardForm]

This conversion above ignores the commment content (*what*)

The result I expected is something like the DisplayForm below.


enter image description here

Update: one of my test code, how to improve it, and not use (StringReplace) if possible.


RowBoxtoString[RowBox[{"(*","Comment Content","*)"}]]
(*Comment Content*)
share|improve this question
Very closely related: – R. M. Jun 15 '13 at 7:15
up vote 1 down vote accepted

Here's a way, with a couple of output methods :-

First, setting up a RowBox template, with some non-comment objects. (The template is useful for setting up more complex FullForm expressions.)

Clear[a, b]
c = FullForm@ToBoxes[a == b]

RowBox[List["a", "\[Equal", "b"]]

Now editing the RowBox parameters, (or substituting values into the FullForm object):

c[[1, 1, 2]] = "(*what*)";
c[[1, 1, 3]] = Sequence[];

RowBox[List["a", "(*what*)"]]

c still has a FullForm wrapper at this point, so First is used to reach the RowBox object.



Alternatively, displaying as regular output, (or as "Input", ready for evaluation):

CellPrint@Cell[BoxData@First@c, "Output"]


Finally as a function :-

RowBoxtoString[x_, y_] := Module[{a, b, c},
  c = FullForm@ToBoxes[a == b];
  c[[1, 1, 1]] = x;
  c[[1, 1, 2]] = y;
  c[[1, 1, 3]] = Sequence[];
  CellPrint@Cell[BoxData@First@c, "Output"]]

RowBoxtoString["a", "(*what*)"]


share|improve this answer
fine, I like your usage of Set for Boxes. – HyperGroups Jun 15 '13 at 14:39

I guess commment is not expression, so use NotebookWrite[nb, content] may help.

NotebookWrite[SelectedNotebook[], StringJoin @@ RowBox[{"a", "(*", "what", "*)"}]]

results: a (*what*) which a is still a symbol.

If you want the output to be a single string:

  "\"" <> (StringJoin @@ RowBox[{"a", "(*", "what", "*)"}]) <> "\""]

results: "a(*what*)"

share|improve this answer
"a (*what*)" this is also one expected result. – HyperGroups Jun 15 '13 at 5:20
@HyperGroups Updated, hope it helps. – jamtype7 Jun 15 '13 at 5:55
Well,I need ToExpression, See my edit, better to not use StringReplace and make it suit for my original example. – HyperGroups Jun 15 '13 at 6:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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