How to diff 2 notebooks at the source level? - wolfram-mathematica

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?

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
and paste it into a simple diff tool such as Quick Diff Online
which will then display the standard diff for you:
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"]]
To use on your demonstrations you can copy the cell expressions from the two versions by right clicking on the cell brackets:

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[]

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.

Related

Can I use \input{file.tex} or similar to efficiently bring content (not a whole document) from a LaTeX file into a Jupyter notebook?

I am new to Jupyter notebooks and Python and have started using these to make materials for a workshop so that I can also produce a handout. I have useful content in various LaTeX files that I would like to include in a notebook.
I would like to know whether there is a command that will allow me to efficiently bring my already modularized content into a notebook. I will be happy to take suggestions on any approach to my problem in case the LaTeX route is the wrong way to proceed.
As a particular case, suppose an external file fig.tex has only a stand-alone tikzpicture (that I have successfully included in another LaTeX document). If I start a notebook code cell with
%%itikz and follow with \input{fig} I obtain error messages.
I can remedy the problem if I add a preamble with \documentclass{}, many necessary \usepackage{} and \usetikzlibrary{} commands (which I have already included at the top of the notebook), and wrap the content with begin/end document commands.
There is more manual handling here than I would like. Is there a more efficient way to include the tikzfigure content?
So it turns out with itikz you have an --implicit-pic option that fills in the preamble for you.
In principle, with this option, your cell would look like the following:
%%itikz --implicit-pic
% my awesome figure
\input{path/to/fig}
This creates a tex file populated like so:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.0]
% my awesome figure
\input{path/to/fig}
\end{tikzpicture}
\end{document}
In addition when using an implicit-pic it is usefull to load tikz packages and set options . To quote from the Quickstart guide:
In an --implicit-pic, it's often useful to:
Set the \tikzpicture[scale=X] via --scale=<X> while iterating.
Set the \usepackage{X,Y,Z} via --tex-packages=<X,Y,Z>
Set the \usetizlibrary{X,Y,Z} via --tiz-libraries=<X,Y,Z>
For more info, see items 16-20 in the Quickstart notebook.

How to get list of figures in Asciidoc

