# Paste data into Mathematica with formatting

Select the following Mathematica code and paste it into a notebook. In Windows 7(Mathematica 8) the line breaks are not interpreted in the expression. In fact any data copipasted from stackexchange loses it's formatting. How do I paste content from stackexchange into Mathematica while keeping the formatting?

Print[
"text"
]


EDIT: Now ideally Mathematica would support this natively. Solutions to integrate this into Mathematica would be great. I have 3 possible ideas.

1. modify Ctrl+V so this automatically occurs
2. assign a different modifier
3. create a button or palette

I'm going to do the 3rd for now, but any other solutions are appreciated.

-
Prior question: mathematica.stackexchange.com/questions/29525/… –  Mr.Wizard Aug 16 '13 at 23:35
@Mr.Wizard that's fair if you really want. The other question though I wouldn't mark as a duplicate. Formatting data in strings doesn't seem like an appropriate solution to the other issue IMO. I just wanted people to find the question if they search for formatting issues. I would mark this as duplicate mathematica.stackexchange.com/questions/13317/… But not the question I asked. –  Liam William Aug 16 '13 at 23:39
@Mr.Wizard Modified the question. It is up for grabs if you want it ;) I think this would be valuable for users. Also I wouldn't mark this as a duplicate because your solution breaks automatic formatting and expression highlighting. –  Liam William Aug 16 '13 at 23:42
I'm going to reopen this question. I would encourage you to include something about making this easy to use, perhaps as a palette button, as that seems like an easier goal. Sorry for the hasty close. –  Mr.Wizard Aug 16 '13 at 23:51
i just create a Code cell, which preserves the formatting. it'd be nice if these kinds of tricks weren't necessary though. i assume Mathematica eats the formatting primarily to delete spaces, which don't mix well with the pretty-form display –  amr Aug 19 '13 at 3:11

As Mr.Wizard showed me here. Run the following with " " replaced with the pasted data.
For example:

CellPrint@Cell[
First@FrontEndExecute@UndocumentedTestFEParserPacket["Print[
\"test\"
]", False], "Input"]


And here is a Palette with a Button that automates the process.

CreatePalette@Button["Paste", NotebookWrite[InputNotebook[],
Catch[NotebookGet@ClipboardNotebook[] /. Cell[
r_, ___] :> Block[{}, Throw[r, tag] /; True];
$Failed, tag] ] ]  If you want a shortcut you can run the following to assign it to Ctrl+Shift+V as described here. FrontEndExecute[ FrontEndAddMenuCommands[ "DuplicatePreviousOutput", {Delimiter, MenuItem["Raw Paste Clipboard", FrontEndKernelExecute[ NotebookWrite[InputNotebook[], Catch[NotebookGet@ClipboardNotebook[] /. Cell[r_, ___] :> Block[{}, Throw[r, tag] /; True];$Failed, tag]]

], MenuKey["v", Modifiers -> {"Control", "Shift"}],
SystemMenuEvaluator -> Automatic]}]]


Or do the add the following to KeyEventTranslations as described here. EDIT: This doesn't seem to be working currently and advised fix would be great. You can the put the AddMenuCommands in init.m as workaround.

Item[KeyEvent["v", Modifiers -> {Control, Shift}],
FrontEndExecute[{

FrontEndNotebookWrite[InputNotebook[],
Catch[NotebookGet@ClipboardNotebook[] /.
Cell[r_, ___] :> Block[{}, Throw[r, tag] /; True];
\$Failed, tag]]

}]],

-
Thanks a lot for sharing this. This seems to solve one of my current problems. Big +1. –  Leonid Shifrin Aug 16 '13 at 23:20
@LeonidShifrin Yes every time I copy data from stackexchange I begin the process of reformatting and adding linbreaks, etc.. Not anymore. ;) –  Liam William Aug 16 '13 at 23:21
@Leonid I've been spewing UndocumentedTestFEParserPacket all over the site ever since I learned it from John Fultz. Please don't tell me this is the first time you've seen it. –  Mr.Wizard Aug 16 '13 at 23:22
@Mr.Wizard I had seen it several times, I just never connected the dots to fix the problem. It would probably be best to modify the internal paste function so you don't have to run this everytime –  Liam William Aug 16 '13 at 23:23
@AlexeyPopkov I have to many windows open right now to want to mess with mathematica's internals but you could use this as a workaround for now mathematica.stackexchange.com/questions/1606/… –  Liam William Aug 18 '13 at 4:12

As a start we can create a basic paste function using the ClipboardNotebook as follows:

pasteRaw[] :=
NotebookGet[ClipboardNotebook[]] /.
Notebook[{Cell[BoxData[data_] | data_, ___]}, ___] :>
(CellPrint[Cell[BoxData[data], "Input"]];)


After copying the desired text you can evaluate pasteRaw[] to paste it as input. At present no syntax checking is done and you will get an error box if the input is invalid or incomplete.

-
Do you know of anyway to force getting the string representation of data from the clipbaord instead of getting the parsed cell data from ClipboardNotebook[]? For example if you use both my code and yours extra spaces are added to the BoxData which wouldn't occur if you used UndocumentedTestFEParserPacket[str], False] –  Liam William Aug 25 '13 at 3:18
@Liam I'll have to think about that; perhaps after the weekend. –  Mr.Wizard Aug 25 '13 at 3:22