4

Mathematica has a dirty habit of sometimes escaping all the formatting in my strings when loading a .nb. I know this is inevitable for .m etc. and it still produces the required output, but having source like this is irritating and harder to maintain.

"\!\(\*FractionBox[SubscriptBox[\(x\), \(max\)], \(t\)]\)"

I thought Evaluating in Place or Converting to Whatever Form might help, but alas. Is there a way for me to change the strings back to the proper x_max/t form without just manually pulling each one out and evaluating it?

mmal
  • 3,508
  • 2
  • 18
  • 38
  • You mentioned evaluating in place doesn't work, seems to work for me, any other clues? – M.R. Dec 02 '15 at 17:28
  • @M.R. Here's a clue, even if I evaluate and have the correct form, and then paste that back in, it goes back to escaped formatting. If I just type the whole thing out fresh, and paste that thing in it's fine. Maybe it's just me? –  Dec 03 '15 at 14:54
  • Can you give an example of the desired output? Do you want "FractionBox[SubscriptBox[x, max], t]" instead? – M.R. Dec 03 '15 at 19:06
  • I want what you get as e.g. Out[31] in your screenshot below. I want the string as "rendered" rather than a string with backslashes, or something that will only evaluate to what I want. –  Dec 03 '15 at 19:12
  • I think there is a bug in v10.2 where you first run your string so it outputs the desired fraction. Then in the output cell, place the cursor at the rightmost end, and hit spacebar. You'll get an "INTERNAL SELF-TEST ERROR" MathEditCells2|c|1412. – QuantumDot Jan 01 '16 at 21:51

2 Answers2

3
s = "\!\(\*FractionBox[SubscriptBox[\(x\), \(max\)], \(t\)]\)"

You can use Evaluate or ToExpression

Evaluate@s
ToExpression[s, StandardForm, HoldForm]

enter image description here

When you copy the contents of a cell from a notebook into a program such as a text editor, no explicit backslash backquote sequence is usually included. But if you expect to paste what you get back into a cell of a different type from the one it came from, then you will typically need to include a backslash backquote sequence in order to ensure that everything is interpreted correctly.

M.R.
  • 31,425
  • 8
  • 90
  • 281
  • Thanks for your answer. I was hoping not to have to do this, as I have a lot of code that with the bad formatting, and would like a way to fix all of it at once. Incidentally Evaluate happens automatically, so you just need to to "evaluate" a cell to get the right formatting. –  Dec 03 '15 at 14:46
3

If I understand you correctly, we have a file with a lot of stuff with 'bad formatting', i.e.

"\!\(\*FractionBox[SubscriptBox[\(x\), \(max\)], \(t\)]\)"
"Just some text"
"\!\(\*SuperscriptBox[\(a\),\(b\)]\)"

and we want first expression to be of the form: Times[Power[t,-1],Subscript[x,max]].

We have file Example.nb. Clear all outputs if there are some in place (Menu: Cell - Delete All Outputs).

enter image description here

Let us Get the content of Example.nb in the new file with:

nb = Notebooks["Example.nb"][[1]];
list = NotebookGet[nb][[1]]

Clean the content of the file with some rules (there are many ways to identify strings wrapped by BoxData that have symbol \!):

format[x_String] :=  If[StringContainsQ[x, "\\!\\"], ToExpression[x], x];
list2 = list /. Cell[BoxData[x_String], a___] :> Cell[BoxData[format[x]], a]

Finally we will Put the result in new notebook:

NotebookPut[Notebook[list2]]

New file with proper formatting (try //FullForm for cells) looks as follows: enter image description here

For more information see tutorial on Low-level formatting.

garej
  • 4,865
  • 2
  • 19
  • 42