8

Any one knows a tool to find difference between 2 notebooks at the source level?

The compare notebooks tool in workbench 2 seems to work at the internal data structure level which is not useful for me. I am looking for tool that looks at differences at the source level (what one sees when looking at a notebook, i.e. not the FullForm).

I am using V8 of Mathematica on windows.

EDIT1:

How I display the output/report from NotebookDiff in a more readable form?

enter image description here

Nasser
  • 12,849
  • 6
  • 52
  • 104
  • 2
    By "at the source level" do you mean at the raw text level? If so then you can use normal diff tools provided you've turned off `FileOutlineCache` and `TrackCellChangeTimes` which make too much noise. There are also scripts that will remove these things before the diff (without modifying the notebooks). See [Version control of Mathematica notebooks](http://stackoverflow.com/q/2816628/421225) and the comments in the [accepted answer](http://stackoverflow.com/questions/2816628/version-control-of-mathematica-notebooks/2819800#2819800). – Simon Jun 30 '11 at 00:36
  • 1
    I mean just the source code I look at on the screen. Normal source code. Not raw text or FullForm or any of that. I am trying to find what code changes are made between 2 versions of the notebook. How would one do that in Mathematica? Workbench compare notebooks looks at raw data, which is hard to read. – Nasser Jun 30 '11 at 00:43
  • Oops, I should have read closer. You only what differences in "what you see" which is a vague concept and a different kettle of fish. Maybe you should try [NotebookDiff](http://reference.wolfram.com/legacy/v5_2/Add-onsLinks/AuthorTools/ReferenceGuide/NotebookDiff/) from the old `AuthorTools` package. – Simon Jun 30 '11 at 00:45
  • BTW, I have used Git for version control of notebooks (which includes checking diffs every so often) following all of the tips in the link in my first comment and it worked fine for me. – Simon Jun 30 '11 at 00:46
  • AuthorTools is no longer sold. I looked at the link you had there, followed the link to the perl script, but it lead to nowwhere I am afraid. Thanks – Nasser Jun 30 '11 at 01:04
  • @Nasser: `AuthorTools` was never "sold" and is still part of Mathematica. See the code given in [Michael's answer](http://stackoverflow.com/questions/6528798/how-to-diff-2-notebooks-at-the-source-level/6529298#6529298). – Simon Jun 30 '11 at 05:26
  • @Nasser: I've fixed the link to [nbcache](http://www2.itwm.fhg.de/as/asemployees/wichmann/nbcache.html). – Simon Jun 30 '11 at 05:35

3 Answers3

11

This answer is based on discussion in the comments to other parts of this question. It also could (and should) be automated if it's going to be used with any regularity. This could be done by tagging the cells you want compared and using NotebookFind to find the cells for extraction and comparison.


A solution for comparing just a single large cell of code (as sometimes occurs when makeing demonstrations) is to copy the code in InputForm from both notebooks

enter image description here

and paste it into a simple diff tool such as Quick Diff Online which will then display the standard diff for you:

enter image description here

The above code was taken from one of Nasser's demonstrations.


Another option is to use CellDiff from the AuthorTools package.

Needs["AuthorTools`"];
CellDiff[Cell["Some text.", "Text"], 
         Cell["Some different text.", "Text"]]

CellDiff

To use on your demonstrations you can copy the cell expressions from the two versions by right clicking on the cell brackets:

enter image description here

Simon
  • 14,631
  • 4
  • 41
  • 101
9

There is an undocumented package in the built-in add-ons (in $InstallationDirectory/AddOns/Applications) called AuthorTools. Once loaded, it exposes a NotebookDiff function which provides some basic diff features:

Needs["AuthorTools`"];

nb1 = NotebookPut[
  Notebook[{Cell["Subsection heading", "Subsection"], 
    Cell["Some text.", "Text"]}]];

nb2 = NotebookPut[
  Notebook[{Cell["Edited Subsection heading", "Subsection"], 
    Cell["Some different text.", "Text"]}]];

NotebookPut@NotebookDiff[nb1, nb2]

As this package is undocumented, please realize it is potentially buggy and is not considered a supported feature, but hopefully you still find it useful.

Note that you can also get handles to notebooks with e.g.:

nb1 = NotebookOpen["path/to/a/notebook.nb"]

and a list of notebooks currently open in the front end

Notebooks[]
Simon
  • 14,631
  • 4
  • 41
  • 101
Michael Pilat
  • 6,480
  • 27
  • 30
  • I tried the method you show above. What do I need to do to read the result in some readable form? Please see screen shot in my EDIT 1. – Nasser Jun 30 '11 at 02:40
  • 2
    @Nasser: Wrap the call to `NotebookDiff` with `NotebookPut`. – Brett Champion Jun 30 '11 at 03:09
  • Thanks Brett. I see it now. But result is not useful at all. I have 2 different versions of the notesbook, all the code is contained in one cell (this is a demo, so Manipulate code must be in one cell). When I get the report, and click on 'view Difference' for the cell, all what it did, is display side-by-side the code of that one large cell from each notebook, but no indication where the differences are! No red-lines, or other indication as in other diff tools, to tell me where the differences, even though I can see some difference. It was just saying the cells are different. But not where! – Nasser Jun 30 '11 at 04:13
  • @Nasser: If everything is in one cell (which is not necessary for a demo) then a plain text diff would be more than enough. You could even use an [online one](http://www.quickdiff.com/). Of course you would need to copy your big `Manipulate` as `InputText` - but that should be no big deal. – Simon Jun 30 '11 at 05:23
  • @Simon, thanks for the link to the diff web page. I tried it, and it actually worked as I wanted. I copied the cell content and paste it there from both notebooks, and it did show the difference using colors. I think I will use that from now on. Easy to use also. – Nasser Jun 30 '11 at 05:43
  • @MichaelPilat I remember you had signed up for the old mma proposal. That got shut down for strange reasons, so we started a [new proposal](http://area51.stackexchange.com/proposals/37304/mathematica?referrer=hWeRiD9Qz0oIDM_9uBjtlA2) and it has nearly twice as many committers as the old one in less than a third of the time. It would be great if you could commit to it too – we can probably get the site going by the end of this month... – abcd Jan 08 '12 at 16:47
  • @yoda - Gladly, I just committed to the new proposal! – Michael Pilat Jan 10 '12 at 02:52
2

If you must work with notebooks then NotebookDiff in AuthorTools is probably your best bet. If this is an important part of your workflow (due to version control or some other constraint) and you have some flexibility you may want to consider moving the code from the existing notebook (.nb) into a package file (.m), which will be saved as plain text. You can still open and edit package files in the Mathematica notebook front end, but you get the added benefit of being able to diff them using existing text diffing tools.

ragfield
  • 2,486
  • 13
  • 15