Skip to content

Latexdiff and git

November 7, 2010

A lot of scientists have latex as their preferred format for writing articles. A very nice tool when working on latex projects with several people is latexdiff, a perl script that based on two versions of a latex document creates a "diff document" which shows all new (text) additions in blue with curly underline, and all removals in red with a through line.

Latexdiff works well with svn and cvs repositories, where you ask latexdiff to simply compare a given file in two given revisions. However, to my knowledge this has not yet been extended to git. Hence I decided to write a bash script that works on a latex project in a git repository.

In the current form, the script compares the current version against a specified version in the repository. The specified version can be a branch, tag or a revision id. It will include any uncommitted changes as well. It does require that you are currently on a branch. It could probably be extended to work under more general circumstances, but this works fine for me.

One limitation in latexdiff is that it doesn’t work on more than one latex file. On certain big projects (like my PhD thesis), the document is written from several subdocuments using \include{} (or \input{}) statements. I added this to the bash script, so that you can specify several files that have changes you want to include in your pdf document. It actually currently does not fix \input statements since I don’t use them.

You could probably rewrite the script for e.g. mercurial rather easily. If you do, please add a link to it in the comments here.

Update: Instead of just leaving the script in pastebin I added it to gitorious, so it will be easier for you to make changes to it. You can get it with this command:
git clone git://
If you have the git plugin for Mercurial installed, you can of course use the same command:
hg clone git://

Update 2: Thanks to Paul Anton, I figured out a much better way to solve the problem. The script is now updated so that it must be set up with a command that it can use to get latexdiff in your repository, and then it will work without any need to be in a branch or similar. It does work for me using git, could someone test in mercurial and post here? If you don’t want to check out the repository, you can also have a look at it online:

Credit also goes to amonakov for giving information on how to set this up for git.

