LaTeX packages for use with revision control

Yes, there are packages called svn and svninfo, as well as a few for other version-control systems. See the UK TeX FAQ: you can have something like \SVNdate $Date$ which will use the value of the Date SVN variable in your document, or have footers containing the date and version number in the document. I don't think either of them shows diffs, though.


For systems like CVS or Subversion, which modify source files, the TeX FAQ's bare-bones answer is my favourite:

\def\RCS$#1: #2 ${\expandafter\def\csname RCS#1\endcsname{#2}}
\RCS$Revision: 1.13 $ % or any RCS keyword
\RCS$Date: 2010/04/02 18:20:00 $
...
\date{Revision \RCSRevision, \RCSDate}

It's not very sophisticated, but you're only going to use it for draft versions, so this is a case where simple and robust beats pretty.

Systems like Mercurial or Git don't modify source files. In such cases, a good solution is to generate a file which can be \input into a source document. In the case of Mercurial, I use a Makefile rule like:

hg.id: .hg
    hg log -r'p1()' --template '\\def\\HgNode\{{node|short}}\n\\def\\HgDate\{{date|rfc3339date}}\n\\def\\HgAuthor\{{author|person}}\n' >$@

I \input that file into the document, and use \HgDate and \HgNode in \date commands or footnotes.

Update: the OP also mentioned changes between versions. That turns out to be a bit tricky in LaTeX, but the TeX FAQ has a discussion of the various (not completely satisfactory) possibilities.

Edit: More recent versions of Mercurial require the { characters in the template to be escaped. I modified the answer to include that.

Edit: hg parent is better than hg tip, as it gives the correct result if you're formatting an old (tagged?) version of the repository. But (a further edit) hg log -r'p1()' is better than either, since it will produce a single result even in the occasional case when a working directory has two parents, such as when you are mid-merge and resolving merge conflicts.


For the last part of your question, I like the Perl script latexdiff. This generates LaTeX documents displaying linebreak-insensitive differences, with changebars and other visual markup. (Alas, the implementation is quite hacky, relying on complex regular expressions for parsing, so it's worth ensuring one has Perl 5.8.10 or later installed for greater regular expression nesting depth.)