# Can't get real currfile if used in precompiled .fmt file

## Background:

I am not sure if this is a bug with the currfile package, or if I not properly using the package, or perhaps an issue with using .fmt files. BTW, this is my first attempt at using .fmt files.

Since I use the same preamble in all my documents I am trying to speed up compilation by generating a .fmt file as per the referenced questions below, but am having trouble with some of the things that are performed \AtBeginDocument. The example below test to see that the value of the token \MyToken is set to the portion of the file name between the two dashes.

## Problem:

Using the precompiled .fmt file I get:

Note that the value of \currfilename is the file name of the preamble file, which is not what I want (Perhaps some \expandafter magic can help).

However, if I make one complete .tex file with the two given below (and remove the %& MyPreamble), the output you get is the desired behavior. The value of \currfilename is the name of the file.

## Partial Solution

• One partial solution is to use \jobname (as per the commented line in the preamble). Then this produces identical results in both cases. But, I can't use that in my real application as I change \jobname in order to generate multiple versions from the same source document.

## Notes:

• The actual files names here matter.
• To generate the MyPreamble.fmt, I use:

pdflatex -ini -jobname=MyPreamble "&pdflatex MyPreamble.tex\dump"


## Code: MyPreamble.tex:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[realmainfile]{currfile}%

\newtoks{\MyToken}
\MyToken={oo}% set default value
\AtBeginDocument{%
\par\noindent\textcolor{blue}{Debug: currfilename = "\currfilename"}\par%
% These two (StrBefore and StrBetween) statements do not work,
% if this is used via .fmt file.
\StrBefore{\currfilename}{.}[\CurrentFileName]%
\StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]%
%
%\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
\IfStrEq*{\ExtractedValue}{\the\MyToken}{}{%
\par\noindent\textcolor{red}{Error: MyToken (middle) was "\the\MyToken",
but was expected to be "\ExtractedValue".}\par%
}%
}
%% ----- preamble ends here


## Code: foo-x-bar.tex:

%& MyPreamble

% This file need to be named in three parts separated by two dashes.
% This need to be set to be the value in between the two dashes. So,
% if this file is named "foo-x-bar.tex", then this needs to be "x"
\MyToken={x}%

\begin{document}
\lipsum[1]
\end{document}

-

If you use \getmainfile and \themainfile, the correct file name is used:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[realmainfile]{currfile}%

\newtoks{\MyToken}
\MyToken={oo}% set default value
\AtBeginDocument{%
\getmainfile
\par\noindent\textcolor{blue}{Debug: themainfile = "\themainfile"}\par%
% These two (StrBefore and StrBetween) statements do not work,
% if this is used via .fmt file.
\StrBefore{\themainfile}{.}[\CurrentFileName]%
\StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]%
%
%\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
\IfStrEq*{\ExtractedValue}{\the\MyToken}{}{%
\par\noindent\textcolor{red}{Error: MyToken (middle) was "\the\MyToken",
but was expected to be "\ExtractedValue".}\par%
}%
}
%% ----- preamble ends here


With

pdflatex -recorder foo-x-bar


I get at the first run

-

When compiling the document file, TeX does not use \input to load it, hence there's nothing for currfile to hook onto, hence why \currfile is not correct. You can work around this by compiling the document file instead with the command line

pdflatex -recorder -fmt MyPreamble '\input{foo-x-bar.tex}'


(Remember when loading the currfile package with the realmainfile option, you should compile with the -recorder option and compile twice.)

-
Thanks for this solution. This would have required me to change the compile command for each file, so have accepted the other answer. I was about to switch back to using \jobname and adapt your solution to provide the fix for the case where I could not use jobanme, before egreg came to the rescue. – Peter Grill Nov 7 '12 at 0:38
@PeterGrill: no problem. I too played with \themainfile (and I noticed you were using the realmainfile option to currfile but not using \getmainfile etc) but didn't get a solution as good as egreg's! – cyberSingularity Nov 7 '12 at 8:52