1

I made the following code to add a revision number to my latex header. I rely on a .txt file, which is assumed to

  1. exist in the same directory.
  2. always have a number. Anything else will probably make it break

The packages I use are:


\usepackage{xifthen} 
\usepackage{calculator}

The code to get the number from the txt file and increment it by one is:


\newread\file   %starts a new file reader
\openin\file=tracking.txt   %define 'file' as the 'tracking.txt' file from the tex directory
\def\lastNum{}   %declares a variable
\read\file to \lastNum  %read the content of the txt file into the variable
\ADD{1}{\lastNum}{\version} %increment it by 1 and saves in a new variable
\newwrite\file          %starts a file writer
\immediate\openout\file=tracking.txt   % open the file
\immediate\write\file{\version}      %write the new incremented value
\closeout\file        %closes the file

I then use the \version in my header.

I would like to make this a little more robust and reusable. So I have been trying to find ways to handle cases like:

  1. if the tracking.txt file does not exist, then create it.
  2. if the tracking.txt exists, but empty or has some illegal text, then start at 0 (write zero to it). Illegal text would be anything other than an a number

using the xifthen package, i added this line of code...


\ifthenelse{isempty{read\file}}{\lastNum{0}}{read\file to \lastNum}

This is the full, non working, version:


\newread\file   %starts a new file reader
\openin\file=tracking.txt   %define 'file' as the 'tracking.txt' file from the tex directory
\def\lastNum{}   %declares a variable
\read\file to \lastNum  %read the content of the txt file into the variable
\ifthenelse{isempty{read\file}}{\lastNum{0}}{read\file to \lastNum} %check
\ADD{1}{\lastNum}{\version} %increment it by 1 and saves in a new variable
\newwrite\file          %starts a file writer
\immediate\openout\file=tracking.txt   % open the file
\immediate\write\file{\version}      %write the new incremented value
\closeout\file        %closes the file

I keep getting errors in the ADD method.. there is something wrong in the read file I think. Any tips would be highly appreciated! I am new to latex programming-- please excuse my primitive questions and crude code.

0

Why don't you use the aux file, and latex's label-ref system?

\documentclass{report}
\usepackage{refcount}
\makeatletter
\AtBeginDocument{
    \newcounter{version}        % start version counter, associated label is lblversion
    \setcounterref{version}{lblversion} % init counter to label value (thanks to refcount)
    \stepcounter{version}       % step to counter for current version, add corresponding label
    \immediate\write\@auxout{   % while we're at it, let's write immediately current version number
        \string
        \newlabel{lblversion}{{\theversion}{}}%
    }%
}
\makeatother

\begin{document}
\theversion

\chapter{chap1}
\end{document}

Each compilation will add one to the counter version, memorised by the label lblversion. However I haven't (yet) found how to get rid of the 'Label(s) may have changed' warning...
Also, this is enclosed in a AtBeginDocument which might be problematic if you want the version number in the title, but as the aux file is read just before the \begin{document}, it is necessary (as long as you use the aux file).

  • Thanks! This works very well. One last question, how do I control the increment. Because of the Latex, BibTex, Latex, Latex, pdflatexs command, it jumps 4 steps at a time. Any ideas? – melkhaldi Mar 31 '15 at 16:09
  • The version comes in the header and front page just fine which is what I need the most. The file name can remain as is. – melkhaldi Mar 31 '15 at 16:16
0

The other answer cannot work for headers as it reads the aux file at begin{document}. However, like you did with a dedicated file, and the InputIfFileExists command:

\documentclass{report}
\newcounter{version}                        % start version counter, no label here
\InputIfFileExists{\jobname.version}{}{}    % read from file, if it exists (might want to consider a ./\jobname.version here to limit to the working folder)
\stepcounter{version}

\makeatletter
{%
  \if@filesw                                                % respect \nofiles
    \begingroup
      \chardef\reserved@c=15                                % same write register as environment `filecontents` uses
      \immediate\openout\reserved@c=\jobname.version\relax  % open/create file to read/write to
      \immediate\write\reserved@c{%                         % instead of writing only version number, we write the assignment
        \string\setcounter{version}{\theversion}%
      }%
      \immediate\closeout\reserved@c
    \endgroup
  \fi
}
\makeatother

\begin{document}
\theversion

\end{document}

This snippet is not really my own (though adapted here to your needs), but I have been using for my external file needs since I found it on tex.sx and I have trouble finding the question again...

  • Thanks! I went with the second answer since it uses the aux :) – melkhaldi Mar 31 '15 at 16:01

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.