4

I need to automatically include a build number in PDF which increases automatically when it is complied again.

E.g.

Version 1.0.1 for the first build.

Then after the second build, it should be

1.0.2

Do you have any idea to do this?

  • 2
    you have tagged this revision-control but not said which version control system you are using, there are packages to insert rcs/svn/cvs version numbers and questions on this site for git, for example. the details just relate to the software generating the number, – David Carlisle Dec 11 '15 at 9:11
  • Looks like using LaTeX itself for this purpose. – AlexG Dec 11 '15 at 9:26
  • I am not using any revision control now. I make large documents and I need to insert just counter value which increase with each build. – Kasun Dec 11 '15 at 9:30
  • 1
    @Kasun "I am not using any revision control now" You should have a look at Why should I use version control if I'm working alone and already back up regularly?. I even wrote me a small bash script to commit on every CTRL+S... – Tobias Kienzler Dec 11 '15 at 14:26
  • 1
    Sorry, but I'm struggling more than usual to understand what end result you are trying to achieve. A 'build number' as you describe it doesn't appear to be very useful, when, say, compared with a git commit reference. For example, it is unrelated to the developmental state of the document, and cannot be used to repeat that state. Would you care to add some clarification to the question? – Brent.Longborough Dec 15 '15 at 10:43
8

This should do:

\documentclass[twoside]{article}
\usepackage{kantlipsum}

\AtBeginDocument{
  \ifdefined\buildcount\else\gdef\buildcount{0}\fi
  \xdef\buildcount{\the\numexpr\buildcount+1\relax}

  \pagestyle{myheadings}
  \markboth{}{Build No. 1.0.\buildcount}
}

\makeatletter
\AtEndDocument{
  \write\@mainaux{
    \string\gdef\string\buildcount{\buildcount}
  }
}
\makeatother

\begin{document}
\kant[1-20]
\end{document}

The *.aux file may never be deleted. Otherwise the information gets lost.

| improve this answer | |
1

Here's another possibility:

\documentclass{minimal}

\makeatletter
\def\set@docident{%
  \begingroup
    % yes, a big \ifcase would be more straightforward;
    % no, that wouldn't be any fun at all
    % (also it ends up looking messy and even tricksier).
    \def\@step##1##2\@nil{\advance\@tempcnta##1 \def\@tempa{##2}}
    \def\@tempa{{31}{28}{31}{30}{31}{30}{31}{31}{30}{31}{30}{31}}%
    \@tempcnta=\day             % day of month
    \@tempcntb=\month           % month of year (unit-offset)
    \loop
      \advance\@tempcntb-1
      \ifnum \@tempcntb>0
        \expandafter\@step\@tempa\@nil
    \repeat
    \@tempcntb=\year            % yes, do calculate leap years
    \divide\@tempcntb 4 \multiply\@tempcntb 4
    \ifnum\@tempcntb=\year
      \ifnum\month>2            % but let's not worry about century years (slack...)
        \advance\@tempcnta 1
      \fi
    \fi
    \xdef\docident{%
      \the\@tempcnta            % day-of-year
      -\the\time}               % minutes since midnight
  \endgroup
}
\set@docident
\makeatother

\begin{document}

The docident is \docident.

\end{document}

This sets a \docident macro which produces a string which increases monotonically, and which doesn't depend on any stored state.

I use this to reassure me that a document – in my case a printed exam paper – is the same version, when it's handed out to students, as it was when I printed off the canonical version at the end of proofing. It's not bulletproof, but since it's sensitive to the time at which the LaTeX run was done, it provides a little more assurance than a VCS revision number alone.

It's actually an obfuscated date-time, with the same good and bad properties.

And yes, this is rather self-indulgently more complicated than it absolutely needs to be; but yes, even so, it will indeed give the wrong day-of-year in March–December 2400.

A simpler version, but equivalent, would be

\def\set@docident{%
  \@tempcnta=\month
  \multiply\@tempcnta 12
  \advance\@tempcnta\day
  \multiply\@tempcnta 1440
  \advance\@tempcnta\time
  \xdef\docident{\the\@tempcnta}}
| improve this answer | |
  • Multiple compilation runs in succession did not change the version number. At which interval is it supposed to change? Every elapsed minute of walltime? Thus, it is more kind of time-stamp than compilation counter. – AlexG Dec 11 '15 at 14:31
  • @AlexG Yes, as noted, it's really just an obfuscated timestamp, with minute resolution. This worked for me because it was unique enough, but without potentially distracting readers by appearing alongside other dates on the page. – Norman Gray Dec 11 '15 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.