12

is there a way to create different output files (pdf) with differen versions for each compiling? For example:

  1. compile -> V0.1
  2. compile -> V0.2

and so on... I know about the package vrsion but it is not what I am looking for. Dont need the versioning on the footer etc. :)

Takeru
  • 447
  • 2
    if you use VMS operating system, then this is done automatically by the system. http://en.wikipedia.org/wiki/Files-11 "Every file has a version number, which defaults to 1 if no other versions of the same filename are present (otherwise one higher than the greatest version). Every time a file is saved, rather than overwriting the existing version, a new file with the same name but an incremented version number is created" You can use purge to remove old versions. I wonder if TexLive 2013 runs on VMS? – Nasser Jul 05 '13 at 01:48
  • 1
    if you use ZFS as filesystem, you can create a snapshot after each compile and than you have the different results in the filesystem. However, this is not useful, but - instead of the VMS hint above - it can be use with TeXlive 2013 – Micha Jul 05 '13 at 03:41
  • How would you like the versioning to be apparent? Would you like different files, different output, or both? – Sean Allred Jul 05 '13 at 03:52
  • Well, I guess it's possible, but a bit beyond my TeX knowledge. Create a counter that is saved for the next run in a plain text file (maybe http://tex.stackexchange.com/questions/64270/macro-saving-a-value-for-next-run) and increased at each run. Create a bash/powershell/batch script which reads the file and sets the jobname to -. – Uwe Ziegenhagen Jul 05 '13 at 05:03
  • Just out of curiosity, I know from myself that I compile the document I'm working on quite often. Just to make sure that, if an error occurs, I don't have to search to find the place the error must be in. Aren't you afraid to end up with version V0.9999999 etc? Although versioning is a good thing, to many versions can actually be worse because the differences will be so small. – Mythio Jul 05 '13 at 10:25
  • 1
    @Nasser Sorry, I am using Windows 7 with TeXStudio. Never heard of VMS before. – Takeru Jul 05 '13 at 10:53
  • Micha Sorry, also not using ZFS, in fact, I don't even know what it is. :D

    SeanAllred Hmm, I thought about something like different PDF output files. I have 1 folder with all my files and there I have 100 PDF files if I compile 100 hundred times.

    UweZiegenhagen Hmm no idead hat bash/pwershell is :(

    Mythio Yeah, that could be a problem but I have got my thesis here and I am always changing things so I want to keep track of what I am changing with this. Another possible approach would be a changelog maybe?

    – Takeru Jul 05 '13 at 11:00
  • 1
    if you want to keep track of changes, my recommendation is to use a version control system (git, mercurial, bazaar...) – guillem Jul 05 '13 at 11:33

2 Answers2

9

From the magnificient Heiko Oberdiek I received a nice answer with helpful comments:

  • Instead of writing the number to the file, write the command to set the counter to the file. Then one can load it easily using \InputIfFileExists{\jobname.runs}{}{} and has covered the case that the file is missing in the first run.
  • Instead of \arabic{run} the example uses \number\value{run} or \the\value{run}, then any modification of\arabic` won't hurt
  • Write the file at the end of the compilation, then there's high probability that the run was successful.

Example file:

\documentclass{article}

\newcounter{run}
\InputIfFileExists{\jobname.runs}{}{}
\stepcounter{run}

\usepackage{atveryend}
\usepackage{newfile}
\AtVeryEndDocument{%
  \newoutputstream{runs}%
  \openoutputfile{\jobname.runs}{runs}%
  \addtostream{runs}{\string\setcounter{run}{\number\value{run}}}%
  \closeoutputstream{runs}%
}

\begin{document}

Output: V0.\therun

\end{document}

To set the name of the PDF file I tried to modify \jobname which unfortunately does not work, as we use \jobname to specify the name of the file where our setcounter command is written to. What I do now is to use Powershell with regexps to get the run number and rename the file after the run:

$filename = "counterfun4"
$content = [IO.File]::ReadAllText("C:\Users\Uwe\desktop\counterfun4.runs")
$content = [regex]::escape($content)

if ($content -match '\\setcounter\\{run}\\{(\d+)\}')
{
    $run = $($matches[1])
} else {
    $run = "a"
}

pdflatex "$filename.tex"

Rename-Item "C:\Users\Uwe\desktop\counterfun4.pdf" ("counterfun4-"+ $run +".pdf")

The Powershell code can for sure be improved however the example is fully working. A Linux solution is for sure possible, maybe one can contribute one.

EDIT 2013-07-07:

Heiko has sent another version which just uses the plain number in the external file, what this makes the external processing easier:

\documentclass{article}

\newcounter{run}
\usepackage{catchfile}
\IfFileExists{\jobname.runs}{%
  \begingroup
    \CatchFileEdef\tmp{\jobname.runs}{\endlinechar=-1\relax}%
    \setcounter{run}{\tmp}%
  \endgroup
}{}
\stepcounter{run}

\usepackage{atveryend}
\usepackage{newfile}
\AtVeryEndDocument{%
  \newoutputstream{runs}%
  \openoutputfile{\jobname.runs}{runs}%
  \addtostream{runs}{\number\value{run}}%
  \closeoutputstream{runs}%
}

\begin{document}

Output: V0.\therun

\end{document}
Uwe Ziegenhagen
  • 13,168
  • 5
  • 53
  • 93
8

With bash (normal shell with most Linux distros) this is easy. For example make file named pdflatex to somewhere in PATH (like ~username/bin or /usr/local/bin for all users) and in it put

#!/bin/bash

file=${1%.tex}
version=$(ls $file.*.pdf | rev | cut -f 2 -d . | rev | sort -rn | head -1)
version=$((version+1))
/usr/bin/pdflatex $file.tex 
mv $file.pdf $file.$version.pdf

Or make an alias of this, they will also be found before executables in /usr/bin.

E: If you use -shell-escape and have filesystem supporting extended attributes you could also do something like

\documentclass{article}

\begin{document}

\makeatletter
\newcommand{\versionnumber}{\@@input|"attr -q -g version \jobname.tex || echo 0"}
\makeatother

\newcounter{version}
\setcounter{version}{\versionnumber}
\stepcounter{version}

This pdf is compile number \theversion.

\immediate\write18{/usr/bin/attr -s version -V \theversion \space \jobname.tex}

\write18{/usr/bin/mv \jobname.pdf \jobname.\theversion.pdf}

\end{document}