31 Comments leave one →
  1. November 11, 2010 8:33 pm

    Interestingly, getting latexdiff to work with hg is much easier. Just type the following into your .hgrc and/or hgrc file(s):

    extdiff =
    cmd.latexdiff = latexdiff

    Now, write “hg latexdiff -r42 myfile.tex” to compare the current revision to revision 42. Neat!

    • November 12, 2010 1:34 pm

      That is of course a much quicker solution! I am sure git has something similar that you can add to .git/config, I just don’t know git well enough to know exactly what/how. Strange that I don’t find any solutions when searching the web, one would imagine I am not the first person in the world that thought this would be something useful…

    • November 12, 2010 1:43 pm

      Sure enough:

      So adding
      cmd = latexdiff “$LOCAL” “$REMOTE”
      prompt = false
      ldiff = difftool -t latex

      That is quite neat, thanks for pointing me in the right direction!

  2. November 23, 2010 1:08 pm

    Hi, thanks for this nice script. The only thing I found a bit disturbing were all these question marks replacing my citations, due to the lacking diff.bib file. I therefore added two lines for a quick and dirty bibtex support:

    index ea43dec..f462860 100755
    — a/
    +++ b/
    @@ -90,7 +90,10 @@ then

    # now we generate the latex file:
    +# and the bib-file
    +bibtarget=`(basename $maindiff .tex)`
    pdflatex -interaction nonstopmode $maindiff > /dev/null
    +bibtex $bibtarget
    pdflatex -interaction nonstopmode $maindiff > /dev/null

  3. November 23, 2010 5:29 pm

    Thank you, I completely forgot about bibtex. The patch is added now. Happy to see someone found it useful! 🙂

  4. Christian permalink
    February 9, 2011 8:16 pm

    I was just about to write a script to do the exact same thing! A simple Google search and your work has saved me the effort 🙂 This is exactly what I was looking for and it works great. Thanks!

  5. February 9, 2011 9:44 pm

    @Christian Happy to help! 🙂

  6. stevenhamblin permalink
    April 1, 2011 3:06 am

    Thanks for the script, Yngve, it greatly simplified my thesis work. 🙂

    Just as a note, the bibtex command didn’t work on OS X 10.6.7 (it was erroring out with a complaint about “sed bad flag in substitute command ‘t'”), but it ended up not mattering because I just replaced the whole shebang with latexmk anyways. Never did figure out what caused the error, though…

    • April 1, 2011 7:49 am

      Happy to hear Steven! Strange this bug you mention, I have added a comment on the project wiki: Anyone are welcome to investigate and send merge requests! 🙂

    • philippe permalink
      January 30, 2015 3:13 pm

      Really nice script! Also here the bibtex part didn’t work out of the box. As suggested by Steve I am also building the pdf with latexmk instead. Works like a charm!

  7. December 14, 2011 4:12 pm

    Thanks, this works great!

    I use Mac OS X, which doesn’t have xdg-open, but it has its own open built in. I recommend removing the call to xdg-open, or at least making the script fail gracefully on systems without it.

  8. December 28, 2011 12:59 pm

    Hi Alex,

    Thanks for the feedback, happy to hear you found it useful. I am aware of the open command on OSX, I’ll see if I would manage to use that instead if you are on OSX.

    If anyone else solves it then merge requests are welcome as always.

    I fixed the link, thanks for noticing.

  9. February 23, 2012 11:07 am

    A co-worker and myself have written a Python tool to work with either git or hg repositories and latexdiff. As it works with python, it should also work on OSX or Windows, although it was written for Linux. More details are available on the TeX stackexchange post that was mentioned earlier:

    • February 23, 2012 12:14 pm

      Very interesting, thanks for the pointer! Python is a very nice language, so your implementation might very well be better than mine. Does it do batch-diff, in case you have several tex files included in your master document? That was a feature which was important for me when writing my script…

  10. March 30, 2012 4:20 am

    Nice post! I’m just about to start writing my thesis. This could prove useful for striving along the path.

  11. November 3, 2012 2:54 pm

    Hey, I split my document into multiple files using include. Shouldnt I get the diffs from main.tex and introduction.tex, when calling the script like this: main.tex introduction.tex

    It only indicates diffs from main.tex in the output PDF. In this example there are uncommitted changes in introduction.tex and main.tex. Using an older SHA which has changes shows the same misbehaviour.

    • November 3, 2012 3:01 pm

      Hello. Yes, that should give the diff from both files. I also use include and it works for me. Do you use mercurial or git? Do you get any error messages?

      • November 3, 2012 4:28 pm

        Using git and getting no error messages at all. But guess what? It works now!

        I moved files back to a subfolder ‘includes’. My previous example did not use a subfolder, as I thought the subfolder might be part of the problem.

        So, this works now: main.tex ./includes/introduction.tex

        In contrast, this does not work, although it’s the way the README suggests: main.tex includes/introduction.tex

        I guess it is a thing you could easily fix. Otherwise you could just update the README.

        Thans for the script, man! Works nicely now!

      • November 4, 2012 11:39 am

        Could it be by any chance that you have the following in your script:
        in contrast to
        (the .tex at the end is optional)
        I think this could might explain it. The “search&replace” in the script is quite simple at the moment, so I could imagine that it doesn’t check both with and without “./”.

  12. November 21, 2012 5:36 pm

    Hi, Thank you for a good idea. Though after trying to compile my main file with all of the included files with your file I ran into several problems. I tried to find what was wrong, and finally found that this cannot work without exporting/archiving the revision which one is comparing towards, due to the non changed files not being copied into the temp folder.
    To solve this I wrote a python program which can be found on
    and cloned using:
    hg clone
    It calls bibtex as well, such that the output has correct references.

    • November 22, 2012 8:31 am

      Interesting. Not sure I understand your problem yet, it could be because you use hg. That part is not very well tested yet as far as I am aware, I don’t know any users that use hg (and use git myself). I just always assumed they worked in a similar fashion…

      If anyone has any patches to fix these shortcomings you are of course more than welcome to send me them.

      • November 22, 2012 9:45 am

        I would agree that I didn’t state the problem well, so I’ll try again.
        My setup is such that I have a main file, which includes several other files. I wanted to be able to compare changes for the main file and all of the included files in one, getting out one pdf with all the changes.
        In order to do this I tried to call you script first with only my main file, this only gave me the changes in this file (as expected), I than called it with the main file, and ‘*.tex’, such that it would diff all my files, though this didn’t result in a pdf with all the changes.
        I the looked at latexdiff, which takes the optional argument –flatten, which also takes the diff for the included files (note that in my latexdiff there is a bug, such that you can’t have commented includes in the main file). I set this in my -hgrc file as an option, though I didn’t work as the extdiff tool in hg creates an folder in tmp, which only holds the changed files, and therefore latexdiff couldn’t find all of the included files. I tried it both with and without your script.
        My solution for this, is to instead of using ‘hg extdiff’ I use ‘hg archive’ into a folder, and then run latexdiff on that folder (which contains a copy of the repository at a given revision).
        I don’t know if git has the same problem, but I know that in hg diff is optimised for speed and therefore doesn’t copy over unchanged files.

        Hope this was a better explanation

  13. March 26, 2013 2:38 pm

    Hi, I have a real beginners question: How do I execute the “”? Do I use the Windows Commandtool CMD or what else? The provided examples could not help. I have perl installed.

    • March 28, 2013 12:56 pm

      This script is using extensively unix tools. On Windows I do not believe you have e.g. ‘pdflatex’ or ‘bibtex’ installed and available in the shell. Hence I do not believe it will work. If anyone feels like writing the script so it works also on Windows feel free to submit patches. I do not have a Windows machine available, nor the need to develop this script for Windows myself. Sorry.

      • March 28, 2013 1:56 pm

        It probably will under Cygwin, assuming you’ve got all those tools (sed, bash, pdflatex, bibtex, etc) on your %PATH%. Also, you will need to make an if/else clause replacing the “xdg-open” command with “cygstart”.

  14. March 31, 2013 1:32 am

    Great work!
    Also, for brazilians or other accent-like languages with bibtex errors

    “Warning–I didn’t find a database entry for …”

    your .bib files cannot contain direct accents, e.g. “é”, “ó”, replace with “\’e”, “\’o”

    Took me a half an hour to find out… =)


  1. Writing the PhD thesis: the tools Part I | Between the Candle and the Star
  2. Making the diff-erence in using LaTeX – WeST Blog (University of Koblenz-Landau) | WeST Blog
  3. Using latexdiff with git | DL-UAT

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: