Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

DiffTools

Guides

  • Diff Tools

Tech Notes

  • Comparing Notebooks
  • Introduction to DiffTools

Symbols

  • AlignCellLists
  • BaseDiffStyle
  • Diff3
  • DiffApply
  • DiffGranularity
  • DiffInputFunction
  • DiffInvert
  • Diff
  • DiffObject
  • DiffView
  • IgnoreElements
  • IncludeMatches
  • MoreMatches
Introduction to DiffTools
Diff
Cells and Notebooks
DiffObject
Two-way and Three-way Diffs
DiffView
​
The Wolfram/DiffTools paclet provides features for finding, managing, and viewing changes between various types of expressions.
Load the paclet:
In[1]:=
Needs["Wolfram`DiffTools`"]
Diff
The main function in the Wolfram/DiffTools paclet is
Diff
. When comparing two expressions,
Diff
[
expr
1
,
expr
2
]
will automatically find the differences between them, and prepare a result to express those changes visually. Note that not all expressions are currently supported by
Diff
. More about that below.
Use
Diff
to compare two strings:
In[2]:=
Diff
["This is a test","This is another example"]
Out[2]=
The output of
Diff
is a formatted
DiffObject
, which is a computable object that happens to display as a interface for "browsing" the diff. There are a variety of formats available for viewing a
DiffObject
, most of which are accessible in this output. You can go straight to a particular format by specifying the name of that format as the 3rd argument to
Diff
. In the case of string comparisons, other formats include
"Separate"
and
"CharacterAlignment"
.
Compare two strings, and do not combine the strings in the result:
In[3]:=
Diff
["This is a test","This is another example","Separate"]
Out[3]=
{
This is a
te
st
,
This is a
no
t
h
e
r example
}
Compare two strings, and show how the characters align:
In[4]:=
Diff
["This is a test","This is another example","CharacterAlignment"]
Out[4]=
The
Diff
function takes options as well. The
DiffGranularity
option indicates which elements of the given expressions should be the focus of the diff. For example, when comparing two strings, they are by default compared character by character. But by providing a granularity of
"Words"
, you can change that behavior.
Compare the words in two strings:
In[5]:=
Diff
"This is a test","This is another example",
DiffGranularity
"Words"
Out[5]=
Note that not all expression heads are supported by
Diff
at this time. Supported heads include:
List
,
String
,
BioSequence
,
File
,
Cell
,
CellObject
,
Notebook
, and
NotebookObject
.
Of course, strings and cells can be used to contain arbitrary expressions. So if you have expressions with heads that aren't among those explicitly supported, coercing them to one of the supported expression types can be done with the
DiffInputFunction
option. The setting for that option is taken to be a function to be applied to the given inputs before they are diffed.
Compare the
InputForm
strings of two expressions:
In[6]:=
Diff

