# Include Build Number inside PDF

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?

• 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
• @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
• 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

This should do:

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

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

\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.

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\@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
\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...)
\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