Function Repository Resource:

HighlightTextDifferences

Source Notebook

Highlight the changes between two similar strings

Contributed by: Jon McLoone

ResourceFunction["HighlightTextDifferences"][before,after]

displays a string highlighting differences between before and after.

Details and Options

By default text removed from before is shown in red and text added to create after is shown in green.

Examples

Basic Examples (2) 

Insertions into the text are highlighted in green:

In[1]:=
ResourceFunction[
 "HighlightTextDifferences"]["This is some text", "This is some changed text"]
Out[1]=

Words removed from the text are displayed in red with a strikethrough:

In[2]:=
ResourceFunction[
 "HighlightTextDifferences"]["This is some text with words removed", "This is some text"]
Out[2]=

Scope (2) 

Non-text expressions are converted to InputForm strings:

In[3]:=
ResourceFunction["HighlightTextDifferences"][x^2 + 1, x + 1]
Out[3]=

For a comparison based on the expression structure, use the resource function ExpressionLineDiff:

In[4]:=
ResourceFunction["ExpressionLineDiff"][x^2 + 1, x + 1]
Out[4]=

Options (1) 

The styles for inserted and deleted text can be changed:

In[5]:=
ResourceFunction[
 "HighlightTextDifferences"]["This is some text with some words removed", "This is some text and extra words",
 "AdditionStyles" -> Bold, "RemovalStyles" -> {FontSize -> 8, FontSlant -> Italic}
 ]
Out[5]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 13 January 2023

Related Resources

License Information