1
1-
3
x
x,
1
1-
3
y
y,
DiffInputFunction
(ToString[#,InputForm]&)
Out[6]=
DiffInputFunction
also supports a small number of string settings, specifying common operations, like coercing the expressions to their
InputForm
strings.
Use the built-in utility to coerce the expressions to
InputForm
:
In[7]:=
Diff

1
1-
3
x
x,
1
1-
3
y
y,
DiffInputFunction
"InputForm"
Out[7]=
Compare the
ExpressionCell
rendering of two expressions:
In[8]:=
Diff

1
1-
3
x
x,
2
1-
3
x
x,
DiffInputFunction
ExpressionCell
Out[8]=
DiffObject
The high-level
Diff
function conveniently combines finding and viewing of differences. In practice, that is often exactly what you want. But there may be times when you want to compare two expressions once, and view those changes in multiple different ways. Or perhaps you want to store such changes in a computable form, that you can then use to transform other expressions.
In fact, the expression typically returned by the
Diff
function is a
DiffObject
. This computable object can be visualized in different ways by the
DiffView
function, and can be used to transform expressions via
DiffApply
.
DiffObject
a symbolic representation of those changes
DiffView
display the changes from a given
DiffObject
DiffApply
apply the changes from a
DiffObject
to an expression
Low-level functions in Wolfram/DiffTools.
Diff
[
expr
1
,
expr
2
,"SummaryBox"]
returns a more conventional rendering of the resulting
DiffObject
.
Find the changes that would transform one string to another:
In[9]:=
obj=
Diff
["This is a test","This is another example","SummaryBox"]
Out[9]=
DiffObject
Type: Characters
Summary: 1 addition, 11 edits

We won't document the structure of a
DiffObject
here, but one can ask an object for its available properties in the usual way:
List all available properties:
In[10]:=
obj["Properties"]
Out[10]=
{Type,Data,Changes,Matches,Additions,Removals,Edits,Conflicts,TypesetElements,SummaryString,LabeledChanges,MetadataInfo}
Extract a summary:
In[11]:=
obj["SummaryString"]
Out[11]=
1 addition, 11 edits
You can view a
DiffObject
in various formats with the
DiffView
function. More about that in the next section.
View the diffs represented by a given
DiffObject
:
In[12]:=
DiffView
[obj]
Out[12]=
This is a
no
t
h
e
st
r example
Produce a non-default view:
In[13]:=
DiffView
[obj,"Separate"]
Out[13]=
{
This is a
te
st
,
This is a
no
t
h
e
r example
}
This
DiffObject
contains information about what string operations would be required to transform the original string into the new string. In fact,
DiffApply
can take a
DiffObject
along with the original string and do just that:
Apply a
DiffObject
to the original string:
In[14]:=
DiffApply
[obj,"This is a test"]
Out[14]=
This is another example
Note that
DiffApply
knows that the
DiffObject
for a string stores string positions for each change. That is what allows
DiffApply
to work on the original string. That also means it is possible to apply such changes to other strings. Of course, that is not always a sensible thing to do:
Apply changes to a string with the same initial segment as the original:
In[15]:=
DiffApply
[obj,"This is a test of DiffApply."]
Out[15]=
This is another example of DiffApply.
Corrupt a string by applying changes that were not intended for it:
In[16]:=
DiffApply
[obj,"If you think this is a test, you're mistaken."]
Out[16]=
If you thnonhkr examplehis is a test, you're mistaken.
An occasionally useful operation one can perform on a
DiffObject
is to reverse it. If
obj
is the
DiffObject
for turning
expr1
into
expr2
, then
DiffInvert
[obj]
will return a
DiffObject
for turning
expr2
into
expr1
.
Apply reversed changes to the second string:
DiffView
Find the changes from one list to another:
View those changes in a non-standard format:
View those changes in different formats:
The available formats depend on how the expressions were compared. As we've already seen, a string comparison supports combined or separated views. But if you want to view the changes as line-by-line, then computing the changes should focus on lines to begin with.
Focusing on different elements can produce different types of changes:
Cells and Notebooks
The Wolfram/DiffTools paclet also includes functions that can find and visualize differences between cells and notebooks. This is especially useful when working with notebook files in version control systems like CVS or GIT, where a textual comparison of the ASCII contents of notebook files is often difficult to visually parse.
When viewing the changes between notebooks, the default behavior is to display a summary box, with the option of viewing a so-called report notebook, which includes information about which cells have been added, removed, or edited.
Open a report describing the diffs:
Ignore all changes in cell options:
Compare notebook files:
Two-way and Three-way Diffs
In addition to comparing one expression against another, Wolfram/DiffTools includes some support for so-called 3-way diffs. This is useful when you're interested in taking two different edits from a common starting point, and seeing if those edits can be cleanly merged with one another. When they can't -- for instance, if both edits change the same piece of the original in incompatible ways -- then human judgement may be needed to resolve the conflict.
Compute and visualize a simple 3-way diff:
A 3-way diff with additions and removals:
Some 3-way diffs may have conflicting changes:

© 2025 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com