17

There are some well-known options for preventing the front end from adding "unnecessary" stuff to .nb files when keeping them e.g. in a git repository: CreateCellID, "FileOutlineCache" and "TrackCellChangeTimes".

However, recent Mathematica versions seem to add an ExpressionUUID to each cell, which makes the task of keeping .nb files in a repository more challenging.

So is there perhaps some dedicated option to turn that off and therefore get rid of ExpressionUUID in a given notebook?

Edit: Here is an example of a notebook created with Mathematica 12.0 that contains ExpressionUUID

vsht
  • 3,517
  • 13
  • 23
  • 2
    You can post-process the files using this tool. I was able to install it pretty easily and confirm it does remove the cell metadata – Jason B. Jan 29 '20 at 18:20
  • 4
    One thing you can do is write a .wl rather than a .nb. – evanb Jan 29 '20 at 18:21
  • There are cases where one has to stick to .nb files, e.g. for packet documentation. With the 3 above mentioned options it worked quite well with Mma 10.4 and git without any extra tools. So I still sort of hope for a hidden option to get rid of those pesky ExpressionUUID. – vsht Jan 29 '20 at 22:11
  • @vsht I do not know if I did something but I can not create them. Do they appear in every cell that you've created? – Kuba Jan 30 '20 at 07:11
  • @Kuba Are you using Mma 12? I added a link to a pastebin that contains a simple notebook with ExpressionUUID inside cells. – vsht Jan 30 '20 at 09:28
  • @vsht Ah, I forgot they are only added in the file because they don't exist explicitly in the front end. – Kuba Jan 30 '20 at 09:46
  • 2
    @vsht, no you cannot control the presence off ExpressionUUIDs. – ihojnicki Feb 03 '20 at 15:15
  • @ihojnicki I see, thanks for your reply. In any case I sent a suggestion to WRI to add an option (perhaps semi-hidden) to disable the addition of ExpressionUUIDs in a future Mma version. That would be quite useful and is probably not much work for the devs, since ExpressionUUIDs are apparently not vital for the notebooks but tend to be quite annoying for version control systems. – vsht Feb 03 '20 at 16:45
  • @vsht, what I will say is that the lack of an option was not an oversight. – ihojnicki Feb 03 '20 at 17:09
  • @ihojnicki Is there any potential harm if one, say, deletes ExpressionUUIDs by hand or uses an external tool like mathematica-notebook-filter? – vsht Feb 03 '20 at 17:16
  • @vsht, I cannot comment on the filter. Never used it, haven't looked at its implementation. As for ExpressionUUID... Today? In v12.0? There is no harm in removing them. And it's not like a notebook expression would be marked as corrupt without them. But it very well might end up losing out on future functionality. – ihojnicki Feb 03 '20 at 18:44
  • 1
    @ihojnicki Hmm, I understand that ExpressionUUID might be required for some future functionality, but on the other hand it makes it even more difficult to keep notebooks in git repositories. I also sent a suggestion to introduce "git friendly" notebooks, perhaps as a separate datatype with reduced functionality. In any case, thanks for your reply. I guess I'll stick to the filter solution and try to integrate it as a git hook in my repo. Otherwise maintaining documentation will become even more inconvenient than it is now. – vsht Feb 03 '20 at 20:58

2 Answers2

4

There is ResourceFunction["SaveReadableNotebook"]:

SaveReadableNotebook can be used to create notebook files that are well suited for version control systems that look at changes to files on a line-by-line basis.

It is better to use it with the option "ExcludedCellOptions":

ResourceFunction["SaveReadableNotebook"]["original.nb", "original_readable.nb", 
  "ExcludedCellOptions" -> {CellChangeTimes, ExpressionUUID, CellLabel}];

The formatted notebook file still opens normally:

NotebookOpen["original_readable.nb"]
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
2

One work-around (which I use) is to store your core Mathematica programming in *.m files---using a text editor to modify the files---and load them into Mathematica notebooks, as needed.

I do keep Mathematica notebooks in git as well, but looking at diffs or anything like that is hopeless.

  • Interesting idea! Regarding the original reason for my question (storing documentation notebooks for FeynCalc in git), we decided to abandon Mma documentation center and switch to Markdown at some point in the future (probably for the next release). The M2MD package by @Kuba makes it easy to produce high quality MD from .m files, e.g. as here – vsht Aug 20 '20 at 11:31
  • @vsht FeynCalc/FeynArts ... that brings back old memories! I used them back in graduate school (1990's) for something or other. – Brett van de Sande Aug 21 '20 at 02:29
  • Well, I started to contribute to FeynCalc during the grad school :) – vsht Sep 15 '20 at 08:40
  • There is a palette to compare notebooks. Not perfect but it is sure better than trying to use a regular diffs on notebooks: https://reference.wolfram.com/language/AuthorTools/tutorial/Differences.html – Gustavo Delfino Jun 15 '22 at 14:59