I am using asciid for an article. In the end of my document I want to have a list of figures. How to I create a list of figures? Did not find something useful in the documentation for me.
Nope there isn't one at the time of answer. I checked the docs (which you indicated you did as well) and I also grepped the codebase. There is good news though! You should be able to do this with an extension.
Extensions can be written in any JVM language if you're using asciidoctorj, or in Ruby if you're using the core asciidoctor (I'm not sure about JavaScript for asciidoctorjs). You'll need to create two extensions probably: a TreeProcessor extension to go through the whole AST looking for images and pulling them out into a storage structure. Then you'll also need to create either an inline or block macro to actually place it within the page.
I strongly recommend examining the API for the nodes and functions you'll want to make use of. There are some other examples of processors that may also be helpful to examine.

Steps to compare notebooks in Workbench

What, exactly, are the steps involved in using Wolfram Workbench (version 2) to compare two notebooks?
Please be explicit even in such things as what I do in order to open the two notebooks in Workbench.
(I find Workbench fiendishly difficult to use. Its built-in documentation is, I find, of limited value. The tutorial screencasts about it are just too rushed to be able to follow, even with stopping and starting. And there's still a confusion, at least for me, among various versions of sample files that are to accompany the tutorial.)
I have never used the workbench but when I fired it up I was comparing documents in minutes, so it can't be that hard.
The steps:
Create a project using the File > New > New Project menu (EDIT: many project types will do I think. I have tried both the Application and the Basic Mathematica type)
Add the two files to be compared to the project using import (in the File menu or the right mouse button context menu). EDIT: The best approach is probably using "General/File system". It will ask for a directory to import from. After that you may select the files you want to import from that directory.
Select both files in the package explorer view
Right mouse button > Compare with> Each other
Compare editor fires up showing the two documents side by side
(Welcome to StackOverflow Murray, it's good to see you here!)
This is not a direct answer to your question, but I think you might find it useful to know that the << AuthorTools` package includes functionality for comparing notebooks. Evaluate the following to open the ("hidden") documentation:
NotebookOpen#
FileNameJoin[{$InstallationDirectory, "AddOns", "Applications",
"AuthorTools", "Documentation", "English", "AuthorToolsGuide.nb"}];
Then you can compare them using CreateDocument#NotebookDiff[notebook1, notebook2], or perhaps using the somewhat more convenient
CreateDocument#NotebookDiff[SystemDialogInput["FileOpen"], SystemDialogInput["FileOpen"]]
which will let you select the files more easily.
Alternatively, you can open the two notebooks in the front end, and use this little control panel:
Dynamic#Column[
{PopupMenu[Dynamic[nb1],
Thread[Notebooks[] -> NotebookTools`NotebookName /# Notebooks[]]],
PopupMenu[Dynamic[nb2],
Thread[Notebooks[] -> NotebookTools`NotebookName /# Notebooks[]]],
Button["Show differences",
CreateDocument#NotebookTools`NotebookDiff[nb1, nb2]]}
]
It's interesting to mention that in Mathematica 8 there's some undocumented notebook-related functionality in the NotebookTools context, including a NotebookTools`NotebookDiff[] function.
Related question: Is it possible to invoke Mathematica's diff functionality from the command line?

Eliminating code duplication in a single file

Sadly, a project that I have been working on lately has a large amount of copy-and-paste code, even within single files. Are there any tools or techniques that can detect duplication or near-duplication within a single file? I have Beyond Compare 3 and it works well for comparing separate files, but I am at a loss for comparing single files.
Thanks in advance.
Edit:
Thanks for all the great tools! I'll definitely check them out.
This project is an ASP.NET/C# project, but I work with a variety of languages including Java; I'm interested in what tools are best (for any language) to remove duplication.
Check out Atomiq. It finds code that is duplicate that is prime for extracting to one location.
http://www.getatomiq.com/
If you're using Eclipse, you can use the copy paste detector (CPD) https://olex.openlogic.com/packages/cpd.
You don't say what language you are using, which is going to affect what tools you can use.
For Python there is CloneDigger. It also supports Java but I have not tried that. It can find code duplication both with a single file and between files, and gives you the result as a diff-like report in HTML.
See SD CloneDR, a tool for detecting copy-paste-edit code within and across multiple files. It detects exact copyies, copies that have been reformatted, and near-miss copies with different identifiers, literals, and even different seqeunces of statements.
The CloneDR handles many languages, including Java (1.4,1.5,1.6) and C# especially up to C#4.0. You can see sample clone detection reports at the website, also including one for C#.
Resharper does this automagically - it suggests when it thinks code should be extracted into a method, and will do the extraction for you
Check out PMD , once you have configured it (which is tad simple) you can run its copy paste detector to find duplicate code.
One with some Office skills can do following sequence in 1 minute:
use ordinary formatter to unify the code style, preferably without line wrapping
feed the code text into Microsoft Excel as a single column
search and replace all dual spaces with single one and do other replacements
sort column
At this point the keywords for duplicates will be already well detected. But to go further
add comparator formula to 2nd column and counter to 3rd
copy and paste values again, sort and see the most repetitive lines
There is an analysis tool, called Simian, which I haven't yet tried. Supposedly it can be run on any kind of text and point out duplicated items. It can be used via a command line interface.
Another option similar to those above, but with a different tool chain: https://www.npmjs.com/package/jscpd

General Purpose Filter As You Type (aka typeahead, Incremental find, autocomplete) is it out there?

Background
Lately I've become a fanatic that everything I type while working on a computer should be compatible with "DRY". If there's anything I have to type more than once in any context, I want some kind of user-aware auto-complete option to do some of the work for me -- always -- no exceptions.
Having to work under Windows, I've looked at GUI solutions to make this insane goal a reality.
The (almost) optimal solution
If you have a moment, open up Firefox 3.0 and type a few keystrokes into the address bar. You will notice that it performs a kind of Incremental Autocomplete based on space-separated sub-strings of whatever you type. Another place in Firefox that does something similar is the about:config URL.
This is sub-optimal, because I don't want this in Firefox only. I want to use this everywhere.
The Question
Does anyone out there know of a widget or app that does nothing but insanely good incremental auto-complete that can be used as a general purpose "run everywhere" tool? Something that allows the user to: 1) maintain one or more "completion candidate files"; 2) pick one of those files as the source for Firefox 3.0 style completion; 3) return the result (or blank if the user canceled), and do those three things only?
Details
Here's how it should work:
STEP1: user saves or more csv file(s) (or other easy-edit format) somewhere in his hard-drive
STEP2: user creates a Windows Script Host script or a batch file (or whatever) instantiates the FilterAsYouType GUI
STEP3: user runs the script file, and the script file instantiates the GUI, telling it which CSV file to use as the source of all potential completions
STEP4: the user either chooses one of the completions, supplies his own text that is not in the list, or cancels out without supplying anything
STEP5: when the user is done the script saves the result to a variable and does something with it
Here is some pseudo-code for the script:
include "GenericTypeaheadWidget";
var gengui = new GenericTypaheadWidget('c:\docs\favorite_foods.csv');
var fave_food = gengui.get_user_input();
if(fave_food != ''){
alert('you chose '+fave_food+'!');
}
The rationale
The goal is to just have a way to always be able to do auto-completions from a list of arbitrary items, even if the list is a couple thousand items, and not have to rely on it being built into some IDE or standalone application that only accepts certain kinds of input or has an overly-complicated API relative to the simplicity of this task.
CSV (or text or sqlite database) would provide a way for me to self-generate "candidate lists" or "history logs" and then just use those logs as the source of the possible completions.
The disclaimer
I've tried several GUI "launcher" programs, command-line engines like power-shell and scripting shells, the regular plain old command-line history with varying degrees of satisfaction. The problem with these is they all do extra superfluous stuff like searching directories or built-in commands. I just want nothing but whatever is in the CSV file I happen to be pointing at.
I'm wondering if there is any simple tool that does nothing but what I'm describing above.
UPDATE: It looks like this question is very closely related to Graphical Command Shell, which captures the essential idea presented here.
You should really try Launchy - it's exactly what you're looking for, a "run anything" with intelligent autocompletion. It completely changes the way you interact with a Windows PC.
And it has open source-code, so you can borrow its autocompletion code if you want to roll your own interface.

Resources