Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Do you have a sample corrupted file? –  J. M. Jan 26 '12 at 11:15
    
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). –  István Zachar Jan 26 '12 at 11:32
    
Related question: stackoverflow.com/questions/8283256/… –  Leonid Shifrin Jan 26 '12 at 11:48

2 Answers 2

up vote 2 down vote accepted

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 *)
]
share|improve this answer
    
I did try //BoxData//Cell[..] combination but could not get it to work. Now I see I needed to add CellPrint. –  kguler Jan 26 '12 at 16:42
    
Thanks Mr.Wizard, it worked. –  István Zachar Jan 27 '12 at 14:29

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
share|improve this answer
    
Thanks, that worked. Saves me a lot of typing! –  István Zachar Jan 26 '12 at 11:43
    
On a closer look, it only works in simple cases. See edit in my post. –  István Zachar Jan 26 '12 at 11:51
    
+1 for Normal –  Mike Honeychurch Jan 26 '12 at 22:21
    
Yes, with the addition, it works. Strange though that this method returns an expression and not a string. –  István Zachar Jan 27 '12 at 14:30

Your Answer

 
discard

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.