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}
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