7
$\begingroup$

Sometimes strings in a notebook/package file end up garbled due to some unknown/unintentional conversion of the expression/cell/notebook. I could not find a way to revert such unreadable text to its original version. When I convert such a cell to OutputForm (via the menu option), the result looks similar to the original, but then I cannot copy the text anymore.

From this (inserted as image to maintain formatting):

Mathematica graphics

To this:

"Set.\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\).\!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\), Set.\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\).\!\(\*
StyleBox[\"field\",\nFontSlant->\"Italic\"]\).\!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\) or Set.\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\).\!\(\*
StyleBox[SubscriptBox[\"f\", \"1\"],\nFontSlant->\"Italic\"]\).\!\(\*
StyleBox[SubscriptBox[
  StyleBox[\"f\",\nFontSlant->\"Italic\"], \"2\"],\n\
FontSlant->\"Italic\"]\).(\[Ellipsis]).\!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\) is interpreted as \
SetField[\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\), \!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\)], SetField[\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\), \!\(\*
StyleBox[\"field\",\nFontSlant->\"Italic\"]\), \!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\)] or SetField[\!\(\*
StyleBox[\"obj\",\nFontSlant->\"Italic\"]\), \!\(\*
StyleBox[SubscriptBox[
  StyleBox[\"f\",\nFontSlant->\"Italic\"], \"1\"],\n\
FontSlant->\"Italic\"]\), \!\(\*
StyleBox[SubscriptBox[\"f\", \"2\"],\nFontSlant->\"Italic\"]\), \
\[Ellipsis], \!\(\*
StyleBox[\"new\",\nFontSlant->\"Italic\"]\)] respectively. Note that \
Set modifies the behaviour of Dot."

I don't really know what the Cell > Convert To menuoptions do, as both cells are in StandardForm and are also set to StandardForm Display. Is there a way to revert the garbled text to its original without manually editing it? Anyone knows the cause of this behaviour? You are welcome to edit the title of this post as I had no idea how to make it informative.

Edit:

These conversions mostly happen in package files (.nb) when I comment out some usage messages or other formatted text. The text immediately converts to raw form when I remove the comment markers. Sadly, when I use kguler's answer below, and copy the text reverted by Normal, the pasted text appears again in raw form.

$\endgroup$
3
  • $\begingroup$ Do you have a sample corrupted file? $\endgroup$ Commented Jan 26, 2012 at 11:15
  • $\begingroup$ Quite a few. But I intend to look for a solution that reverts the given expression "inline" (i.e. I don't even have to close the notebook). $\endgroup$ Commented Jan 26, 2012 at 11:32
  • $\begingroup$ Related question: stackoverflow.com/questions/8283256/… $\endgroup$ Commented Jan 26, 2012 at 11:48

2 Answers 2

2
$\begingroup$

The fastest method keystroke-wise that I have found is:

  1. Create new empty cell

  2. Press Ctrl+Shift+E to get Cell[BoxData[""], "Input"]

  3. Paste your expression into that one replacing the "" inside of BoxData.

  4. Press Ctrl+Shift+E again.

Alternatively, plaste your expression into this and evaluate:

FrontEndExecute@FrontEnd`CellPrint[
  (* expression here *)
]
$\endgroup$
0
4
$\begingroup$

This looks like the Raw Input Form (one of the Convert To options in the Cell menu) of a string with formatted elements. Also, if you take a formatted string like the first code block in your question and append it with //ToBoxes, or if you select Show Expression in the Cell menu, you get something similar to the second block.

Append //Normal to the second block of code in your question to get the first block back.

Edit: If you use //Normal or //Normal//Print; and after highlighting the content of the printed cell select Copy As->Plain Text in the Edit menu, it seems work. Alternatively, combining all steps, if you add the following at the end of problem cells

 // Normal // Print; SelectionMove[
 EvaluationNotebook[], Previous, Cell]; FrontEndExecute[
 FrontEndToken["CopySpecial", "PlainText"]]; SelectionMove[
 EvaluationNotebook[], After, CellGroup, 2]; FrontEndExecute[
 FrontEndToken["Paste"]];

you get a copy of the contents that can be edited and copied without reverting to FullForm.

Cleaner alternative is a variant of Mr. Wizard's answer: Put at the end of the problem cell

 // BoxData // Cell[#,"Text"] & // CellPrint
$\endgroup$
3
  • $\begingroup$ Thanks, that worked. Saves me a lot of typing! $\endgroup$ Commented Jan 26, 2012 at 11:43
  • $\begingroup$ On a closer look, it only works in simple cases. See edit in my post. $\endgroup$ Commented Jan 26, 2012 at 11:51
  • $\begingroup$ Yes, with the addition, it works. Strange though that this method returns an expression and not a string. $\endgroup$ Commented Jan 27, 2012 at 14:30

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.