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.

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?


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.

share|improve this question
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. –  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. –  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
@amr Yes I am not sure why M doesn't respect spaces. You mean Ctrl+Shift+E form like BoxData form yes? The funny thing is that the clipboard holds the appropriate data and M even interprets it with spaces. For example you can do NotebookGet@ClipboardNotebook[] and get the correctly formatted data. I'm not sure what is going :| –  William Aug 19 '13 at 3:14

2 Answers 2

up vote 10 down vote accepted

As Mr.Wizard showed me here. You use UndocumentedTestFEParserPacket to print the pasted data. For example:

d[t_] := (StringReplace[t, {
     RegularExpression["^[ ]+$"] -> "",
     "\n" -> "\[IndentingNewLine]"}]);

        /. Cell[r_, ___] :> Block[{}, Throw[r, tag] /; True];
       $Failed, tag]
      , False]
   , t_String :> d[t], Infinity]
  , "Input"]

If you want a shortcut you can run the following to assign it to Ctrl+Shift+V as described here.

  "DuplicatePreviousOutput", {Delimiter, 
   MenuItem["Raw Paste Clipboard", 
            /. Cell[r_, ___] :> Block[{}, Throw[r, tag] /; True];
           $Failed, tag]
          , False]
       , t_String :> d[t], Infinity]
      ]], MenuKey["v", Modifiers -> {"Control", "Shift"}], 
    System`MenuEvaluator -> 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}], 

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

share|improve this answer
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. ;) –  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
@Liam Have you tried your code for KeyEventTranslations.tr? It gives a warning message "Serious Startup Error" when Mathematica starts (I use v. 8.0.4). –  Alexey Popkov Aug 17 '13 at 22:07
@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/… –  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.

share|improve this answer
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] –  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
Figured out why Mathematica kept adding the additional spaces. I wasn't really aware of the differences between IndentingNewLine and \n and how it modified the the spaces and other outputs. Again thanks for the help with UndocumentedTestFEParserPacket. –  William Oct 18 '14 at 8:54
@Liam Sorry that I never returned to this, but I am glad you solved your own problem. –  Mr.Wizard Oct 18 '14 at 17:31

Your Answer


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.