How to Use latexdiff to Mark Differences Between Two Versions of the LaTeX File?

Includes PGF, TikZ, PSTricks, and platforms/distributions: Kile, TeXstudio, TeXmaker, TeXnicCenter, MiKTeX, LyX
Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

When writing any document, it is always a good practice to keep track of all major changes. This is important for the purpose of record-keeping, and especially, very useful for collaborative writing, such as in most scientific papers. There are many reasons which may cause a writer to revert on the previous version of the document. Here we want to show, how to visually mark differences between two versions of the LaTeX document, and create a new LaTeX document with markup differences between the two latex files. Ultimately compiling the resulting TeX file to a pdf document will highlight the marked changes.

This approach uses a very powerful command line and takes care of changes in the main latex document, including subdirectories/subfiles, that's even if the document is split into multiple .tex files. We will use latexdiff, a Perl script, and thus it requires we make installation of at least Perl 5.8 or higher. You can download and install Perl from here.


To get latexdiff, open your terminal, and issue the commands below to install it:

  1. sudo apt-get update
  2. sudo apt-get install latexdiff


To realize the usage of latexdiff, let's create two directories (LaTeX_dir_1 and LaTeX_dir_2) on the Desktop, each containing the main LaTeX document, respectively, named TeX_file_1.tex and TeX_file_2.tex, where there is the inclusion of sub-files via \input{file.tex} command to these main LaTeX documents, see attached. We will run latexdiff and see which changes were made to the TeX_file_2.tex in the LaTeX_dir_2 folder, and show the differences in diff.tex, as follows below.

Move to the first folder in the Desktop:

  1. cd Desktop/LaTeX_dir_1/


Compare LaTeX files and create the file diff.tex in the folder LaTeX_dir_1 with differences:

  1. latexdiff --append-safecmd=subfile TeX_file_1.tex /home/username/Desktop/LaTeX_dir_2/TeX_file_2.tex --flatten > diff.tex


The difference document produced looks like:
Remember to replace "username" above by the appropriate name of your machine. Two directories with test LaTeX files (taken from here) are attached.
Attachments
LaTeX_dir_1.zip
(1.24 MiB) Downloaded 56 times
LaTeX_dir_1.zip
(1.24 MiB) Downloaded 56 times
LaTeX_dir_2.zip
(796.17 KiB) Downloaded 59 times
LaTeX_dir_2.zip
(796.17 KiB) Downloaded 59 times
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “LaTeX and MathJax”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest