2
$\begingroup$

Restatement of question, including code.

numList = {"3 ", "(2)^2 ", "foo", "bar"};
Manipulate[nums = Extract[numList, {{1}, {i}}];
 Grid[{{"Perform the following calculation."}, {"input=" <> 
 StringJoin[nums]}, {"ans=" <> 
 ToString[ToExpression[StringJoin[nums]]]}}], {i, Range[2, 4]}]

Paste code into Mathematica. Replace foo with 2, control-6 (to get a superscript), 2. Replace bar with open paren, 2, close paren, control-6, 2. Execute, and then click on the ButtonBar. When i is 2 or 3, it works fine. If you click on i=4, you get a syntax error.

These should all be legal ways of doing 3 times 2^2.

$\endgroup$
9
  • 1
    $\begingroup$ "carrot" = caret? $\endgroup$ Oct 2, 2019 at 23:58
  • $\begingroup$ What exactly fails? $\endgroup$
    – Kuba
    Oct 3, 2019 at 6:06
  • 1
    $\begingroup$ Is there are particular reason why you want to input your expressions as strings? $\endgroup$
    – Lukas Lang
    Oct 4, 2019 at 8:01
  • 1
    $\begingroup$ If you look at FullForm[numList] you'll in the last string that "(2)" is not the base of the superscript. This string does what you wanted: "\!\(\*SuperscriptBox[\((2)\), \(2\)]\)" -- not sure how enter it tho' $\endgroup$
    – Michael E2
    Oct 5, 2019 at 0:03
  • 1
    $\begingroup$ Hm. I finally emailed Wolfram support, and they say this is a bug, and they'll work on it. $\endgroup$ Oct 23, 2019 at 16:14

1 Answer 1

1
$\begingroup$

How about this?

ToExpression@StringReplace[
 StringTrim@ToString@"(1\!\(\*SuperscriptBox[\()\), \(2\)]\)",
 pow_ ~~ "\n" ~~ base__ :> base <> "^" <> pow
]

1

$\endgroup$
1
  • $\begingroup$ Thanks, but I'm trying to, in a non-MMtica-user-friendly way, display some text, and then evaluate it. The above is not legible to anyone save a very few of us. $\endgroup$ Oct 3, 2019 at 14:52

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.