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\jobnamein order to generate multiple versions from the same source document.
References:
- Custom format file: How to automate the generation of the "precompiled preamble" whenever one of the files that are used there are changed
- ultrafast pdflatex with precompiling
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}

\jobnameand adapt your solution to provide the fix for the case where I could not usejobanme, before egreg came to the rescue. – Peter Grill Nov 07 '12 at 00:38\themainfile(and I noticed you were using therealmainfileoption tocurrfilebut not using\getmainfileetc) but didn't get a solution as good as egreg's! – cyberSingularity Nov 07 '12 at 08:52