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.

I would like to save my Mathematica notebooks as plain-text documents, so that I can keep them under git version control.

I thought that File > Save as... + Plain Text would do what I want, but I'm having problems with it.

First, I can't figure out how to "re-import" into Mathematica (in a form that can be readily evaluated) a notebook saved this way.

Second, the human-readable text content is overwhelmed by serializations of graphic objects, etc. Therefore, the output produced by git diff on two versions of such text files is a nightmare.

Third, the human-readable text portions are not all that readable. Of course, some rich formatting cannot be easily converted to text, but I would like to preserve at least some of the indentation found in the original, if possible.

Is there a better approach?

share|improve this question
    
have you tried saving as an m-file? (ie save as->mathematica package?) You might also want to kill all output before doing that –  acl Apr 15 '13 at 18:44
2  
In case it is useful, Mathematica->format->Option Inspector->Notebook Options->File Options->AutoGeneratedPackage will save a .m file containing the notebook's initialization cells, automatically every time you save the notebook. –  Tobias Hagge Apr 15 '13 at 18:52
    
@TobiasHagge good point. perhaps it's worth mentioning that that, too, will only include the code/initialization cells in executable form. –  acl Apr 15 '13 at 19:59
add comment

1 Answer

I deal with this by a) removing all output, b) saving as a package file (m-file).

To remove all output, I have some code from somewhere and put it into a button, inside a palette:

Button["kill out",
 FrontEndExecute[{FrontEnd`NotebookFind[FrontEnd`SelectedNotebook[], 
    "Output", All, CellStyle, AutoScroll -> False], 
   FrontEnd`FrontEndToken["Clear"]}]
 ]

Clicking this removes all output cells from the selected notebook. Possibly I lifted this code from somewhere (in which case if anybody knows, let me know). As Sjoerd points out in a comment, there's a menu function for this, but it requests confirmation first (which this approach doesn't).

I suggest removing the output because otherwise you end up with the serialized representations of graphics etc, as you have noted.

Also note that this approach saves code cells (or more generally, as rm-rf points out, initialization cells); everything else gets commented out.

share|improve this answer
    
There is an "delete all output" option under the Cell menu. –  Sjoerd C. de Vries Apr 15 '13 at 19:17
    
Also, you should probably mention that if you're saving as a package, one should use code cells (or more generally, initialization cells), as otherwise, it will be commented out –  rm -rf Apr 15 '13 at 19:20
    
@SjoerdC.deVries yes I should have mentioned that the reason I did this is that the menu function asks for confirmation; the one I gave doesn't. –  acl Apr 15 '13 at 19:26
    
@rm-rf does it save all initialization cells? didn't know that –  acl Apr 15 '13 at 19:28
add comment

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.