5
$\begingroup$

I start with

f[x].

In boxes, this is

RowBox[{"f", "[", "x", "]"}].

I apply MakeExpression to give f[x] again, as desired.

Then I try

f[ x],

which in boxes is RowBox[{"f", "[", "\[IndentingNewLine]", "x", "]"}]. Applying MakeExpressiongives f[x], which is ok for my current purposes.

The problem arises when I try to apply this to Table. Without the newline character, eg.

Table[x, {x, 2}],

converting to boxes and then using MakeExpression works as expected.

Then I try

Table[x , {x, 2}].

In boxes, this is RowBox[{"Table", "[", RowBox[{"x", "\[IndentingNewLine]", ",", RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}], "]"}]

When I apply MakeExpression to this, I get ErrorBox[RowBox[{"Table", "[", ErrorBox[ RowBox[{"x", "\[IndentingNewLine]", ",", RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}]], "]"}]]

Does anyone know how I can get around this?

$\endgroup$
3
  • $\begingroup$ If the problem comes from "\[IndentingNewLine]", why not just remove it? For example, /."\[IndentingNewLine]"->Nothing//MakeExpression $\endgroup$ May 27, 2016 at 14:43
  • $\begingroup$ So I have tried DeleteCases[ (My box expression here) , "[IndentingNewLine]"], but it didn't do anything (I feel like it should have so I was a bit unsure about this). Your code replaces it with 'Nothing', which MakeExpression still can't read properly $\endgroup$
    – Jojo
    May 27, 2016 at 14:57
  • $\begingroup$ It seems to work for me, see my answer below. $\endgroup$ May 27, 2016 at 15:06

2 Answers 2

1
$\begingroup$

If the problem comes from the "[IndentingNewLine]", one can simply remove it. For example:

RowBox[{"Table", "[", 
    RowBox[{"x", "\[IndentingNewLine]", ",", 
      RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}], "]"}] /. 
  "\[IndentingNewLine]" -> Nothing // MakeExpression

(* HoldComplete[Table[x, {x, 2}]] *)

If you use versions early than 10.2, then this seems to work

DeleteCases[
 RowBox[{"Table", "[", 
   RowBox[{"x", "\[IndentingNewLine]", ",", 
     RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}], 
   "]"}], "\[IndentingNewLine]", ∞]
(* HoldComplete[Table[x, {x, 2}]] *)
$\endgroup$
4
  • $\begingroup$ When I run your code, I get ErrorBox[RowBox[{"Table", "[", ErrorBox[ RowBox[{"x", Nothing, ",", RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}]], "]"}]]  $\endgroup$
    – Jojo
    May 30, 2016 at 11:55
  • $\begingroup$ What version are you using? See my update. $\endgroup$ May 30, 2016 at 14:24
  • $\begingroup$ Thanks so much, that works just great :) It appears that we only have a license for version 9 at my university $\endgroup$
    – Jojo
    May 30, 2016 at 15:24
  • $\begingroup$ @Joe No problem, glad it helped. $\endgroup$ May 30, 2016 at 15:25
0
$\begingroup$

Why don't you use ToExpression instead?

ToExpression[RowBox[{"f", "[", "x", "]"}], StandardForm, Defer]

f[x]

ToExpression[RowBox[{"f", "[", "\[IndentingNewLine]", "x", "]"}], StandardForm, Defer]

f[x]

ToExpression[RowBox[{"Table", "[", 
RowBox[{"x", "\[IndentingNewLine]", ",", 
RowBox[{"{", RowBox[{"x", ",", "2"}], "}"}]}], "]"}], StandardForm, Defer]

Table[x, {x, 2}]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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