Diff

Diff[first,second]

returns a representation of the diffs between first and second.

Diff[first,second,format]

represents the diffs in the indicated format.

Details and Options

  • Diff produces results for the following types of expressions:
  • lists{}returns differences between two lists
    text"text", File["fname.txt"]returns differences in individual characters, words or lines between two blocks of text
    biomolecular sequencesBioSequence[]returns differences in letter strings between two biomolecular sequences
    cellsCell[], CellObject[]returns differences in content, styles and options between two cells
    notebooksNotebook[], NotebookObject[], File["fname.nb"]returns differences in cells and options between two notebooks
  • Diff returns a DiffObject.
  • Diff uses SequenceAlignment when comparing sequences of elements.
  • Diff supports all the options of SequenceAlignment, with the following additions:
  • DiffAlignmentMethod Automatichow to align the diff
    DiffGranularity Automaticthe granularity of the diff
    DiffIgnore {}list of data types that should be ignored
    DiffIncludeMatches Automaticwhether to include matching data in the resulting DiffObject
    DiffInputFunction Identitya function to preprocess the diff inputs
  • The visual display of the Diff output can be controlled with the following options:
  • Alignment{Automatic, Automatic}hwo to align objects in the display area
    AppearanceAutomaticthe overall appearance of the diff
    AppearanceElementsAutomaticwhat elements to include in the displayed diff
    DiffStyle<||>base style specifications for the diff
    ImageSizeAutomaticthe overall image size of the diff
    ImageSizeActionAutomaticwhat to do if the content size does not match the image size
    ScrollbarsFalsewhether to include scrollbars
    ScrollPosition{0,0}scrollposition if scrolling is enabled
  • When diffing text, DiffGranularity can be given settings of "Lines", "Words" or "Characters" to encode the differences into the resulting DiffObject accordingly. By default, if you are comparing two strings, you will get a "Characters" diff. If either of the objects is a text file, you will get a "Lines" diff.
  • When comparing notebooks, Diff uses extra knowledge about the structure of cells to determine how to align the cell lists of the two notebooks. By default, it looks at the ExpressionUUID of a given pair of cells for alignment. To align the cell lists by only matching the edit distances between cells, use DiffAlignmentMethod"CellEditDistance".
  • Diff[first,second] is equivalent to Diff[first,second,"DiffObjectViewer"], which produces a viewer with a popup menu allowing you to interactively select between different formats. Other values of format display as the given format, without the ability to interactively change the view type.
  • Some values of format display first and second explicitly, with inline annotations indicating the changes between the two:
  • "CharacterAlignment"for strings or biosequences, display individual characters of first and second vertically aligned with one another
    "Combined"display a combined version of first and second
    "Separate"display the list {first,second}
    "SideBySide"display elements of first and second in aligned, side-by-side columns
    "Unified"display elements of first and second in a single, unified column
  • Some values of format use abstract visualizations to depict the nature and extent of the changes between first and second:
  • "AlignmentChart"display a chart with elements of first and second depicted as columns of points with connecting lines to indicate how the elements align with one another
    "ArrayPlot"display a combined list of elements in an array plot, with colored array cells annotating the changes
    "SideBySideSummary"display first and second as separate sequences of colored array cells with long, unchanged sequences elided
    "UnifiedSummary"display first and second as a combined sequence of colored array cells with long, unchanged sequences elided
  • Other acceptable values of format include:
  • "Details"produce a textual description of each change
    "DiffObjectViewer"display in a panel with an interactive format setting
    "Report"create a notebook containing a report of the differences between two notebook expressions
    "SummaryBox"display as a DiffObject summary box
  • Some values of format will only work with a subset of expression types.
  • Diff[first,second][format] is equivalent to Diff[first,second,format].

Examples

open allclose all

Basic Examples  (3)

Find the diffs between two lists:

Find the diffs between two strings:

Format the DiffObject as a plain summary box:

Scope  (5)

Treat strings as a sequence of words instead of characters:

Compare two lists without regard to order or duplicates:

Find the lines where two text files differ:

Find the diffs between two cells:

Find the diffs between two notebooks:

Options  (15)

Appearance  (1)

Specify an initial appearance for the result:

AppearanceElements  (1)

The "Unified" format shows line numbers by default:

Remove the line numbers from AppearanceElements:

DiffAlignmentMethod  (1)

When a notebook contains multiple differing cells in a row, Diff attempts to find the best alignment:

Turning that feature off with DiffAlignmentMethod"SequenceAlignment" will result in two blocks of cells marked as a single change, without inner alignment:

DiffGranularity  (1)

By default, strings are compared as a sequence of characters:

Use DiffGranularity to treat strings as a sequence of lines instead:

DiffIgnore  (2)

When comparing cells, Diff will find style and option changes:

Use DiffIgnore to ignore all style and option changes:

Use DiffIgnore to ignore specific notebook options:

DiffIncludeMatches  (3)

When there are lots of matching elements, Diff does not include them all in the output:

Use DiffIncludeMatchesTrue to store all the matching elements:

DiffIncludeMatches also has an impact on the DiffObject for strings:

Displaying a diff will typically show all the matches included in the DiffObject:

DiffInputFunction  (2)

Compare the ExpressionCell rendering of arbitrary expressions:

Compare the InputForm strings of arbitrary expressions:

DiffStyle  (1)

Diff formats typically use green for additions and red for removals by default:

Use DiffStyle to add styling to particular elements:

Remove the default styling with the setting DefaultDiffStyle None:

ImageSize  (1)

Specify a fixed height for the formatted content:

ImageSizeAction  (1)

Automatically shrink the display to fit in a fixed size:

Scrollbars  (1)

Set Scrollbars Automatic to show scrollbars when necessary:

Properties & Relations  (5)

Ask a DiffObject for the list of available properties:

Extract a particular property:

Ask a DiffObject for the list of available formats:

Display a particular format:

Change the initial format of a DiffObject:

Return a visualization directly:

Given two lists, Diff and SequenceAlignment will find the same changes but represent them differently:

Extract the SequenceAlignment from the DiffObject:

Possible Issues  (2)

Diff compares strings as strings, even if they could be interpreted as file paths:

To compare the contents of the files instead, use File explicitly:

Diff will not attempt to compare objects of different types:

Neat Examples  (1)

Diff every pair of files in a list:

Wolfram Research (2024), Diff, Wolfram Language function, https://reference.wolfram.com/language/ref/Diff.html.

Text

Wolfram Research (2024), Diff, Wolfram Language function, https://reference.wolfram.com/language/ref/Diff.html.

CMS

Wolfram Language. 2024. "Diff." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Diff.html.

APA

Wolfram Language. (2024). Diff. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Diff.html

BibTeX

@misc{reference.wolfram_2024_diff, author="Wolfram Research", title="{Diff}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/Diff.html}", note=[Accessed: 18-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_diff, organization={Wolfram Research}, title={Diff}, year={2024}, url={https://reference.wolfram.com/language/ref/Diff.html}, note=[Accessed: 18-December-2024 ]}