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?

  • $\begingroup$ have you tried saving as an m-file? (ie save as->mathematica package?) You might also want to kill all output before doing that $\endgroup$ – acl Apr 15 '13 at 18:44
  • 2
    $\begingroup$ 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. $\endgroup$ – Tobias Hagge Apr 15 '13 at 18:52
  • $\begingroup$ @TobiasHagge good point. perhaps it's worth mentioning that that, too, will only include the code/initialization cells in executable form. $\endgroup$ – acl Apr 15 '13 at 19:59
  • $\begingroup$ What do you mean when you say "plain text"? I ask because the Mathematica notebook files only contain Wolfram Language expressions encoded in ASCII. $\endgroup$ – Christopher Cole Jul 3 '14 at 17:03

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",
    "Output", All, CellStyle, AutoScroll -> False], 

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; maybe it's from here, thanks to SeanD). 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.

  • 1
    $\begingroup$ There is an "delete all output" option under the Cell menu. $\endgroup$ – Sjoerd C. de Vries Apr 15 '13 at 19:17
  • $\begingroup$ 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 $\endgroup$ – rm -rf Apr 15 '13 at 19:20
  • $\begingroup$ @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. $\endgroup$ – acl Apr 15 '13 at 19:26
  • $\begingroup$ @rm-rf does it save all initialization cells? didn't know that $\endgroup$ – acl Apr 15 '13 at 19:28
  • $\begingroup$ Maybe you lifted the code from mathematica.stackexchange.com/a/8762 $\endgroup$ – user16306 Jul 3 '14 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.