5

I use Mercurial. Imagine the following scenario. Suppose I have a document which is in a state of ongoing revision. I print out a version and file it. Some time later, I want to check whether the version I have on disk has changed compared to the version I have printed out. For simplicity assume that the version I have on disk has no local changes; i.e. the current version in version control is also the most recent version.

It would be handy to have some identifier as part of the file, say for example the revision when the file was last changed. Then I could look at that and see that there has been no change since I last printed the file.

Now, Mercurial actually has an extension that does something similar, namely the keyword extension. However, this has problems, and is officially disapproved of by the Mercurial founder and project leader, Matt Mackall.

After looking at this, it occurred to me that though using this extension could be problematic, there was no real problem using LaTeX to embed version control information in a LaTeX file. And nearly all text documents I write are LaTeX documents.

I see there is prior art by Martin Scharrer. Any tips on how to do similar things in Mercurial?

EDIT: To be clear, my intention was not to use the keywords extension, but rather call hg directly from TeX as part of the typesetting.

Faheem Mitha
  • 7,778
  • 2
    You could adopt a similar approach to the vc bundle but with a script that write the info from hg to some file that tex reads... – Seamus Jun 13 '14 at 09:06
  • I have used mecurial but don't have a live access now, but there have been packages to do this for rcs. cvs svn etc so hg should be no different. It would help (as always) if you posted a complete document with a mecurial keywords comment that showed the format. althought looking at that documentation pag eit looks like you wouldn't have to do anything, just put $HGdate$ in the document header and it'll get changed to teh checkout date – David Carlisle Jun 13 '14 at 09:17
  • @DavidCarlisle Were you suggesting using the keywords extension itself? I was hoping to bypass that. Maybe call hg directly from TeX? – Faheem Mitha Jun 13 '14 at 09:19
  • @Seamus Thanks for the suggestion. vc does indeed look suitable. However, it also does not appear to be in TeX Live, unfortunately, nor does it support Mercurial currently. – Faheem Mitha Jun 13 '14 at 09:25
  • @FaheemMitha : is it something like an equivalent of git2info for mercurial you are looking for ? – Clément Jun 13 '14 at 09:27
  • @Clément yes, something similar. However, I'd like something that allows users to define their own tags. – Faheem Mitha Jun 13 '14 at 09:32
  • What about a commit hook? You could instruct hg to write a .tex file containing some simple command like \date{\today -- $HG_NODE} into a version.tex file you \input in your main file in the appropriate place, every time you commit. See this – Bordaigorl Jun 13 '14 at 10:07
  • @FaheemMitha vc bundle is just a simple script that calls your VCS to get various bits of information (commit date, version number etc) and writes the output into a tex file into the definition of a tex macro. This macro can then be used to put the info in your document. It should be pretty easy to write up the relevant mercurial script if you know mercurial. – Seamus Jun 13 '14 at 11:34
  • @Bordaigorl This is the approach vc bundle takes. – Seamus Jun 13 '14 at 11:34
  • @Seamus I think it should be possible to call hg directly from the TeX file, right? – Faheem Mitha Jun 13 '14 at 13:41
  • @FaheemMitha Yes. This is what vc bundle does. You can call shell scripts from within tex using \write18, I don't know the details, but look at the vc bundle documentation for how to do it. – Seamus Jun 13 '14 at 13:49
  • @Seamus: Great, thanks. I think that is a reasonable approach, though one has to explicitly allow the write18 call. – Faheem Mitha Jun 13 '14 at 13:56

1 Answers1

6

The following one-liner, if placed in a LaTeX file, gives the Mercurial changeset identification hash. Substitute rev for node to get the local revision number.

\input{"| hg log -v -l 1 \jobname.tex --template '{node}'"}

For this to work, running LaTeX with the -shell-escape option is required. Thanks to @egreg and @DavidCarlisle for providing most of the information for this answer in chat.

Faheem Mitha
  • 7,778